2

I am trying to query an array of PFGeoPoints stored on the Parse backend. I have a PFObject in Parse called "Post", with data assigned to it such as "location", "title", "message", etc.

everything is being sent to Parse upon posting from my app and is properly stored in the backend. I am having issues retrieving properties of the "Post" PFObject from Parse and storing them into an MKAnnotation on the map.

Here is my MapViewViewController:

import UIKit
import Parse
import CoreLocation
import MapKit

class MapViewViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet var mapView: MKMapView!

var MapViewLocationManager:CLLocationManager! = CLLocationManager()
var currentLoc: PFGeoPoint! = PFGeoPoint()

var postTitle: String!
var postBody: String!


override func viewDidLoad() {
    super.viewDidLoad()
    mapView.showsUserLocation = true
    mapView.delegate = self
    MapViewLocationManager.delegate = self
    MapViewLocationManager.startUpdatingLocation()
    mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}

override func viewDidAppear(animated: Bool) {
    var annotationQuery = PFQuery(className: "Post")
    currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
    annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
    annotationQuery.findObjectsInBackgroundWithBlock {
        (points, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successful query for annotations")
            // Do something with the found objects
            //THIS IS WHERE I GET LOST WITH THE PARSE QUERY/ADDING ANNOTATION TO MAP


            //THE FOLLOWING CODE PRINTS THE CORRECT POSTCOUNT STORED IN PARSE
            println("total posts: \(points?.count)")

        } else {
            // Log details of the failure
            println("Error: \(error)")
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func logoutButton(sender: UIBarButtonItem) {
    PFUser.logOut()
    dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func newPostButton(sender: AnyObject) {
    performSegueWithIdentifier("mainToNewPost", sender: self)
}

I would like to query and retrieve the Post with all of its info entered by the user (PFGeoPoint, title, body) and turn it into an mkannotation on the map.

Here is a photo of my Parse backend to give you a better idea:

http://i1249.photobucket.com/albums/hh512/zachkhan3/Screen%20Shot%202015-04-22%20at%2012.39.42%20PM.png

ztan
  • 6,861
  • 2
  • 24
  • 44
Zach
  • 180
  • 1
  • 2
  • 12
  • Well, it looks like you have the posts already, and you just need to create the annotations – Wain Apr 22 '15 at 19:57
  • I see, i guess im getting confused while pulling the points from parse and turning them into annotations. – Zach Apr 22 '15 at 19:59
  • Well, `println("total posts: \(allPosts?.count)")` isn't clear because `allPosts` isn't defined anywhere, but you need to iterate the `points` and create the annotations from them – Wain Apr 22 '15 at 20:21
  • Oops! 'allPosts' should be 'points' - FIXED. Im having a hard time figuring out exactly how to "iterate" the points and create annotations from them. I would like to set the title, body and location value to each pin. – Zach Apr 22 '15 at 20:24
  • Start with a simple for loop and print the values for each point, then create the annotation in the loop and set the values, add the annotations to the map (best to create an array or annotations, but one step at a time) – Wain Apr 22 '15 at 20:27

1 Answers1

4

From the screenshot you provided, I assume you are querying the Post object which is a PFObject. So you can access each post object in from the returned posts array. Then you can use the Location property from each post object, and add annotation to your mapView.

annotationQuery.findObjectsInBackgroundWithBlock {
        (posts, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successful query for annotations")
            let myPosts = posts as [PFObject]

            for post in myPosts {
                let point = post["Location"] as PFGeoPoint
                let annotation = MKPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                self.mapView.addAnnotation(annotation)
            }
        } else {
            // Log details of the failure
            println("Error: \(error)")
        }
    }
ztan
  • 6,861
  • 2
  • 24
  • 44