geocodeAddressString not working for swift
Asked Answered
C

2

6

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")
Champaign answered 14/7, 2017 at 3:47 Comment(7)
So, you're never seeing "hello"? And you're sure you're getting to this geocodeAddressString line?Delaware
yes, I put a print("bye") after this call, it can print out successfully, so strangeChampaign
You should create reproducible example of the problem (i.e. blank project, that does nothing but reproduce this particular behavior). I cannot reproduce your problem. And if we can't reproduce the problem, we can't help you solve it.Delaware
By the way, this is not in a playground or command line app, right? It's in an actual iOS project? What version? I was thinking that you might have this problem if geo fell out of scope, but it looks like geocodeAddressString keeps the geocoder alive until the request finishes. And even if you had some cancelGeocode() calls lingering elsewhere in your code, you'd at least see "hello" message.Delaware
Thanks a lot Rob, yes, it's an actually project, and finally got the reason, the print out sequence is: before, after, hello. I think the reason is the handle is called completion handler? so it's only triggered after all the method finish? By the way, I put this piece of code in viewDidload() methodChampaign
Yes, it’s called asynchronously (i.e. later). In your question, you said it was never called. But if you’re saying it actually is called, only later, then that is precisely as expected. It’s very common pattern, where slow processes take a parameter which is a closure that is called when the asynchronous is later completed.Delaware
Just wanted to chime in based on some recent personal experience with this that you may get different results for the same input in different countries. This is because calling it without the region input parameter uses the devices locality to inform the results. In addition, it seems that there is some maximum distance on what it will provide you. For instance: entering "55101" (US zip code) in India provides no results, while it provides the expected results in the US. I have not found a workaround for this, so in this way, this API seems to not be as good as google's equivalent service.Gamboge
K
1

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
      }
    })
Kermit answered 15/7, 2017 at 5:25 Comment(0)
D
0

Try this code:

geocoder.geocodeAddressString(("4 Bradford St, Perth WA 6050"), completionHandler: {(placemarks, error) -> Void in

                    if let placemark = placemarks?.first {
                        let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
                        coordinates.latitude
                        coordinates.longitude


                        self.lat = coordinates.latitude
                        self.long = coordinates.longitude
                        print("lat \(self.lat)")
                        print("long \(self.long)")
                    }
                })
Discipline answered 14/7, 2017 at 5:14 Comment(2)
still no result come back :(Champaign
Ok! could you send your sample output ?Discipline

© 2022 - 2024 — McMap. All rights reserved.