2016-10-10 39 views
1

這是我的代碼此代碼段的斯威夫特3:reverseGeocodeLocation不會在其完成處理

if loc.latitude != 0.0 && loc.longitude != 0.0 { 
    let loca = CLLocation(latitude: loc.latitude, longitude: loc.longitude) 
    geoCoder.reverseGeocodeLocation(loca) { (placemarks, error) in // this is the last line that is being called 
     var placemark : CLPlacemark! 
     placemark = placemarks?[0] 
     city = (placemark.addressDictionary?["City"] as! String) 
    } 
} 

執行我的應用程序去正確的,沒有發生運行時錯誤。

然而,這是被稱爲最後一行是

geoCoder.reverseGeocodeLocation(loca){(placemarks, error)

我還雙檢查了loca不爲零。

爲什麼完成處理程序沒有被調用?

+0

geoCoder在哪裏定義?它是你的視圖控制器的成員嗎? – xpereta

回答

3

在Closure中使用completionHandler

檢查下面的例子:

geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in 

      // Place details 
      var placeMark: CLPlacemark! 
      placeMark = placemarks?[0] 

      // Address dictionary 
      print(placeMark.addressDictionary, terminator: "") 

      // Location name 
      if let locationName = placeMark.addressDictionary!["Name"] as? NSString { 
       print(locationName, terminator: "") 
      } 

      // Street address 
      if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString { 
       print(street, terminator: "") 
      } 

      // City 
      if let city = placeMark.addressDictionary!["City"] as? NSString { 
       print(city, terminator: "") 
      } 

      // Zip code 
      if let zip = placeMark.addressDictionary!["ZIP"] as? NSString { 
       print(zip, terminator: "") 
      } 

      // Country 
      if let country = placeMark.addressDictionary!["Country"] as? NSString { 
       print(country, terminator: "") 
      } 

     }) 
+3

這是一個怎樣的答案?問題中的代碼確實有一個完成處理程序。 – xpereta

+4

請注意,完成處理程序在問題代碼中,它只是在尾部閉包語法中。這兩種形式是正確的。 – xpereta

+2

出於某種原因@Kampai發佈的代碼工作,而我的工作沒有。我接受了這個答案,但是我很高興知道我的方法有什麼問題 - 也許這是一個很快的問題 – DCDC

0

我遇到了這個問題,它好像有關於回答一些混亂。以下是使用Swift 3獲取位置信息的廣泛途徑,僅限於任何未來的讀者。

此功能碼使用didUpdateLocations功能和reverseGeocodeLocation()將位置轉換爲人類可讀地址。它還將地圖視圖設置爲當前用戶位置。這當然假設你已經設置了你的位置管理器對象。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    // Get first location item returned from locations array 
    let userLocation = locations[0] 

    // Convert location into object with human readable address components 
    CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in 

     // Check for errors 
     if error != nil { 

      print(error ?? "Unknown Error") 

     } else { 

      // Get the first placemark from the placemarks array. 
      // This is your address object 
      if let placemark = placemarks?[0] { 

       // Create an empty string for street address 
       var streetAddress = "" 

       // Check that values aren't nil, then add them to empty string 
       // "subThoroughfare" is building number, "thoroughfare" is street 
       if placemark.subThoroughfare != nil && placemark.thoroughfare != nil { 

        streetAddress = placemark.subThoroughfare! + " " + placemark.thoroughfare! 

       } else { 

        print("Unable to find street address") 

       } 

       // Same as above, but for city 
       var city = "" 

       // locality gives you the city name 
       if placemark.locality != nil { 

        city = placemark.locality! 

       } else { 

        print("Unable to find city") 

       } 

       // Do the same for state 
       var state = "" 

       // administrativeArea gives you the state 
       if placemark.administrativeArea != nil { 

        state = placemark.administrativeArea! 

       } else { 

        print("Unable to find state") 

       } 

       // And finally the postal code (zip code) 
       var zip = "" 

       if placemark.postalCode != nil { 

        zip = placemark.postalCode! 

       } else { 

        print("Unable to find zip") 

       } 

       print("\(streetAddress)\n\(city), \(state) \(zip)") 

      } 

     } 

    } 

    // Create a coordinate based on users location latitude and longitude 
    let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, 
              longitude: userLocation.coordinate.longitude) 

    // Set the span (zoom) of the map view. Smaller number zooms in closer 
    let span = MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001) 

    // Set the region, using your coordinates & span objects 
    let region = MKCoordinateRegion(center: coordinate, span: span) 

    // Set your map object's region to the region you just defined 
    map.setRegion(region, animated: true) 

}