2017-09-18 8 views
-1

私はストーリーボードを使用して1つのビューアプリケーションを作成しました。私はViewControllerにmkMapViewを配置し、プログラムがMKMapViewのソースとデスティネーションの座標である2つのピンの間のルートを示していることをナビゲーションコントローラに埋め込みました。ロケーション座標を変更し、ピンを変更してマップ上のルートを再描画します

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

    } 

宛先とソースピン位置の緯度にのS = SをIがのMapView上changeLatitudeボタンが配置されており、Iは電流源ピンの位置を割り当てることによって、地図 ピン位置を変更してい+ 0.02000は.ANDをインクリメントしますソースピンを新しい位置にドロップします。

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

}

私は古いソースと古い目的地の位置から古いピンを削除したが、古いルートは私が古いルート線を削除することができます.How古いルートを削除したいが残っていますか?

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) 


     } 

    } 

Iおよびルート行を削除する方法

self.mapView.removeAnnotations(self.mapView.annotations) 

についてのリフレッシュ()関数内のステートメントの下に使用してKMmapViewから古いピンを削除します?。

あなたはそれがあなたのマップからオーバーレイを削除するこのお試しくださいhttps://www.dropbox.com/s/77m0395nyho9die/IOS9DrawRouteMapKitTutorial1.zip?dl=0

+0

あなたはその金額であなたの目的地を常に移動する必要がありますか? @frincit –

答えて

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

このリンクからサンプルプロジェクトをダウンロードすることができます。

関連する問題