【问题标题】:How do i return coordinates after forward geocoding?前向地理编码后如何返回坐标?
【发布时间】:2016-02-15 09:55:03
【问题描述】:

我正在尝试查看用户是否在某个地址的一定距离内。我已成功获取用户位置,并使用正向地理编码转换地址。我留下了两组坐标。我正在尝试做一个 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")
     }
 }
}

【问题讨论】:

    标签: swift geocoding


    【解决方案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,return!只有稍后才会发生 B。很明显,您不能返回 B 中发生的任何事情,因为它还没有发生。

    【讨论】:

      【解决方案2】:

      只需将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){
      
      }
      

      【讨论】:

        【解决方案3】:

        没有足够的声誉来回答您的问题,但我今天也遇到了同样的问题。我对您的应用程序设计了解不多,但对于我的情况(与您一样,卡在同一个地方,同样的功能,同样的问题,无法保存到变量)。我的解决方案(可能有点暂时,不好)是将(placemark.location?.coordinate.latitude)!(placemark.location?.coordinate.longitude)! 保存到CoreData 作为Double。 这就是我实现它的方式。正如我之前所说,由于我不太了解您的应用,因此取决于您的需求,您可能想要其他东西。

        LocationManager.sharedInstance.getReverseGeoCodedLocation(address: searchBar.text!, completionHandler: { (location:CLLocation?, placemark:CLPlacemark?, error:NSError?) in
        
            if error != nil {
                print((error?.localizedDescription)!)
                return
            }
        
            if placemark == nil {
                print("Location can't be fetched")
                return
            }
        
            //Saving geo code to Core Data
            newEntry.lat = (placemark?.location?.coordinate.latitude)!
            newEntry.long = (placemark?.location?.coordinate.longitude)!
        })
        

        感谢 this repo 获取 LocationManager.swift 文件

        【讨论】:

          猜你喜欢
          • 2016-09-09
          • 1970-01-01
          • 1970-01-01
          • 2021-10-22
          • 2017-12-22
          • 1970-01-01
          • 2012-05-21
          • 2017-12-04
          • 1970-01-01
          相关资源
          最近更新 更多