0

I passed the two values fromAddress text field and toAddress text fields and i need to connect the two annotation points like a route. I don't need any directions based on the api's. Just need to connect.

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)


            }
    })


}


}
Praveen Kumar
  • 298
  • 2
  • 15

1 Answers1

0

You can easily get both annotations coordinates and draw an MKGeodisicPoliline on the map view as shown in the code below. The polyline will have 2 annotations.

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

where coordinates array has the location coordinates objects of the annotations.

Petros
  • 342
  • 3
  • 15
  • Hi how do i pass the coordinates stored in the location manager function func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) I need to connect the coordinates stored at the **locations** array, the nth one and the n-1 one – Praveen Kumar Mar 18 '16 at 06:31