2016-03-23 5 views
0

MKPointAnnotationとユーザーの現在地の間のルートを表示しようとしていますが、失敗しています。MKPointAnnotationとユーザーの現在地の間のルートを迅速に表示する方法2

私の考えは次のとおりです。ユーザーの現在地取得 - >」座標MKPointAnnotationを得ることが - > MKPolylineRenderer

と整列の問題は、私は問題を見つけることができないということです。 :(私は私が変更する必要があります見当がつかない。

class MapInSearch: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 
    @IBOutlet weak var mapView: MKMapView! 
    var destination: MKMapItem? 
    var coords: CLLocationCoordinate2D? 
    let locationManager = CLLocationManager() 

    var PlaceLat = "" 
    var PlaceLong = ""// get from previous view controller 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.locationManager.requestAlwaysAuthorization() 

     // For use in foreground 
     self.locationManager.requestWhenInUseAuthorization() 

     if CLLocationManager.locationServicesEnabled() { 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
      locationManager.startUpdatingLocation() 
     }// step 1 

     self.mapView.showsUserLocation = true 
     self.mapView.delegate = self 
     self.addRoute() // step 2 
    } 

    func addRoute() { 
     var pointsToUse: [CLLocationCoordinate2D] = [] 

     if PlaceLat != "" || PlaceLong != "" { 
     let coords = "\(PlaceLat), \(PlaceLong)" 
     let p = CGPointFromString(coords) 
     pointsToUse += [CLLocationCoordinate2DMake(CLLocationDegrees(p.x), CLLocationDegrees(p.y))] 
     } 
     pointsToUse += [CLLocationCoordinate2DMake(CLLocationDegrees(coords!.latitude), CLLocationDegrees(coords!.longitude))] 
     let myPolyline = MKPolyline(coordinates: &pointsToUse, count: 2) 
     mapView.addOverlay(myPolyline) 

    } 

    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { 
      let lineView = MKPolylineRenderer(overlay: overlay) 
      lineView.strokeColor = UIColor.greenColor() 
      return lineView // step 3 
    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     self.coords = manager.location!.coordinate 
     print("locations = \(coords!.latitude) \(coords!.longitude)") 
    } 

私は4-5のチュートリアルを混入しているため。また、これらのチュートリアルを迅速1.2で書かれています。(私は迅速にそれを編集しようとした私のコードは非常に無秩序です

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    self.mapView.delegate = self 

    var coordinates : [CLLocationCoordinate2D] = []; 
    addRoute(coordinates);  
} 

func addRoute(coordinates: [CLLocationCoordinate2D]) { 

    // insert your code to populate coordinates array with your coordinates 
    polyLine = MKPolyline(coordinates: &coordinates, count: coordinates.count) 
    self.mapView.addOverlay(polyLine, level: MKOverlayLevel.AboveRoads) 
} 

その後に:?2、しかし、私は失敗しています)

答えて

0

あなたは(私たちはMyViewControllerそれを呼び出します)あなたのビューでは、XCodeの7.3におけるスウィフト2の最新の反復を使用して、あなたの問題を解決しました同じファイル:

extension MyViewController: MKMapViewDelegate { 
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { 
     let pr = MKPolylineRenderer(overlay: overlay); 
     pr.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.5); 
     pr.lineWidth = 5; 
     return pr; 
    } 
} 

重要な部分が拡張機能であることがあります。私はこのコードをテストしていないので、入り込んだ問題を修正してください。

関連する問題