2015-11-13 28 views
0

我想查看用戶是否在一個地址的特定距離內。我已成功設法獲取用戶位置,並使用向前地理編碼轉換地址。我剩下兩套座標。我正在試着做一個if語句,說明他們是否在「遠處」,打印出一些東西!如何在向前地理編碼之後返回座標?

目前,當我打印placemark函數內的座標時,我得到了所需的座標。當我打電話給他們創建eventLatitude和eventLongitude時,它們變成0.0。我知道這是一個非常規的問題,但我不確定誰來解決這個問題。有人能給我一個例子。

我的代碼如下

我有這些變量

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

回答

0

的viewDidLoad中之前,你是正確的關於異步問題。基本上,你不能做任何這個代碼後:

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

的原因是,在大括號(B)內的事情發生比外面的東西(包括東西之後,A2)。換句話說,我上面的原理圖中的代碼按照A1,A2,B的順序運行,但是您依賴於花括號內部發生的事情,因此您需要依賴代碼在以內的花括號內,以便它執行按照地理編碼的結果進行排序。

當然這也意味着周圍的函數無法返回結果,因爲它返回之前花括號裏的東西竟然發生了。我的原理圖中的代碼是A1,A2,返回!只有後來B纔會發生。很明顯,你不能返回任何發生在B中的事情,因爲它還沒有發生。

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){ 

} 
相關問題