Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

visibleAnnotations unexpectedly return nil #14531

Closed

Description

@mic12130

After adding an annotation to a visible region of an empty map, visibleAnnotations still return nil, meaning that no annotation is visible.

But the more confusing thing is that the counting will be correct if sleep for a moment after adding the annotation.

Steps to reproduce

  • Count immediately after adding annotation - return nil
        let annotation = MGLPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: 23, longitude: 160)
        setCenter(annotation.coordinate, animated: false)
        addAnnotation(annotation)
        
        print(visibleAnnotations?.count ?? "nil") // return nil
  • Add annotation, take a rest, then count - return 1 as expect
        let annotation = MGLPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: 23, longitude: 160)
        setCenter(annotation.coordinate, animated: false)
        addAnnotation(annotation)
        
        DispatchQueue.global().async {
            sleep(1)
            DispatchQueue.main.async {
                print(self.visibleAnnotations?.count ?? "nil") // return 1
            }
        }

Expected behavior

visibleAnnotations should return the accurate counting of visible annotations, which should be 1 in the code above.

Configuration

Mapbox SDK versions: 4.10
iOS/macOS versions: iOS 12.2
Xcode version: 10.2.1

Activity

  1. added
    iOS
    on Apr 29, 2019
  2. 1ec5 commented on May 13, 2019

    @1ec5
    Contributor

    Most likely, under the hood, mbgl is adding the annotation source and layer asynchronously. The same thing would happen if you add a style layer and immediately try to query for its visible features. The annotation API is designed to be synchronous because that’s how it was originally implemented, but since migrating it to the runtime styling API, it’s possible that the synchronicity is misleading.

  3. julianrex commented on May 13, 2019

    @julianrex
    Contributor

    @pozdnyakov, will your refactoring help here? i.e. will we be able to query for features that have been added, but have yet to be rendered? This is a common use case (and misunderstanding). Certainly, it feels like I should be able to query for something I have just added.

  4. added
    Core
    on Jul 1, 2019
  5. julianrex commented on Jul 1, 2019

    @julianrex
    Contributor

    Behind the scenes visibleAnnotations calls queryRenderedFeatures, which returns cached results. You have to wait about 300ms before results are valid.

    See the following related issues:

    #10778 (comment)
    #12103

    Closing since this is expected and current behavior - though we would like to change this, as it's clearly confusing and error-prone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CoreiOS

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions