mapbox-maps-ios
mapbox-maps-ios copied to clipboard
Unable to move AnnotationView in Mapbox V10
Environment
- Xcode version: 13.0
- iOS version: 15.x
- Devices affected: All
- Maps SDK Version: 10.6.x
I have been using Mapbox v6 but I got mail from Mapbox to upgrade to v10 for the latest updates.
My app uses Mapbox to show custom maps and show live locations for multiple users. In V6, we have implemented custom Annotations and getting custom annotation view from this mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? method. And then we were changing annotationView.coordinate which was working fine and our map was able to show live location data for every user.
In new v10 SDK, I tried to show PointAnnotation which has fix locations and unable to change. To change location for PointAnnotation, we need to remove that annotation from pointAnnotationManager and then add new annotation with updated location which seems quite weird.
I have also tried to use custom views with V10 and added annotation views based on ViewAnnotationOptions which I am unable to see on my map.
Please check my code here (I am not able to see Annotation View):
private func annotation(at location: CLLocation, forDevice device: String) -> UIView? {
guard let mapView = self.mapView else { return nil }
let options = ViewAnnotationOptions(
geometry: Point(location.coordinate),
width: 100,
height: 40,
associatedFeatureId: device,
allowOverlap: false,
anchor: .center
)
guard let userMarker = mapView.viewAnnotations.view(forFeatureId: device) {
let sampleView = createSampleView(withText: "Hello world!")
// Added annotation view when it does not exist
try? mapView.viewAnnotations.add(sampleView, options: options)
return sampleView
}
// Update Annotation's position if already added
try? mapView.viewAnnotations.update(userMarker, options: options)
return userMarker
}
After some modifications to the above code, it worked to show annotation views on the map but randomly it generates multiple annotation views.
private func annotation(at location: CLLocation, forDevice device: String) -> UIView? {
guard let mapView = self.mapView else { return nil }
let options = ViewAnnotationOptions(
geometry: Point(location.coordinate),
width: 100,
height: 40,
associatedFeatureId: device,
allowOverlap: false,
anchor: .center
)
if let userMarker = mapView.viewAnnotations.view(forFeatureId: device) {
// Removed annotation if already exists
try? mapView.viewAnnotations.remove(userMarker)
}
// Created new annotation and added again to the map.
// This seems quite weird as we need to add/remove annotation views just to change location of any annotation.
let sampleView = createSampleView(withText: "Hello world!")
try? mapView.viewAnnotations.add(sampleView, options: options)
//try? mapView.viewAnnotations.update(userMarker, options: options)
return sampleView
}
It seems very difficult for me to understand why changing location of annotations is so hard in newer SDK which should have been very easy.
Please suggest me some best solution to update annotation view's locations on the map. I believe adding/removing for multiple annotations will be a costly operation.
NOTE: I am not trying to show current user's location on the map but need to show other users' locations on the map and locations for these annotations need to be updated as any user moves.
Expected behavior: Annotation need to be moved to specific coordinate locations without adding/removing them from annotation manager.
I have tried to contact customer support for the same but I did not receive any update on this.
Does anyone have any suggestion or workaround for this? Or let me know if Mapbox SDK is not being maintained anymore.
Try to delete associatedFeatureId from parameters.
@xxcombat I did removed associatedFeatureId from the parameters dictionary but still when I need to change location of the Annotation, I have to follow:
try? mapView.viewAnnotations.remove(sampleView)
try? mapView.viewAnnotations.add(sampleView, options: options)
Means, I need to manually remove existing annotation and add a new annotation just to change the location coordinates. Do you think this is optimised solution? Just to change position of the view, why do I need to remove the existing view and add a new one with new position?
Even if I follow this way, my custom Annotation view is not treated as an annotation. In older SDK, I was able to select/deselect annotations from the custom view only. But there is no way to do this in newer SDK.
Is there anything else to improve annotations implementation here or do I need to go through this pain as well?
I am still trying to find solution but unable to find anything. :( @mobihunterz is this issue resolved for you?
Don't use annotations, use SymbolLayer and update the source with
try? mapView.mapboxMap.style.updateGeoJSONSource(withId: "yourLayerID", geoJSON: .featureCollection(FeatureCollection(features:features)))
@mobihunterz to accomplish your use case, you can either switch to point annotations or use the view annotation update method specified here.