Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geocodeAddressString not working for swift

Hi I am trying to use geocodeAddressString to convert the address to a placemark on the map, but whatever address I passed to this method, the below block will never be executed, could anyone give me some light? Thanks a lot By the way, I can see "before" and "after" on the concole, but without "hello"

  let geo = CLGeocoder()
    print("before")
    geo.geocodeAddressString(("4 Bradford St, Perth WA 6050"), completionHandler: {
        (placemarks, error) -> Void in

        print ("hello")
        if let placemark = placemarks?[0]
       {
          self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
        }
    })

  print ("after")
like image 304
Paddy Wang Avatar asked Jul 14 '17 03:07

Paddy Wang


1 Answers

Use this

import CoreLocation

let address = "1 Infinite Loop, CA, USA"
let geocoder = CLGeocoder()

geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
   if((error) != nil){
      print("Error", error)
   }
   if let placemark = placemarks?.first {
      let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
      }
    })
like image 123
BHAVIK Avatar answered Oct 07 '22 13:10

BHAVIK