问题
Im trying using Mapbox iOS8 Swift cocoa plugin for mapbox and facing an issue when trying to show the users location on a mapView. My code is the following
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've made the necessary changes to info.pList and get the appropriate message the first time i fire up my app. The issue is, it prints the following:
<MGLUserLocation: 0x7fd8aa6c9a00>
3.40282346638529e+38
3.40282346638529e+38
Can anyone provide me an example of how I can show the users location (blue dot) on the map.
回答1:
To have the map automatically center on the user's location, set mapView.userTrackingMode = .Follow
(MGLUserTrackingModeFollow in Objective C).
To merely show the user's location (but not move to it), set mapView.showsUserLocation = true.
The reason why you're seeing bogus numbers for mapView.userLocation
is that the user's location typically isn't available yet in viewDidLoad
. Use the mapView:didUpdateUserLocation: delegate method to be notified when the user's location becomes available and when it updates.
回答2:
Thanks for giving me a starting point.
I used your code and added the userLocation to my map and it is showing up for me.
if let userLocation = mapView.userLocation {
userLocation.title = "USER LOCATION MARKER"
mapView.addAnnotation(userLocation)
}
Note that I had to plug in my iPhone and run it from there. Don't know if it's supposed to work in the simulator.
来源:https://stackoverflow.com/questions/30914419/mapbox-ios8-swift-mapview-showuserslocation