在swift 4中没有调用反向地理编码

问题描述 投票:0回答:1

我正在使用swift 4编写应用程序。我在reverseGeocoding上遇到了问题。

这是我的代码:

此函数获取当前坐标。

func getCurrentCoordinates(){
    var currentLocation: CLLocation!
    var locManager = CLLocationManager()
    locManager.requestWhenInUseAuthorization()
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
        CLLocationManager.authorizationStatus() ==  .authorizedAlways){

        currentLocation = locManager.location
        self.coordGPS = currentLocation
    }
}

此函数转换为地址

func convertLatLongToAddress(latitude:Double,longitude:Double){

    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: latitude, longitude: longitude)
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]
        self.sAddress = ""

        /* Location name
        if let locationName = placeMark.location {
            if(self.bLocalize == true){
                print(locationName)
            }else{
                //self.sAddress = locationName
            }
        }*/
        // Street number
        if let number = placeMark.subThoroughfare {
            //print(number)
            if(self.bLocalize == true){
                self.TextFieldStationServiceAdresse.text = placeMark.subThoroughfare! + ", " + self.TextFieldStationServiceAdresse.text!
            }else{
                self.sAddress = self.sAddress! + ", " + number
            }
        }
        // Street address
        if let street = placeMark.thoroughfare {
            if(self.bLocalize == true){
                print(street)
                self.TextFieldStationServiceAdresse.text =  street
            }else{
                self.sAddress = self.sAddress! + ", " + street
            }
        }
        // Zip code
        if let zip = placeMark.postalCode {
            if(self.bLocalize == true){
                print(zip)
                self.TextFieldCodePostal.text = zip
            }else{
                self.sAddress = self.sAddress! + ", " + zip
            }
        }
        // City
        if let ville = placeMark.locality {
            if(self.bLocalize == true){
                self.TextFieldStationServiceCPVille.text = ville
            }else{
                self.sAddress = self.sAddress! + " " + ville
            }
        }
        // Country
        if let country = placeMark.country {
            if(self.bLocalize == true){
                print(country)
            }else{
                self.sAddress = self.sAddress! + ", " + country
            }
        }
    })

我的问题是从未调用completionHandler。

我导入了CoreLocation

你能不能告诉我为什么从未调用过完善手?位置服务不适用于此课程。

谢谢您的帮助。

swift reverse-geocoding
1个回答
0
投票

将此委托方法添加到您的班级。

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


    let userLocation :CLLocation = locations[0] as CLLocation


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


            if let placemark = placemarks?[0] {

                var streetAddress = ""

                if placemark.subThoroughfare != nil && placemark.thoroughfare != nil {

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

                   // self.address.text = streetAddress

                } 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!

                 //   self.city.text = city

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

                }

                var country = ""

                if placemark.country != nil {

                    country = placemark.country!

                    self.city.text = country

                } else {

                    print("Unable to find zip")

                }


                DispatchQueue.main.async {

     self.address.text =  String("\(streetAddress)\n\(city), \(state) \(zip)")

                }

            }

        }
    }
}

别忘了添加

您班级中的CLLocationManagerDelegate

© www.soinside.com 2019 - 2024. All rights reserved.