首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift 2 MapKit注解-如何对注解进行分组?

Swift 2 MapKit注解-如何对注解进行分组?
EN

Stack Overflow用户
提问于 2015-11-12 22:03:27
回答 2查看 1.5K关注 0票数 2

我的地图中有近8.000个注释,我想根据用户在应用程序中所做的缩放对它们进行分组。

所有的纬度和经度都已经以双倍的形式进入了CoreData。

假设我在同一个点上有两个不同的注释图像,我想展示两组,一组带十字,另一组带心。

当用户单击批注时,如果该批注是分组批注,我想向用户显示在此位置有多少批注,“强制”用户放大以查看各个批注。

下面是我的应用程序的图片和当前的注释。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2015-11-13 22:37:08

我已经知道了。

代码语言:javascript
运行
复制
var zoomLevel = Double()
var iphoneScaleFactorLatitude = Double()
var iphoneScaleFactorLongitude = Double()
var CanUpdateMap: Bool = false

static func getLatitudeLongitudeLimitsFromMap(mapView: MKMapView) -> [String: Double] {
    var coord = [String: Double]()

    let MinLat: Double = mapView.region.center.latitude - (mapView.region.span.latitudeDelta / 2)
    let MaxLat: Double = mapView.region.center.latitude + (mapView.region.span.latitudeDelta / 2)
    let MinLon: Double = mapView.region.center.longitude - (mapView.region.span.longitudeDelta / 2)
    let MaxLon: Double = mapView.region.center.longitude + (mapView.region.span.longitudeDelta / 2)

    coord["MinLat"] = MinLat
    coord["MaxLat"] = MaxLat
    coord["MinLon"] = MinLon
    coord["MaxLon"] = MaxLon

    return coord
}

func LoadMap(mapView: MKMapView) {
    // Get the limits after move or resize the map
    let coord: [String: Double] = getLatitudeLongitudeLimitsFromMap(mapView)
    let MinLat: Double = coord["MinLat"]! as Double
    let MaxLat: Double = coord["MaxLat"]! as Double
    let MinLon: Double = coord["MinLon"]! as Double
    let MaxLon: Double = coord["MaxLon"]! as Double
    var arrAnnotations = [MKAnnotation]()

    let FilterMinLat = arrDicListPinsWithLatitudeLongitude.filter({
        if let item = $0["Latitude"] as? Double {
            return item > MinLat
        } else {
            return false
        }
    })
    let FilterMaxLat = FilterMinLat.filter({
        if let item = $0["Latitude"] as? Double {
            return item < MaxLat
        } else {
            return false
        }
    })
    let FilterMinLon = FilterMaxLat.filter({
        if let item = $0["Longitude"] as? Double {
            return item > MinLon
        } else {
            return false
        }
    })
    let FilterMaxLon = FilterMinLon.filter({
        if let item = $0["Longitude"] as? Double {
            return item < MaxLon
        } else {
            return false
        }
    })

    for Item in FilterMaxLon {
        let dic:[String:AnyObject] = Item
        var Name = String()
        var Address = String()
        var IconPNG = String()

        if let Latitude = dic["Latitude"] as? Double {
            if let Longitude = dic["Longitude"] as? Double {

                    if let item = dic["Name"] {
                        Name = item as! String
                    }
                    if let item = dic["Address"] {
                        Address = item as! String
                    }

                    if let item = dic["TypeID"] as? Int {
                        if item == 11 {
                            IconPNG = "icon-cross.png"
                        } else {
                            IconPNG = "icon-heart.png"
                        }
                    }
                    arrAnnotations.append(CreateAnnotation(Address, Title: Name, Latitude: Latitude, Longitude: Longitude, IconPNG: IconPNG))
                }
            }
        }
    }

    // Show in the map only the annotations from that specific region
    iphoneScaleFactorLatitude = mapView.region.center.latitude
    iphoneScaleFactorLongitude = mapView.region.center.longitude

    if zoomLevel != mapView.region.span.longitudeDelta {
        filterAnnotations(arrAnnotations)

        zoomLevel = mapView.region.span.longitudeDelta

        CanUpdateMap = true
    }
}

func filterAnnotations(arrAnnotations: [MKAnnotation]) {
    let latDelta: Double = 0.04 / iphoneScaleFactorLatitude
    let lonDelta: Double = 0.04 / iphoneScaleFactorLongitude
    var shopsToShow = [AnyObject]()
    var arrAnnotationsNew = [MKAnnotation]()

    for var i = 0; i < arrAnnotations.count; i++ {
        let checkingLocation: MKAnnotation = arrAnnotations[i]
        let latitude: Double = checkingLocation.coordinate.latitude
        let longitude: Double = checkingLocation.coordinate.longitude
        var found: Bool = false

        for tempPlacemark: MKAnnotation in shopsToShow as! [MKAnnotation] {
            if fabs(tempPlacemark.coordinate.latitude - latitude) < fabs(latDelta) && fabs(tempPlacemark.coordinate.longitude - longitude) < fabs(lonDelta) {
                found = true
            }
        }

        if !found {
            shopsToShow.append(checkingLocation)
            arrAnnotationsNew.append(checkingLocation)
        }
    }

    // Clean the map
    for item: MKAnnotation in self.mapRedes.annotations {
        myMap.removeAnnotation(item)
    }

    // Add new annotations to the map
    for item: MKAnnotation in arrAnnotationsNew {
        myMap.addAnnotation(item)
    }
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    // This validation should be added, because it will find all the annotations before the map resize
    if CanUpdateMap == true {
        LoadMap(mapView)
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-11-14 01:05:33

只是对我的代码进行了更正:

代码语言:javascript
运行
复制
//....

func LoadMap(mapView: MKMapView) {
    //....

    // Show in the map only the annotations from that specific region
    iphoneScaleFactorLatitude = Double(mapView.bounds.size.width / 30) // 30 = width of the annotation
    iphoneScaleFactorLongitude = Double(mapView.bounds.size.height / 30) // 30 = height of the annotation

    //....
}

func filterAnnotations(mapView: MKMapView, arrAnnotations: [MKAnnotation]) {
    let latDelta: Double = mapView.region.span.longitudeDelta / iphoneScaleFactorLatitude
    let lonDelta: Double = mapView.region.span.longitudeDelta / iphoneScaleFactorLongitude

    //....
}

//....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33673266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档