【问题标题】:Converting a PFGeoPoint Lat and Long from Parse into a CLLocation Lat and Long in swift快速将 PFGeoPoint Lat 和 Long 从 Parse 转换为 CLLocation Lat 和 Long
【发布时间】:2015-01-03 16:30:29
【问题描述】:

我正在尝试从我的解析后端查询坐标数组(纬度和经度)。然后将它们用作地图上的注释。虽然一旦我从 parse 中查询过,我不确定如何将坐标转换为 CLLocation。

用于查询的代码:

var usersLocations = [Double]()

 override func viewDidLoad() {
        super.viewDidLoad()

        PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint: PFGeoPoint!, error: NSError!) -> Void in

            var query = PFUser.query()
            query.whereKey("location", nearGeoPoint: geopoint, withinMiles: 1)
            query.limit = 10
            var users = query.findObjects()

            for user in users {

                self.usersLocations.append(user["location"] as Double)
            }


            self.currentUsersPoint()

        }
    }

然后用于尝试将坐标数组放入 CLLocation 的代码。

var latitude:CLLocationDegrees = usersLocation.coordinate.latitude

var longitude:CLLocationDegrees = usersLocation.coordinate.longitude

我知道这有点混乱,我真的只是不确定,因为我对 swift 和编程还是新手,所以很难解决这一切。如果有人可以提供帮助,将不胜感激。

【问题讨论】:

    标签: ios xcode swift parse-platform cllocation


    【解决方案1】:

    假设usersLocation.coordinatePFGeoPoint,您可以使用我为我的一个项目创建的以下扩展:

    extension PFGeoPoint {
      public var cllocation: CLLocation {
        get {
          return CLLocation(latitude: latitude, longitude: longitude)
        }
      }
    }
    

    然后,以下应该可以工作(location 将是一个CLLocation 对象)。

    var location = usersLocation.coordinate.cllocation
    

    【讨论】:

      【解决方案2】:

      去做吧!

      var local : PFGeoPoint = PFGeoPoint()
      
      if let object = currentObject {
      
          local = object["PFGeoPoint Col Name"] as! PFGeoPoint
      
      }
      
      let getLatParse : CLLocationDegrees = local.latitude
      let getLongParse : CLLocationDegrees = local.longitude
      
      let localParse = CLLocation(latitude: getLatParse, longitude: getLongParse)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-06
        • 2012-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多