MapAppMaker
MapAppMaker

Reputation: 31

Mapbox iOS SDK - visibleFeaturesAtPoint returns empty array

I am trying the MGLMapView.visibleFeaturesAtPoint but am always getting back an empty array. Can someone tell me what I am doing wrong here?

Posted below is my code which is basically the adding the marker example (https://www.mapbox.com/ios-sdk/examples/marker/) but using the same point to get visible features at the marker point.

import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
    super.viewDidLoad()

    let mapView = MGLMapView(frame: view.bounds)
    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    // Set the map’s center coordinate and zoom level.
    mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
    view.addSubview(mapView)

    // Set the delegate property of our map view to `self` after instantiating it.
    mapView.delegate = self

    // Declare the marker `hello` and set its coordinates, title, and subtitle.
    let hello = MGLPointAnnotation()
    hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
    hello.title = "Hello world!"
    hello.subtitle = "Welcome to my marker"

    // Add marker `hello` to the map.
    mapView.addAnnotation(hello)

    //let ptTest = CGPoint(x: 1, y: 1)

    print(mapView.visibleCoordinateBounds)

    let ptTest = mapView.convertCoordinate(hello.coordinate, toPointToView: mapView)
    print(ptTest)
    print(mapView.visibleFeatures(at: ptTest))

}

// Use the default marker. See also: our view annotation or custom marker examples.
func mapView(mapView: MGLMapView, viewForAnnotation annotation: MGLAnnotation) -> MGLAnnotationView? {
    return nil
}

// Allow callout view to appear when an annotation is tapped.
func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    return true
}
}

Upvotes: 0

Views: 650

Answers (1)

MapAppMaker
MapAppMaker

Reputation: 31

Question was answered by mapbox team on Github.

"Does the issue reproduce if you move the call to visibleFeatures(at:) to viewDidAppearAnimated(:) or mapViewDidFinishLoadingMap(:)? By the time viewDidLoad() runs, the view controller has loaded, but the map view may not have had a chance to load the style or tiles completely yet."

Apparently putting it in viewDidLoad() meant that the map had not fully loaded yet and so the features array was returned empty. Moving it to mapViewDidFinishLoadingMap() fixed the issue.

Upvotes: 2

Related Questions