0

I have a list of addresses with postcodes, which I need to display on a map (pin point).

So far I know to use co-ordinates to show pin points on map with core-location framework. Is there any other way of doing it? Like getting co-ordinates from address or use address to show pin points on map?

The main purpose is to calculate distances between locations.

Please guide me here. Thanks

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
kimti
  • 21
  • 1
  • 4
  • Please try to search before asking. [How can I plot addresses in Swift, converting address to longitude and latitude coordinates?](http://stackoverflow.com/questions/24706885/how-can-i-plot-addresses-in-swift-converting-address-to-longitude-and-latitude) – Matan Lachmish Jul 20 '16 at 08:00
  • Thank you, some times you don't have right search string when looking for any answers..Appreciate your time and guidance – kimti Jul 20 '16 at 08:09

2 Answers2

1

To create an annotation from an address you need to use CLGeocoder's method geocodeAddressString:

let address = "5th Avenue, New York"
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
    if let placemarks = placemarks {
        if placemarks.count != 0 {
            let annotation = MKPlacemark(placemark: placemarks.first!)
            self.mapView.addAnnotation(annotation)
        }
    }
}

If you only want to get the coordinates for the location:

...
        if placemarks.count != 0 {
            let coordinates = placemarks.first!.location
        }
...
xoudini
  • 7,001
  • 5
  • 23
  • 37
0

Here is the simple code snippet that shows how to show the pin on the map for the given array of address.

let geocoder:CLGeocoder = CLGeocoder()


for eachAddress in addressArray {
    geocoder.geocodeAddressString(eachAddress) { (placemarks:[CLPlacemark]?, error: NSError?) -> Void in
    //Assuming it has the data without any error
    let placemark = MKPlacemark(placemark: (placemarks?.first)!) 
    self.mkMapView.addAnnotation(placemark)
    }
}
Naveen Kumar H S
  • 1,304
  • 16
  • 30