仅执行一次Geocoder

时间:2019-05-22 14:57:14

标签: ios swift xcode

尝试在 Geocoder 获得地址时停止更新,并将最后一个位置保留为变量。发生的事情是,当我到达位置时,我会自动更新 tableview ,但是如果 Geocoder 一直工作,那么它就不会停止更新tableview,我需要它来更新仅更新一次即可获取地址。

这是代码:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]

    let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

    //print(location.altitude)
    //print(location.speed)

    CLGeocoder().reverseGeocodeLocation(location) { (placemark, error) in
        if error != nil
        {
            print ("THERE WAS AN ERROR")
            self.placelocality = "null"
            self.placecountry = "null"
            print("\(self.placelocality) y \(self.placecountry)")
        }
        else
        {
            if let place = placemark?[0]
            {
                if let checker = place.subThoroughfare
                {
                    self.placelocality = place.locality!
                    self.placecountry = place.country!
                    //print("\(place.locality!) \n \(place.country!)")
                    if self.placelocality != "null" && self.placecountry != "null" {
                        self.ps = self.placecountry
                        self.cdad = self.placelocality
                        self.lblubicacion.text = "Selecciona un servicio en \(self.placelocality), \(self.placecountry)."

                        self.updatesearchbar()

                    } else {
                        self.ps = "País";
                        self.cdad = "Ciudad";
                    }
                }
            }
        }
    }
}

希望您能帮助我,非常感谢

0 个答案:

没有答案