更改位置坐标,更改引脚并在地图上重绘路线

时间:2017-09-18 11:16:21

标签: ios swift3 mkmapview mkannotation

我使用故事板创建了一个视图应用程序。我将mkMapView放在ViewController上并将其嵌入导航控制器中。因此程序显示MKMapView中两个引脚之间的路径,即源和目标坐标。

   import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet weak var mapView: MKMapView!
    var s = 0.0
    var t = 0.0
     var sourceLocation = CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)
    var destinationLocation = CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self

        self.dummy()
    }


    func dummy()
    {
        // 1.


        // 2.


        // 3.
        let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
        let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)

        // 4.
        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

        // 5.
        let sourceAnnotation = MKPointAnnotation()
        sourceAnnotation.title = "Times Square"

        if let location = sourcePlacemark.location {
            sourceAnnotation.coordinate = location.coordinate
        }


        let destinationAnnotation = MKPointAnnotation()
        destinationAnnotation.title = "Empire State Building"

        if let location = destinationPlacemark.location {
            destinationAnnotation.coordinate = location.coordinate
        }

        // 6.
        self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )

        // 7.
        let directionRequest = MKDirectionsRequest()
        directionRequest.source = sourceMapItem
        directionRequest.destination = destinationMapItem
        directionRequest.transportType = .automobile

        // Calculate the direction
        let directions = MKDirections(request: directionRequest)

        // 8.
        directions.calculate {
            (response, error) -> Void in

            guard let response = response else {
                if let error = error {
                    print("Error: \(error)")
                }

                return
            }

            let route = response.routes[0]
            self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)

            let rect = route.polyline.boundingMapRect
            self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
        }

    }

我在mapView上放置了一个changeLatitude按钮,我正在更改地图 引脚位置通过将当前源引脚位置分配到目标和源引脚位置纬度 s = s + 0.02000 递增。并将源引脚放在新位置。

@IBAction func changeLatitude(_ sender: Any) {
     destinationLocation = CLLocationCoordinate2D(latitude: t, longitude: -73.984472)

    s=s+0.02000
    sourceLocation = CLLocationCoordinate2D(latitude: 40.759011-s, longitude: -73.984472)

     t=40.759011-s;
    self.refresh()
    self.dummy()

    //self.refresh(sender: AnyObject)

}

我已从旧源和旧目的地位置移除旧针,但旧路线仍在那里我想删除旧路线。如何删除旧路线?

   func refresh() {
        mapView.setCenter(mapView.userLocation.coordinate, animated: true)
        if self.mapView.overlays.count > 0
        {
            //let allAnnotations = self.mapView.annotations
            //self.mapView.removeAnnotations(allAnnotations)
           // self.mapView.removeOverlays(self.mapView.layoutGuides)
            self.mapView.removeAnnotations(self.mapView.annotations)


        }

    }

我使用about refresh()函数

中的以下语句从KMmapView中删除旧引脚
self.mapView.removeAnnotations(self.mapView.annotations)

如何删除路线?。

您可以从此链接https://www.dropbox.com/s/77m0395nyho9die/IOS9DrawRouteMapKitTutorial1.zip?dl=0

下载示例项目

1 个答案:

答案 0 :(得分:1)

 self.mapView.removeOverlays(self.mapView.overlays)

尝试此操作即可从地图中删除叠加层。