Mapbox iOS8 Swift mapView.showUsersLocation

我试图使用Mapbox的iOS8 Swift cocoa插件mapbox和面临一个问题,当试图显示在mapView上的用户位置。 我的代码如下

func mapView(mapView: MGLMapView!, symbolNameForAnnotation annotation: MGLAnnotation!) -> String! { return "secondary_marker" } let manager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() let mapView = MGLMapView(frame: view.bounds, accessToken: "pk.privateMapboxAccessTokenGoesHere") mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight if CLLocationManager.authorizationStatus() == .NotDetermined { manager.requestAlwaysAuthorization() } mapView.showsUserLocation = true let x:MGLUserLocation = mapView.userLocation println(mapView.userLocation) println(x.coordinate.latitude) println(x.coordinate.longitude) ... more code here to show map works. } 

我已经做了必要的更改info.pList,并获得适当的消息,我第一次启动我的应用程序。 问题是,它打印以下内容:

 <MGLUserLocation: 0x7fd8aa6c9a00> 3.40282346638529e+38 3.40282346638529e+38 

任何人都可以提供我如何在地图上显示用户位置(蓝点)的例子。

要让地图自动在用户的位置上,请设置mapView.userTrackingMode = .Follow MGLUserTrackingModeFollow (Objective C中的MGLUserTrackingModeFollow )。

为了仅显示用户的位置(但不移动到),请设置mapView.showsUserLocation = true

您看到mapView.userLocation伪造号码的mapView.userLocation是用户的位置通常在viewDidLoad尚不可用。 使用mapView:didUpdateUserLocation:委托方法在用户位置变得可用并且更新时通知。

感谢给我一个出发点。

我用你的代码,并添加userLocation到我的地图,它显示了我。

  if let userLocation = mapView.userLocation { userLocation.title = "USER LOCATION MARKER" mapView.addAnnotation(userLocation) } 

请注意,我必须插入我的iPhone并从那里运行。 不知道它是否应该在模拟器中工作。