user4184036
user4184036

Reputation:

Converting a PFGeoPoint Lat and Long from Parse into a CLLocation Lat and Long in swift

I am trying to query an array of coordinates (lat and long) from my parse back end. and then use them as annotations on a map. Although once I have queried from parse I am not sure how to convert the coordinates into a CLLocation.

The code used for query:

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()

        }
    }

And then the code used to try place the array of coordinates into CLLocation.

var latitude:CLLocationDegrees = usersLocation.coordinate.latitude

var longitude:CLLocationDegrees = usersLocation.coordinate.longitude

I'm aware this is a bit messy and I'm really just not sure as I am still new to swift and programming so struggling to work this all out. If anybody can help in anyway it would be appreciated.

Upvotes: 2

Views: 2068

Answers (2)

Raphael Albertini
Raphael Albertini

Reputation: 13

Do it!

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)

Upvotes: 0

rickerbh
rickerbh

Reputation: 9913

Assuming that usersLocation.coordinate is a PFGeoPoint, you could use the following extension I created for one of my projects:

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

Then, the following should work (location will be a CLLocation object).

var location = usersLocation.coordinate.cllocation

Upvotes: 2

Related Questions