2016-08-04 74 views
0

我正在尝试MGLMapView.visibleFeaturesAtPoint,但总是找回空数组。有人能告诉我我在这里做错了吗?Mapbox iOS SDK - 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 
} 
} 

回答

1

问题是由地图上的团队回答Github

“,此问题重现,如果您将调用visibleFeatures(在:)到viewDidAppearAnimated(:)或mapViewDidFinishLoadingMap( :)?通过viewDidLoad中()运行后,视图控制器已加载的时间,但地图视图可能没有机会完全加载样式或瓷砖。“

显然把它放在viewDidLoad()中意味着地图尚未完全加载,所以特征数组返回为空。将它移动到mapViewDidFinishLoadingMap()解决了问题。