2017-01-22 1 views
1

사용자 위치 주석의 모양을 변경하려고합니다. 나는 이것이 지금 MGLUserLocationAnnotationView을 사용하여 가능하다는 것을 이해하지만, 이것을 구현하는 방법을 잘 모르겠습니다. 누구나 이것이 어떻게 수행되는지에 대한 간단한 예를 제공 할 수 있습니까?Swift 3 - Mapbox - 사용자 위치 사용자 지정 사용자 지정 주석

감사합니다.

+0

합니까 [이 질문] (http://stackoverflow.com/q/40279217/1072229) 도움? –

+0

구체적인 예를 더 찾고있었습니다. – JordanW

+0

그 대답은 'MGLFaux3DUserLocationAnnotationView' 예제에 연결되었습니다. '.h'와'.m' 파일은 완전한 구현 인 것 같습니다. –

답변

7

당신 말이 맞습니다. MapBox API MGLUserLocationAnnotationView 설명이 매우 짧습니다. 사용자 위치보기 맞춤 설정은 MapBox iOS SDK 3.4.0부터 사용할 수 있습니다. See also the feature comments on the MapBox GitHub

중요 사항 : MGLUserLocationAnnotationView는 MGLAnnotationView의 하위 클래스입니다. 즉, MGLUserLocationAnnotationView는 일반 주석보기처럼 작동합니다.

다음은 사용자 위치보기를 사용자 지정하는 방법의 예입니다. 새 클래스 (예 : CustomUserLocationAnnotationView)를 만들고 layoutSubviews()를 재정 의하여 맞춤 코드를 추가합니다. 이 예제에서는 UIImage UserLocationIcon.png를 사용하여 맵에서 사용자 위치를 시각화합니다. 및 -mapView : 당신의 UIViewController 또는 어떤 다른 사람 (예를 들어, 서비스 클래스) 적어도 두 가지 기능 -mapViewDidFinishLoadingMap으로 MGLMapViewDelegate을 구현에서

import Foundation 
import UIKit 
import Mapbox 

final class CustomUserLocationAnnotationView: MGLUserLocationAnnotationView { 
    override init(reuseIdentifier: String?) { 
     super.init(reuseIdentifier: reuseIdentifier) 
    } 

    override init(frame: CGRect) { 
     super.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     // Force the annotation view to maintain a constant size when the map is tilted. 
     scalesWithViewingDistance = false 

     layer.contentsScale = UIScreen.main.scale 
     layer.contentsGravity = kCAGravityCenter 

     // Use your image here 
     layer.contents = UIImage(named: "UserLocationIcon")?.cgImage 
    } 
} 

viewForAnnotation :. viewForAnnotation :

extension ViewController: MGLMapViewDelegate { 
    // Wait until the map is loaded before proceed with other actions on map 
    func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) { 
     // Show the user location here 
     mapView.showsUserLocation = true 
    } 

    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? { 
     // Customise the user location annotation view 
     if annotation is MGLUserLocation { 
      var userLocationAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "CustomUserLocationAnnotationViewIdentifier") as? CustomUserLocationAnnotationView 

      if userLocationAnnotationView == nil { 
       userLocationAnnotationView = CustomUserLocationAnnotationView(reuseIdentifier: "CustomUserLocationAnnotationViewIdentifier") 
      } 

      // Optional: You can save the annotation object for later use in your app 
      self.userLocationAnnotation = annotation 

      return userLocationAnnotationView 
     } 

     // Customise your annotation view here... 

     return customAnnotationView 
    } 
} 

지도보기 -mapView : 내 의견 인라인을 참조 위임 기능은 사용자 주석보기를 포함하여 모든 주석보기 인스턴스라고합니다.

선택적 : 당신의 CustomUserLocationAnnotationView 인스턴스를 얻으려면, 당신은 당신의 코드에서 모든 곳에서이 기능을 사용할 수 있습니다

// The userLocationAnnotation was previously saved in your ViewController in the -mapView:viewForAnnotation: delegate function 
    let view = mapView.view(for: self.userLocationAnnotation) as? CustomUserLocationAnnotationView 
+0

이상하게 들리지만 사용자 추적을 시작할 때 특수 효과에 대한 내보기가 호출되지 않습니다. – huggie

+0

@huggie 또한 동일한 문제가 발생했습니다. 'viewForAnnotation'이 호출되지 않았습니다. 스토리 보드에서'mapView'를 설정했기 때문에 그것이'ShowUserLocation'을'On'으로 설정했기 때문에 알게되었습니다. 'Default'로 설정하면'viewForAnnotation'이 호출되었습니다. 코드에서'mapView.showsUserLocation'을 사용하면 괜찮습니다. – Andrej

+0

@Andrej 감사합니다.이 문제가 해결되었습니다! 또한'layoutSubviews()'를 호출하기 위해'CustomUserLocationAnnotationView'에'setNeedLayout()'을 호출해야했습니다. – huggie

관련 문제