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

Not getting my Current Address from location Manager in iOS

我正在做一个基于 Google 地图 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.")
    }
}

如果您 运行 在模拟器上,您需要编辑您的方案,这可能就是您总是获得旧金山地址的原因,转到产品 -> 方案 -> 编辑方案,然后在 window 打开 select "Options" 选项卡,在第一个选项中,您会看到 "Default location" 设置为旧金山,只需将其更改为 "None" 并尝试再次