没有从iOS中的位置管理器获取当前地址

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

我正在开发基于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.")
    }
}
ios swift google-maps cllocationmanager
1个回答
0
投票

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

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