【问题标题】:swift user's location issue when trying to get current location尝试获取当前位置时快速用户的位置问题
【发布时间】:2014-09-28 02:03:46
【问题描述】:

我正在做一个使用当前用户位置及其朋友议程的应用程序。 我写了一点代码来显示你的位置

    var loc :CLLocationCoordinate2D = self.mapView.userLocation.coordinate
    var region = MKCoordinateRegionMakeWithDistance(loc, 5, 5)
    self.mapView.setRegion(region, animated: true)

此外,我收到错误消息:

        Trying to start MapKit location updates without prompting for location               authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

经过一些研究,我不明白应该做些什么来改善我的位置[我从 debug->simulateLocation 改变了,但没有任何改变]

您能否解释一下我必须记住的事项以及改进我的应用程序需要做些什么?

[edit1] 模拟器有没有可能是这样,而手机不应该是这样?

【问题讨论】:

  • 您是否将mapView 的showsUserLocation 属性设置为YES?
  • 是的,我得到同样的错误和同样的,在海洋的中间位置[实际上坐标是 0.0 和 0.0]

标签: ios swift cllocationmanager


【解决方案1】:

从 iOS 8 开始,您必须明确请求用户许可:

    var systemVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)
        if NSProcessInfo.processInfo().isOperatingSystemAtLeastVersion(systemVersion) {
        locationManager.requestAlwaysAuthorization()
    }

    // get current location
    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
        locationManager.startUpdatingLocation()
    }

此外,您必须在 Info.plist 中明确声明权限问题

NSLocationAlwaysUsageDescription

【讨论】:

    【解决方案2】:

    我通过谷歌搜索一下发现了这个问题:

    http://rshankar.com/get-your-current-address-in-swift/

    其中一个步骤是设置->常规->...使用位置->始终,这应该可以解决它,您将不会收到该错误。

    此外,制作本教程的人提到您应该输入一个位置,以确保它可以正常工作,瞧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      相关资源
      最近更新 更多