I am trying to show the direction between two locations in Swift. When my app launches, first it shows the current location and then I can see the city which I want on the map using a text field and a button. Now I am trying to show directions between current location and searched city.
Thanks to this function, I can see the city which I want on the map:
@IBAction func myButton(sender: AnyObject)
{
let geoCoder = CLGeocoder()
let addressString = myTextField.text
geoCoder.geocodeAddressString(addressString, completionHandler:
{(placemarks: [AnyObject]!, error: NSError!) in
if error != nil
{
println("Geocode failed with error: \(error.localizedDescription)")
}
else if placemarks.count > 0
{
let placemark = placemarks[0] as! CLPlacemark
let location = placemark.location
self.mapControl(location.coordinate.latitude, longitude: location.coordinate.longitude)
}
})
}
This is my mapControl
function which loads the city on the map:
func mapControl(latitude : Double, longitude : Double)
{
var lastlocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let span = MKCoordinateSpanMake(3, 3)
let region = MKCoordinateRegion(center: lastlocation, span: span)
let regionRadius : CLLocationDistance = 1000
mapView.setRegion(region, animated: true)
}
How can I show the direction between current location and searched city?