如何在正向地理编码后返回坐标?

时间:2015-11-13 18:06:29

标签: swift geocoding

我正在尝试查看用户是否在某个地址的某个距离内。我已成功设法获取用户位置,并使用转发地理编码转换地址。我留下了两组坐标。我试图做一个if声明说他们是否在“距离”内,打印一些东西!

目前,当我在地标函数内打印坐标时,我得到了所需的坐标。当我打电话给他们创建eventLatitude和eventLongitude时,他们变成0.0。我知道这是一个异常的问题,但我不确定解决这个问题的对象。有人可以举个例子。

我的代码在

下面

在viewdidload之前我有这些变量

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

然后在函数内部我将这些变量设置为地标坐标

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

3 个答案:

答案 0 :(得分:1)

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

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

原因是花括号(B)内的东西发生以后比外面的东西(包括之后的东西,A2)。换句话说,我上面的示意图中的代码按照A1,A2,B的顺序运行。但是你依赖于大括号内发生的事情,所以你需要那个依赖代码 inside 卷曲大括号,以便按顺序执行地理编码的结果。

当然这也意味着周围的函数无法返回结果,因为它在之前返回甚至发生了花括号中的东西。我的原理图中的代码是A1,A2,返回!以后只有B发生。很明显,你不能归还B中发生的任何事情,因为它还没有发生。

答案 1 :(得分:0)

只需将从completionHandler获取的坐标值传递给任何其他方法,然后执行您喜欢的操作。

{
        self.placemarkLatitude = (placemark.location?.coordinate.latitude)! //THIS RETURNS A VALUE
        self.placemarkLongitude = (placemark.location?.coordinate.longitude)! //THIS RETURNS A VALUE

// After this code pass the values like,

passingTheCoordinates(placemarkLatitude, placemarkLongitude)

}


func passingTheCoordinates(latitude:CLLocationDegrees, _ longitude:CLLocationDegrees){

}

答案 2 :(得分:0)

没有足够的声誉来回答您的问题,但是我今天也遇到了同样的问题。我对您的应用程序设计了解不多,但就我而言(与您一样的地方,同样的功能,同样的问题,无法保存到变量中)。我的解决方案(可能在时间上有点不好)是将type Key [2]int m := map[Key]int{} m[Key{2, 2}] = 4 m[Key{2, 3}] = 8 fmt.Println("2^2 = ", m[Key{2, 2}]) fmt.Println("2^3 = ", m[Key{2, 3}]) (placemark.location?.coordinate.latitude)!保存为Double到CoreData中。 这就是我的实现方式。如前所述,由于我不太了解您的应用,因此取决于您的需求,因此您可能还需要其他东西。

(placemark.location?.coordinate.longitude)!

为LocationManager.swift文件提供this repo的信用