mapbox-maps-ios icon indicating copy to clipboard operation
mapbox-maps-ios copied to clipboard

Visible property is removing ViewAnnotation from the list

Open arslankaleem7229 opened this issue 2 years ago • 9 comments

I have integrated a functionality in which I have to show/hide my view annotation but I have seen that once visibility property is set either true or false then I am unable to access that view annotation which is really strange. SO please let me know what's going on. I am testing even after setting a visible to true it is showing but I am unable to access that with associatedFeatureId

print(mapView.viewAnnotations.view(forFeatureId: "abc_xyz") == nil)

This is returning true after setting visible property either true or false no matter

Update:

changing any property is kind of removing the annotation means that you can only update the annotation at once and after that you won't be able to find the annotation by id and that's a strange behavior.

arslankaleem7229 avatar May 24 '23 23:05 arslankaleem7229

@arslankaleem7229 Could you provide some sample code with how you add/update your view annotation?

evil159 avatar May 25 '23 18:05 evil159

@evil159 Sure, here is the code snippet for updating the annotation

//hide
guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: false)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

// show
guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: true)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

arslankaleem7229 avatar May 26 '23 01:05 arslankaleem7229

@arslankaleem7229 Thank you for the clarification, the update code looks good. Could you share the code where you add the view annotation?

evil159 avatar May 30 '23 08:05 evil159

Sure, Here is the code and that's how I am adding annotation


    func addAnnotations(){
            self.addPointAnnotation(with: "abc_xyz")
            self.addViewAnnotation(with: "abc_xyz")
    }
    
    
    private func addPointAnnotation(with id: String) {
        let coordinate = CLLocationCoordinate2D(
            latitude: latitude,
            longitude: longitude
        )
        var pointAnnotation = PointAnnotation(id: id, coordinate: coordinate)
        pointAnnotation.iconImage = "pin_image"
        pointAnnotation.iconAnchor = .bottom
        
        pointAnnotationManager.annotations.append(pointAnnotation)
    }
    
    private func addViewAnnotation(with id: String) {
        do{
            let coordinate = CLLocationCoordinate2D(
                latitude: latitude,
                longitude: longitude
            )
            
            let options = ViewAnnotationOptions(
                geometry: Point(coordinate),
                associatedFeatureId: id,
                allowOverlap: true,
                visible: true,
                anchor: .center,
                selected: true
            )
            
            let annotationView = createAnnotationView()
            try mapView?.viewAnnotations?.add(annotationView, options: options)
        }catch{
        }
    }

arslankaleem7229 avatar May 30 '23 11:05 arslankaleem7229

@evil159 any updates about this issue ?

arslankaleem7229 avatar Sep 17 '23 18:09 arslankaleem7229

Hi @arslankaleem7229 , it is weird to me that you said the view annotation did show/hide when updating its visibility, and in the code you provided there is a guard statement to find the view annotation

//hide
guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: false)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

// show
guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: true)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

so if the view annotation's visibility is getting updated, then the guard statement must go through and we did find the view annotation associated with the given id, so can you please provide full code example and the SDK version you are using to help us reproduce this issue?

maios avatar Nov 15 '23 14:11 maios

Let me clarify this:

For example I have a viewAnnotation with featured "ABC". When I first run this code

guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: false)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

It will do the expected task hide the annotation or infact forget about the hide/show whenever I update the viewAnnotation with any property either its visibility or anything it completes the task but when I search the same viewAnnotation it won't shows up and looks like no viewAnnotation present.

take this thing infact

//hide
guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: false)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

// show
guard let annotationView = self.mapView.viewAnnotations.view(forFeatureId: "abc_xyz") else { return }
let updateOptions = ViewAnnotationOptions(visible: true)
try? self.mapView.viewAnnotations.update(annotationView, options: updateOptions)

If we write this same block of code. viewAnnotataion will hide but won't show again. Second part of code won't detect the viewAnnotation of that featureId

arslankaleem7229 avatar Nov 15 '23 16:11 arslankaleem7229

I see, can you share the MapboxMaps SDK version that you're using?

maios avatar Nov 16 '23 12:11 maios

@maios

  • MapboxMaps (10.15.0):
    • MapboxCommon (= 23.7.0)
    • MapboxCoreMaps (= 10.15.0)
    • MapboxMobileEvents (= 1.0.10)
    • Turf (~> 2.0)

arslankaleem7229 avatar Nov 16 '23 14:11 arslankaleem7229