2016-10-10 14 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) 
    } 
} 

実行は右になり、何も実行時エラーが発生していません。

しかし、呼び出されている最後の行は、私はまた、二重locaがnilでないことを確認

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

です。

なぜ完了ハンドラが呼び出されていないのですか?

+0

geoCoderはどこで定義されていますか?あなたのView Controllerのメンバーですか? – xpereta

答えて

3

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

完了ハンドラが質問コードにあります。これは、後続のクロージャの構文にすぎません。 2つの書式が正しい。 – xpereta

+2

何らかの理由で、@ Kampaiによって投稿されたコードは私の間では動作しません。私はこの答えを受け入れましたが、私のアプローチで何が間違っているかを知ることはうれしいでしょう。おそらく、それは迅速な3 – 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) 

} 
関連する問題