2017-01-22 65 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可视化地图上的用户位置。

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 
    } 
} 

在UIViewController或任何其他(如服务类)与至少两种功能-mapViewDidFinishLoadingMap实现MGLMapViewDelegate: 和-mapView: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 -mapView:viewForAnnotation:委托函数被调用,包括用户注释视图中的每个注释视图实例。

可选:为了让您的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'没有被调用。我发现这是因为我在storyboard中设置了'mapView',并且在那里我设置了'ShowUserLocation'为'On'。将它设置为'Default'后,我的'viewForAnnotation'被调用。在代码中使用'mapView.showsUserLocation'确定。 – Andrej

+0

@Andrej谢谢你解决了我的问题!另外,我必须调用'CustomUserLocationAnnotationView'上的'setNeedLayout()'来调用'layoutSubviews()'。 – huggie