MapBox IOS SDK Swift:如何在不删除的情况下显示特定zoomLevel的用户注释并再次添加?

时间:2016-09-04 15:34:04

标签: swift xcode swift2 mapbox

正如你在这里看到的,每次缩放级别改变时,我都会删除并添加注释,但问题是它太慢而应用程序卡住了。我搜索了整个MapBox文档,但没有找到更好的方法来实现它。

isFull,isMedium和isEmpty只是我需要的缩放级别的标志。

感谢帮助者:)

内部函数mapView(mapView:MGLMapView,regionDidChangeAnimated animated:Bool)     {         if(mapView.zoomLevel> 14)         {

        isFull = true
        isMedium = false
        isEmpty = false
        if (gotMyHistory)
        {
            if (mapView.annotations?.count > 0){mapView.removeAnnotations(mapView.annotations!)}
        LoadAnnotation()
        }
    }else if (mapView.zoomLevel <= 14 && mapView.zoomLevel >= 8 && !isMedium )
    {

        isFull = false
        isMedium = true
        isEmpty = false
        if (gotMyHistory )
        {
            if (mapView.annotations?.count > 0){mapView.removeAnnotations(mapView.annotations!)}
            LoadAnnotation()
        }
    }else if (mapView.zoomLevel < 8 && !isEmpty)
    {
        isFull = false
        isMedium = false
        isEmpty = true
        if (gotMyHistory)
        {
            if (mapView.annotations?.count > 0){mapView.removeAnnotations(mapView.annotations!)}
        }
    }

}

2 个答案:

答案 0 :(得分:2)

我意识到这可能与原始海报无关,因为已经过了这么多时间,但希望有助于其他人。 我遇到了同样的问题,经过3种不同的实现(首先是像你这样的注释),我发现创建自己的地图图层是最顺畅的选择。

  // Wait until the style is loaded before modifying the map style
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {

    //Bicycle Parking Annotations Layer
    let bicycleParkingSource = MGLShapeSource(identifier: "bicycleParking", features: [], options: nil)
    self.bicycleParkingSource = bicycleParkingSource

    style.setImage(UIImage(named: "bicycleParking")!, forName: "bicycleParking") // referenced by an MGLSymbolStyleLayer’s iconImage

    let bicycleParkingSymbols = MGLSymbolStyleLayer(identifier: "bicycleParking", source: bicycleParkingSource)
    bicycleParkingSymbols.iconImageName = MGLStyleValue(rawValue: "bicycleParking")
    bicycleParkingSymbols.minimumZoomLevel = 12.0

    style.addSource(bicycleParkingSource)
    style.addLayer(bicycleParkingSymbols)
}

我实际上发现Mapbox在完成后有一个很好的教程,它有助于改进我的代码。 https://www.mapbox.com/ios-sdk/examples/runtime-multiple-annotations/ 如您所见,您可以为图层设置最大可见缩放级别。希望这可以帮助!

答案 1 :(得分:0)

您的LoadAnnotation功能是什么样的?性能缺乏可能存在,你可以使用dispatch_async,这对我来说是一个性能提升

let priority = Int(QOS_CLASS_UTILITY.rawValue)
        dispatch_async(dispatch_get_global_queue(priority, 0)) {

            dispatch_async(dispatch_get_main_queue()) {
                //load your markers here
            }
        }