无法从iOS中的位置管理器获取我的当前地址

时间:2019-02-21 12:51:33

标签: ios swift google-maps cllocationmanager

我正在基于Google Maps API进行项目。我想使用Google API来获取用户的当前位置和地址。我已经使用位置管理器通过使用用户当前的纬度和经度来获取用户的地址。当我将模拟器的位置设置为旧金山时,会给我地址。这是我的代码:

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
// Here you can check whether you have allowed the permission or not.

if CLLocationManager.locationServicesEnabled()
{
    switch(CLLocationManager.authorizationStatus())
    {
    case .authorizedAlways, .authorizedWhenInUse:
        print("Authorize.")
        let latitude: CLLocationDegrees = (coordinate.latitude)
        let longitude: CLLocationDegrees = (coordinate.longitude)
        print(coordinate.latitude,coordinate.longitude)

        let location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
        CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
            if error != nil {

                let alertController = UIAlertController(title: "Current Location", message: "\(error)", preferredStyle: .alert)
                let saveAction = UIAlertAction(title: "Ok", style: .default) { [unowned self] action in
                    self.dismiss(animated: true, completion: nil)

                    return

                }

                alertController.addAction(saveAction)

                self.present(alertController, animated: true, completion: nil)
                return

            }else if let country = placemarks?.first?.country,
                let city = placemarks?.first?.locality,

                let area = placemarks?.first?.subLocality,
                let street =  placemarks?.first?.thoroughfare,
                let number =  placemarks?.first?.subThoroughfare{
                print(country)
                print(city)
                print(area)
                print(street)
                print(number)

                let locationString = "\(street ?? "")-" + "\(number ?? "") ," + "\(area ?? "")"
                print(locationString)
                 UserDefaults.standard.set(locationString, forKey: "Address")
                let alertController = UIAlertController(title: "Current Location", message: "\(locationString)", preferredStyle: .alert)
                let saveAction = UIAlertAction(title: "Ok", style: .default) { [unowned self] action in
                    self.dismiss(animated: true, completion: nil)

                }


                alertController.addAction(saveAction)

                self.present(alertController, animated: true, completion: nil)
                }



        })
        break

    case .notDetermined:
        print("Not determined.")
        break

    case .restricted:
        print("Restricted.")
        break

    case .denied:
        print("Denied.")
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在模拟器上运行,则需要编辑方案,这可能就是为什么总是获得旧金山地址的原因,请转到产品->方案->编辑方案,然后在打开的窗口中选择“选项标签,在第一个选项中,您会看到“默认位置”设置为旧金山,只需将其更改为“无”,然后重试