pgruber pgruber - 4 months ago 67
Swift Question

Mapbox - setCenterCoordinate is deselecting annotations

I'm trying out mapbox (using the ios sdk) and I've run into a problem that I think I've narrowed down pretty far. This is my code:

func centerMap(location: CLLocationCoordinate2D) {
map.setCenterCoordinate(location,
zoomLevel: 14,
animated: true)
}

func mapView(mapView: MGLMapView, didDeselectAnnotation annotation: MGLAnnotation) {
dealDetails.hidden = false
}

func mapView(mapView: MGLMapView, didUpdateUserLocation userLocation: MGLUserLocation?) {
if let currentLocation = userLocation?.coordinate {
centerMap(currentLocation)
}
}


If I don't re-center the map when the user's location is updated (i.e., just commenting out the
centerMap(currentLocation)
call) then the annotation remains selected. Re-centering the map calls the
didDeselectAnnotation
function, and I can't figure out how to keep that annotation selected. Any help is appreciated!

Answer

I don't think there's any way around that if you update the center coordinate. You'd have to re-select the annotation. However, you probably don't need to do that. If you set the userTrackingMode on the map view to .Follow, it should re-center automatically.

Comments