Reputation:
I am working in Swift.
In my Parse backend I have a key called locations which has a number of geoPoint values of which are latitude and longitude points. I want to query/fetch all these points and place them into an array so I can then use them as different annotations on a map.
I am having trouble querying the locations so that they can be used as a CLLocation for the map.
If anyone could help me do this it would be much appreciated.
Upvotes: 1
Views: 4316
Reputation: 1058
You can create a variable as a PFGeoPoint and you can put your data from parse into this variable:
var descLocation: PFGeoPoint = PFGeoPoint()
var innerP1 = NSPredicate(format: "ObjectID = %@", objectID)
var innerQ1:PFQuery = PFQuery(className: "Test", predicate: innerP1)
var query = PFQuery.orQueryWithSubqueries([innerQ1])
query.addAscendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
descLocation = object["GeoPoint"] as PFGeoPoint
}
} else {
println("Error")
}
}
And in your class where you need the location, just add these line:
var latitude: CLLocationDegrees = descLocation.latitude
var longtitude: CLLocationDegrees = descLocation.longitude
var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)
So you can add annotation to your map using this code:
@IBOutlet var map: MKMapView!
var annotation = MKPointAnnotation()
annotation.title = "Test"
annotation.coordinate = location
map.addAnnotation(annotation)
Upvotes: 4