Местоположение обратного геокода в Swift

Мой вход - широта и долгота. Мне нужно использовать reverseGeocodeLocation функция быстрого, чтобы дать мне выход из населенного пункта. Код, который я пытался использовать,

            println(geopoint.longitude) 
            println(geopoint.latitude)
            var manager : CLLocationManager!
            var longitude :CLLocationDegrees = geopoint.longitude
            var latitude :CLLocationDegrees = geopoint.latitude

            var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
            println(location)

            CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
                println(manager.location)

                if error != nil {
                    println("Reverse geocoder failed with error" + error.localizedDescription)
                    return
                }
                if placemarks.count > 0 {
                    let pm = placemarks[0] as CLPlacemark


                    println(pm.locality)
                }


                else {
                    println("Problem with the data received from geocoder")
                }

в логах я получаю

//-122.0312186
//37.33233141
//C.CLLocationCoordinate2D
//fatal error: unexpectedly found nil while unwrapping an Optional value

Кажется, что CLLocationCoordinate2DMakeфункция не работает, что приводит к фатальной ошибке в reverseGeocodeLocation функция. Я где-нибудь испортил формат?

1 ответ

Решение

Вы никогда не меняете геокодирование местоположения, но передаете manager.location.

увидеть:CLGeocoder().reverseGeocodeLocation(manager.location, ...

Я предполагаю, что это была ошибка копирования и вставки, и что это проблема - сам код выглядит хорошо - почти;)

рабочий код

    var longitude :CLLocationDegrees = -122.0312186
    var latitude :CLLocationDegrees = 37.33233141

    var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
    println(location)

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

        if error != nil {
            println("Reverse geocoder failed with error" + error.localizedDescription)
            return
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as! CLPlacemark
            println(pm.locality)
        }
        else {
            println("Problem with the data received from geocoder")
        }
    })
Другие вопросы по тегам