Mapbox iOS8 Swift mapView.showUsersLocation

I am trying to use the Mapbox iOS8 Swift cocoa Mapbox plugin for mapbox and am encountering a problem while trying to show users location on mapView. My code is as follows

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

I made the necessary changes to info.pList and get the corresponding message when I first launch my application. The problem is this:

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

Can someone provide me an example of how I can show the location of users (blue dot) on the map.

+1
source share
2 answers

To automatically center the map on the user's location, set mapView.userTrackingMode = .Follow ( MGLUserTrackingModeFollow in Objective-C).

To simply show the user's location (but not go to it), set mapView.showsUserLocation = true .

The reason you see dummy numbers for mapView.userLocation is because the user's location is usually not yet available in viewDidLoad . Use the delegation method mapView:didUpdateUserLocation: to receive notifications when a user's location becomes available and when it is updated.

+1
source

Thanks for giving me a starting point.

I used your code and added userLocation to my map and it appears for me.

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

Notice that I had to connect my iPhone and run it from there. I don’t know if he should work in the simulator.

0
source

All Articles