前向地理编码后如何返回坐标? [英] How do i return coordinates after forward geocoding?

查看:78
本文介绍了前向地理编码后如何返回坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看用户是否在地址的一定距离内.我已成功设法获取用户位置,并使用正向地理编码转换了地址.我剩下两套坐标.我正在尝试做一个if语句,说它们是否在一定距离"之内,打印出一些东西!

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!

当前,当我在地标功能内打印坐标时,便获得了所需的坐标.当我打电话给他们创建eventLatitude和eventLongitude时,它们变为0.0.我知道这是一个棘手的问题,但是我不确定谁可以解决这个问题.有人可以给我一个例子.

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.

我的代码在下面

在viewdidload之前,我有这些变量

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")
     }
 }
}

推荐答案

您对异步问题是正确的.基本上,您无法在此代码之后执行

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_

原因是花括号(B)中的内容比其外部的内容(包括之后的内容,A2)发生的时间更晚.换句话说,我上面的示意图中的代码以A1,A2,B的顺序运行.但是,您取决于花括号内发生的事情,因此您需要将相关代码放在花括号内 大括号,以便它与地理编码的结果按顺序执行.

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.

当然,这也意味着周围的函数无法返回结果,因为它会在之前返回大括号.我的原理图中的代码是A1,A2,返回!仅在稍后发生B.因此很明显,您无法返回B中发生的任何事情,因为它尚未发生.

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.

这篇关于前向地理编码后如何返回坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆