How to Retrive Location from a Pfgeopoint - (Parse.Com and Swift) and Show It on the Map with Xcode 6.2

how to retrive location from a PFGeopoint - (Parse.com and Swift) and show it on the map with Xcode 6.2

this worked. Was both a matter of optionals, and a matter of variables, I was using the wrong ones:

        //MARK: (471) Crossing Positions
//*******************************************************

let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId
var radius = 100.0

println("****** this is my geoPoint from map view controller: \(myGeoPoint)")

//MARK: *** let's look for other users ***

var nearArray : [CLLocationCoordinate2D] = []

func filterByProximity() {
PFQuery(className: "Position")
.whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: radius) //(474)
.findObjectsInBackgroundWithBlock ({
objects, error in
if let proximityArray = objects as? [PFObject] {
// println("****** here the proximity matches: \(proximityArray)")
for near in proximityArray {
// println("here they are \(near)")

let position = near["where"] as? PFGeoPoint

var theirLat = position?.latitude //this is an optional
var theirLong = position?.longitude //this is an optional
var theirLocation = CLLocationCoordinate2D(latitude: theirLat!, longitude: theirLong!)

nearArray.append(theirLocation)

if nearArray.isEmpty {
println("*** ERROR! anyone close by ***")
} else
{
for person in nearArray {

let span = MKCoordinateSpanMake(2.50, 2.50)
let region = MKCoordinateRegionMake(theirLocation, span)
self.mapView.setRegion(region, animated: true)

let theirAnotation = MKPointAnnotation()
theirAnotation.setCoordinate(theirLocation)
theirAnotation.title = near["who"] as String

self.mapView.addAnnotation(theirAnotation)
}

}

}
println("****** in a radius of \(radius) there are \(nearArray.count) bikers ******")

}
})
}

filterByProximity()

Displaying Annotations on MKMapKit by taking data from Parse

I think this answer is what you're looking for

The code:

        //MARK: (471) Crossing Positions
//*******************************************************

let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId
var radius = 100.0

println("****** this is my geoPoint from map view controller: \(myGeoPoint)")

//MARK: *** let's look for other users ***

var nearArray : [CLLocationCoordinate2D] = []

func filterByProximity() {
PFQuery(className: "Position")
.whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: radius) //(474)
.findObjectsInBackgroundWithBlock ({
objects, error in
if let proximityArray = objects as? [PFObject] {
// println("****** here the proximity matches: \(proximityArray)")
for near in proximityArray {
// println("here they are \(near)")

let position = near["where"] as? PFGeoPoint

var theirLat = position?.latitude //this is an optional
var theirLong = position?.longitude //this is an optional
var theirLocation = CLLocationCoordinate2D(latitude: theirLat!, longitude: theirLong!)

nearArray.append(theirLocation)

if nearArray.isEmpty {
println("*** ERROR! anyone close by ***")
} else
{
for person in nearArray {

let span = MKCoordinateSpanMake(2.50, 2.50)
let region = MKCoordinateRegionMake(theirLocation, span)
self.mapView.setRegion(region, animated: true)

let theirAnotation = MKPointAnnotation()
theirAnotation.setCoordinate(theirLocation)
theirAnotation.title = near["who"] as String

self.mapView.addAnnotation(theirAnotation)
}

}

}
println("****** in a radius of \(radius) there are \(nearArray.count) bikers ******")

}
})
}

filterByProximity()


Related Topics



Leave a reply



Submit