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

标签 ios swift annotations mapkit latitude-longitude

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

所有的纬度和经度都已经作为 Double 进入了 CoreData。

如果我在同一个点有两个不同的注释图像,我想显示两组,一组带有十字,一组带有心脏。

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

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

谢谢!

enter image description here

enter image description here

enter image description here

enter image description here

最佳答案

我已经知道了。

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

关于ios - Swift 2 MapKit Annotations - 如何对注释进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673266/

相关文章:

ios - IBDesignable View 内的 IBInspectable 按钮

ios - 与iOS应用程序的adodeal集成显示错误

ios - 我的带有自动布局约束的 Swift 4 UIScrollView 不滚动

ios - 如何正确消除这个并发编译错误?

java - 使用entityManager加载集合

ios - 具有与 CoreData 相同类型的持久性和非持久性对象

iOS 辅助功能 : Custom voice over text for bundle display name

ios - 在 iOS 中查找 Submesh 的边界框

.net - 如何允许用户在 WPF 中向 UI 控件添加注释?

java - 如何在 Spring 将@Scheduled(cron) 与SpEL 一起使用?