fromAddress テキスト フィールドと toAddress テキスト フィールドの 2 つの値を渡しました。2 つの注釈ポイントをルートのように接続する必要があります。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)
}
})
}
}