如何在mapView iOS中删除两个注释之间的叠加

时间:2019-03-12 09:07:29

标签: swift mapkit mapkitannotation mkpolyline

这是示例图片:

enter image description here

现在,我要删除5和6个注释之间的覆盖。这是到目前为止我尝试过的代码片段:

创建折线的功能

func createPolyline() {
    if (arrTempFootStepsLocation.count>0){ //This array contains all the coordinates from starting annonation 1 to 6 
       let points:[CLLocationCoordinate2D] = arrTempFootStepsLocation

       let geodesic = MKGeodesicPolyline(coordinates: points, count: points.count)
       mapService.addOverlay(geodesic)
     }
}

创建注释的功能

func addAnnotations() {
    if let arrProof = arrTempProof{ //This array contains 6 coordinates which are taken from arrTempFootStepsLocation only to create annotation
       for (index , modelPhotoProof) in arrProof.enumerated() {
            if (modelPhotoProof.locationPoint?.latitude != 0.0 && modelPhotoProof.locationPoint?.longitude != 0.0){

               let CLLCoordType = CLLocationCoordinate2D(latitude: modelPhotoProof.locationPoint!.latitude, longitude: modelPhotoProof.locationPoint!.longitude)
               let anno = MyMKPointAnnotation()
               anno.tag = index
               anno.coordinate = CLLCoordType
               mapService.addAnnotation(anno)      
            }
        }
    }
}


func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        if view.annotation is MyMKPointAnnotation {
            if arrRemovalAnnotationPoint.count<2{ //This array contains selected annotation coordinates
                arrRemovalAnnotationPoint.append((view.annotation?.coordinate)!)
                print("Annotation point inserted.")
            }
        }
    }

删除两个注释之间的叠加层

@IBAction func btnDeletePolylinesPressed(_ sender:UIButton){
        if arrRemovalAnnotationPoint.count == 2 {

            //Find the index of those two annotations from arrTempFootStepsLocation
            let firstAnnoIndex = arrTempFootStepsLocation.index(where: {$0.latitude == arrRemovalAnnotationPoint.first?.latitude && $0.longitude == arrRemovalAnnotationPoint.first?.longitude})

            let lastAnnoIndex = arrTempFootStepsLocation.index(where: {$0.latitude == arrRemovalAnnotationPoint.last?.latitude && $0.longitude == arrRemovalAnnotationPoint.last?.longitude})

            //prepare array to remove coordinates between those two annotations
            if let first = firstAnnoIndex, let last = lastAnnoIndex{

                var arrToRemoved = [CLLocationCoordinate2D]()
                if first > last {
                    arrToRemoved = Array(arrTempFootStepsLocation[last...first])
                    for i in 0..<arrToRemoved.count{

                        let itemToBeRemoved = arrToRemoved[i]

                        for j in 0..<arrTempFootStepsLocation.count - 1 {

                            let itemOriginal = arrTempFootStepsLocation[j]

                            if (itemToBeRemoved.latitude == itemOriginal.latitude && itemToBeRemoved.longitude == itemOriginal.longitude){
                                arrTempFootStepsLocation.remove(at: j)
                            }
                        }
                    }

                    mapService.removeOverlays(mapService.overlays)
                    self.perform(#selector(self.createPolyline()), with: nil, afterDelay: 0.8)

                }
                else {
                    arrToRemoved = Array(arrTempFootStepsLocation[first...last])
                    for i in 0..<arrToRemoved.count{

                        let itemToBeRemoved = arrToRemoved[i]

                        for j in 0..<arrTempFootStepsLocation.count - 1 {

                            let itemOriginal = arrTempFootStepsLocation[j]

                            if (itemToBeRemoved.latitude == itemOriginal.latitude && itemToBeRemoved.longitude == itemOriginal.longitude){
                                arrTempFootStepsLocation.remove(at: j)
                            }
                        }
                    }

                    mapService.removeOverlays(mapService.overlays)
                    self.perform(#selector(self.createPolyline()), with: nil, afterDelay: 0.8)
                }
            }
            arrRemovalAnnotationPoint.removeAll()
        }
}

现在的问题是,当我按Delete键时,它不能清除这些注释之间的所有坐标。现在,当我重画折线时,它仍然在这些注释之间连接。

我的代码有什么问题?还有其他方法吗?任何帮助将不胜感激。

0 个答案:

没有答案