【问题标题】:parse geoPointForCurrentLocationInBackground Swift sample解析 geoPointForCurrentLocationInBackground Swift 示例
【发布时间】:2015-10-29 23:02:26
【问题描述】:

在这方面找到了多个帖子,但仍然无法将其拼凑起来。

我正在使用 Parse 检索用户的当前位置。文档使它看起来很容易,但似乎缺少一些东西。 https://parse.com/docs/ios/guide#geopoints-getting-the-user-39-s-current-location

首先,我的代码:

class TableViewController: UITableViewController, CLLocationManagerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    print("Get GPS")
    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error ) -> Void in
        if error == nil {
            print("Got geoPoint") //Never reaches this
            print(geoPoint)
        } else {
            print(error ) //No error either
        }
    }
  • 我更新了 info.plist 中的 NSLocationWhenInUseUsageDescription
  • 试过模拟器和两个真机。
  • 添加了 CLLocationManagerDelegate

我试图避免让这变得比它需要的更复杂。

我还尝试了 CLLocationManager 示例,但它似乎也不起作用。

我正在使用所有内容的最新版本...从昨天开始使用 Parse!

我从未因使用我的位置的授权而受到质疑。使用 CLLocationManager 示例进行了尝试!

非常感谢一些指导/支持。

【问题讨论】:

    标签: ios parse-platform geolocation


    【解决方案1】:

    在处理 Parse 的位置更新之前,您错过了一些事情是对的。

    首先,将位置管理器属性添加到您的类。

    class TableViewController: UITableViewController, CLLocationManagerDelegate {
    
        var locationManager = CLLocationManager()
    

    设置其委托并使用它向用户请求授权:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    
        print("Get GPS")
        PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error ) -> Void in
            if error == nil {
                print("Got geoPoint") //Never reaches this
                print(geoPoint)
            } else {
                print(error ) //No error either
            }
        }
    }
    

    在您的 Info.plist 中,您需要添加一个键 NSLocationWhenInUseUsageDescription 并为其提供一个字符串值,以便在授权请求期间向用户发送消息。

    在上述更改后,授权请求和位置更新应该可以工作了。

    【讨论】:

    • 如何更新位置,geoPointgeoPointForCurrentLocationInBackground 检索?换句话说,如果用户打开应用程序并调用此方法,那么用户将移动到一个新位置,如何将 geoPoint 设置为新位置?
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 2011-12-18
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    相关资源
    最近更新 更多