从Parse查询GeoPoint并将其作为MKAnnotation添加到MapKit?

我试图查询存储在Parse后端的PFGeoPoints数组。 我在Parse中有一个名为“Post”的PFObject,并为其分配了数据,如“location”,“title”,“message”等。

从我的应用程序发布后,所有内容都被发送到Parse并正确存储在后端。 我在从Parse检索“Post”PFObject的属性并将它们存储到地图上的MKAnnotation时遇到问题。

这是我的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) } 

我想查询并检索用户输入的所有信息(PFGeoPoint,标题,正文)并将其转换为地图上的mkannotation。

这是我的Parse后端的照片,为您提供更好的主意:

http://sofzh.miximages.com/ios/Screen Shot 2015-04-22 at 12.39.42 PM.png

从您提供的屏幕截图中,我假设您正在查询Post对象,这是一个PFObject 。 因此,您可以从返回的posts数组中访问每个post对象。 然后,您可以使用每个post对象的“ Location属性,并向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)") } }