Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

swift - How do i return coordinates after forward geocoding?

I am trying to see whether the user is within a certain distance of an address. I have successfully managed to get the users location, and convert the address with forward geocoding. I am left with two sets of coordinates. I am trying to make an if statement saying if they are within "a distance", print something!

Currently when i print the coordinates inside the placemark function i get the desired coordinates. When i call them to create eventLatitude and eventLongitude they become 0.0. I know this is a ascycronous problem, but i am unsure on who to resolve this. Can someone give me an example.

My code is below

before the viewdidload i have these variables

var placemarkLongitude = CLLocationDegrees()
var placemarkLatitude = CLLocationDegrees()

then inside the function i set these variables to the placemark coordinates

if let objects = objects {
for object in objects {

    self.geocoder = CLGeocoder()

    //get address from object
    let COAddress = object.objectForKey("Address")as! String
    let COCity = object.objectForKey("City")as! String
    let COState = object.objectForKey("State")as! String
    let COZipCode = object.objectForKey("ZipCode")as! String
    let combinedAddress = "(COAddress) (COCity) (COState) (COZipCode)" //all parts of address
    print(combinedAddress)

    //make address a location

    self.geocoder.geocodeAddressString(combinedAddress, completionHandler: {(placemarks, error) -> Void in

        if(error != nil)
        {

            print("Error", error)
        }

        else if let placemark = placemarks?[0]
        {
            let placemark = placemarks![0]
            self.placemarkLatitude = (placemark.location?.coordinate.latitude)! //THIS RETURNS A VALUE
            self.placemarkLongitude = (placemark.location?.coordinate.longitude)! //THIS RETURNS A VALUE
            print("Longitude: ", self.placemarkLongitude, " Latitude: ", self.placemarkLatitude)
        }
    })

   // user location
   let userLatitude = self.locationManager.location?.coordinate.latitude //THIS RETURNS A VALUE
   let userLongitude = self.locationManager.location?.coordinate.longitude //THIS RETURNS A VALUE
   print("User Location is ", userLatitude, ", " ,userLongitude)
   let userLocation = CLLocation(latitude: userLatitude!, longitude: userLongitude!)

   // event location
   let eventLatitude = self.placemarkLatitude // THIS RETURNS 0.0
   let eventLongitude = self.placemarkLatitude // THIS RETURNS 0.0
   print("Event Location is ", eventLatitude, ", " ,eventLongitude)
   let eventLocation = CLLocation(latitude: eventLatitude, longitude: eventLongitude)

   //Measuring my distance to my buddy's (in km)
   let distance = userLocation.distanceFromLocation(eventLocation) / 1000

     //Display the result in km
     print("The distance to event is ", distance)

     if (distance < 100) {

         print("yay")
     }
 }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are correct about the asynchronous issue. Basically, you cannot do anything after this code:

// [A1]
self.geocoder.geocodeAddressString(combinedAddress, completionHandler: {
    (placemarks, error) -> Void in
    // [B] ... put everything _here_
})
// [A2] ... nothing _here_

The reason is that the stuff inside the curly braces (B) happens later than the stuff outside it (including the stuff afterward, A2). In other words, the code in my schematic above runs in the order A1, A2, B. But you are dependent on what happens inside the curly braces, so you need that dependent code to be inside the curly braces so that it executes in sequence with the results of the geocoding.

Of course this also means that the surrounding function cannot return a result, because it returns before the stuff in curly braces has even happened. The code in my schematic goes A1, A2, return! Only later does B happen. So clearly you cannot return anything that happens in B because it hasn't happened yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...