2016-03-17 58 views
0

我傳遞了兩個值從地址文本字段和地址文本字段,我需要連接兩個註釋點,如路線。我不需要基於api的任何指示。只需要連接。我如何連接兩個註釋點?

import UIKit 
import MapKit 
import CoreLocation 
import Contacts 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

@IBOutlet weak var fromAddress: UITextField! 
@IBOutlet weak var toAddress: UITextField! 
@IBOutlet weak var map: MKMapView! 

let locationManager = CLLocationManager() 
var location = CLLocation!.self 

@IBAction func getDirection(sender: AnyObject) { 

    ConnectingFromAndTo(fromAddress.text!) 
    ConnectingFromAndTo(toAddress.text!) 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib.} 
} 

func ConnectingFromAndTo(address: String){ 

    CLGeocoder().geocodeAddressString(address, completionHandler: 
     {(placemarks, error) in 

      if error != nil { 
       print("Geocode failed with error: \(error!.localizedDescription)") 
      } else if placemarks!.count > 0 { 
       let placemark = placemarks![0] 
       let location = placemark.location 


       let annotation = MKPointAnnotation() 
       annotation.coordinate = location!.coordinate 
       annotation.title = "\(placemark.locality!)" 
       annotation.subtitle = "\(placemark.country!)" 
       self.map.addAnnotation(annotation) 

       self.map.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake (placemark.location!.coordinate.latitude, placemark.location!.coordinate.longitude), MKCoordinateSpanMake(0.002, 0.002)), animated: true) 


      } 
    }) 


} 


} 

回答

0

您可以很容易地獲得兩個註釋座標並在地圖視圖中繪製MKGeodisicPoliline,如下面的代碼所示。多段線將有2個註釋。

var geodesicPolyline = MKGeodesicPolyline(coordinates: &coordinates[0], count: 2) 
map.addOverlay(geodesicPolyline) 

其中座標數組具有註釋的位置座標對象。

+0

您好如何傳遞存儲在位置管理器功能 FUNC的LocationManager座標(經理:CLLocationManager,didUpdateLocations位置:[CLLocation]) 我需要連接存儲在位置** **陣列的座標,第n個和第n-1個 –