【问题标题】:Obtaining user location not functioning in Xcode获取用户位置在 Xcode 中不起作用
【发布时间】:2018-10-26 05:30:33
【问题描述】:

晚上好,我正在尝试让用户在地图上定位自己。当我运行该应用程序并访问我的谷歌地图视图时,我收到错误消息“此应用程序已尝试在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含一个“NSLocationWhenInUseUsageDescription”键和一个字符串值解释向用户展示应用程序如何使用这些数据”,并且地图被设置为默认状态。

我放了

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs your current location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs your current location</string>

在我的 info.plist 中,但它告诉我需要输入 NSLocationWhenInUseUsageDescription 才能访问位置。我已经把它放进去,它仍然不会显示用户位置!我是遗漏了什么还是这是一个错误?在我更新到最新版本的 Xcode 之前,它对我来说效果很好。

我启用谷歌地图的代码如下,

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locationManager.delegate = self
    locationManager.stopUpdatingLocation()
    let location = locations.last
    let lat = (location?.coordinate.latitude)!
    let long = (location?.coordinate.longitude)!
    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)

    self.myMapView.animate(to: camera)

    showPartyMarkers(lat: lat, long: long)
}


func initGoogleMaps() {
    let camera = GMSCameraPosition.camera(withLatitude: 40.014281, longitude: -83.030914, zoom: 17.0)
    self.myMapView.camera = camera
    self.myMapView.delegate = self
    self.myMapView.isMyLocationEnabled = true
}
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    let lat = place.coordinate.latitude
    let long = place.coordinate.longitude

    showPartyMarkers(lat: lat, long: long)

    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)
    myMapView.camera = camera
    txtFieldSearch.text=place.formattedAddress

【问题讨论】:

  • 你启动 CoreLocation 了吗?
  • @ktrkathi 我在我的导入中调用了它,但我从来没有为它编程过任何东西。我会提问以显示我的谷歌地图代码。
  • 如果您使用模拟器。启用调试位置
  • @ktrkathir 我更新了上面的代码以展示我如何实现 gmaps

标签: swift xcode google-maps userlocation


【解决方案1】:

在新的 InfoPlist.strings 文件中添加新记录。

<key>NSLocationAlwaysUsageDescription</key>
<string>app need your location for finding a place</string>

add  lines in viewdidload

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBest

从模拟器中删除您的应用并再次运行。

【讨论】:

  • 我在手机上运行它,这样我就可以访问真实的用户位置。我删了重装还是一样的问题。
  • 检查位置权限设置。
  • 我将其更改为始终,但我仍然收到消息。我的应用不会发送通知以允许或拒绝它。
【解决方案2】:

在 plist 中添加以下键。

Privacy - Location Always and When In Use Usage Description

Privacy - Location When In Use Usage Description

编辑

我没有看到 locationManager 的初始化。

使用以下代码进行初始化:

   /// CLLocationManager object
   var locationManager: CLLocationManager = {
        let manager = CLLocationManager()
        manager.desiredAccuracy = kCLLocationAccuracyBest
        return manager
    }()

Init 开始更新位置后,在 viewDidload 中添加以下行

locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

【讨论】:

  • @Toseefkhiliji 我还有什么遗漏吗?/
  • @zachwilcox:更新答案,从模拟器中删除您的应用并再次运行。
  • 我的代码中已经有这些行。我仍然遇到同样的错误。
  • 我尝试了很多修复,但它仍然说我需要更新我的信息 plist。我已经插入了上述行,但仍然遇到同样的错误。
  • 更新答案,请检查设备定位服务权限。
【解决方案3】:

我在重新发布问题后找到了答案。如果有人也遇到此问题,您要做的是单击您的项目目标构建设置,单击信息(构建设置左侧),并且您要确保自定义 iOS 目标属性具有与常规相同的信息p 列表。如果没有,只需单击加号,然后将它们添加进去。这应该可以解决问题,因为它与我的有关。我希望你们都觉得这很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2016-12-15
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多