繁体   English   中英

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

[英]Swift 2 MapKit Annotations - How to group annotations?

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

所有纬度和经度都已作为Double包含在CoreData中。

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

当用户单击注释时,如果该注释是分组的注释,我想向用户显示该位置有多少个注释,“强迫”用户放大以分别查看注释。

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

谢谢!

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

我已经知道了

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

只是我的代码的更正:

//....

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

    //....
}

//....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM