【问题标题】:Mapbox iOS SDK - visibleFeaturesAtPoint returns empty arrayMapbox iOS SDK - visibleFeaturesAtPoint 返回空数组
【发布时间】:2016-08-19 03:41:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios swift mapbox


    【解决方案1】:

    mapbox 团队在Github 上回答了问题。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 2018-07-25
      相关资源
      最近更新 更多