Mapbox iOS SDK-visibleFeaturesAtPoint返回空数组 [英] Mapbox iOS SDK - visibleFeaturesAtPoint returns empty array

查看:95
本文介绍了Mapbox iOS SDK-visibleFeaturesAtPoint返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试MGLMapView.visibleFeaturesAtPoint,但总是返回一个空数组.有人可以告诉我我在做什么错吗?

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

下面发布的是我的代码,基本上是添加标记示例( https://www.mapbox.com/ios-sdk/examples/marker/),但使用同一点在标记点获得可见特征.

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
}
}

推荐答案

mapbox团队在 Github .

Question was answered by mapbox team on Github.

如果将调用移至visibleFeatures(at :)到viewDidAppearAnimated( :)或mapViewDidFinishLoadingMap( :),问题是否会重现?viewDidLoad()运行时,视图控制器已加载,但地图视图可能还没有机会完全加载样式或图块."

"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."

显然,将其放入viewDidLoad()意味着地图尚未完全加载,因此features数组返回为空.将其移至mapViewDidFinishLoadingMap()可解决此问题.

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.

这篇关于Mapbox iOS SDK-visibleFeaturesAtPoint返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆