简体   繁体   English

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

[英]Query a GeoPoint from Parse and add it to MapKit as MKAnnotation?

I am trying to query an array of PFGeoPoints stored on the Parse backend. 我正在尝试查询存储在Parse后端的PFGeoPoints数组。 I have a PFObject in Parse called "Post", with data assigned to it such as "location", "title", "message", etc. 我在Parse中有一个称为“ Post”的PFObject,并为其分配了数据,例如“ location”,“ title”,“ message”等。

everything is being sent to Parse upon posting from my app and is properly stored in the backend. 从我的应用发布内容后,所有内容都会发送到Parse,并正确存储在后端中。 I am having issues retrieving properties of the "Post" PFObject from Parse and storing them into an MKAnnotation on the map. 我在从Parse检索“ Post” PFObject的属性并将其存储到地图上的MKAnnotation中时遇到问题。

Here is my MapViewViewController: 这是我的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. 我想查询和检索由用户输入的所有信息(PFGeoPoint,标题,正文)的帖子,并将其转换为地图上的mkannotation。

Here is a photo of my Parse backend to give you a better idea: 这是我的Parse后端的照片,可为您提供更好的主意:

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

From the screenshot you provided, I assume you are querying the Post object which is a PFObject . 从您提供的屏幕快照中,我假设您正在查询Post对象,它是PFObject So you can access each post object in from the returned posts array. 因此,您可以从返回的posts数组中访问每个post对象。 Then you can use the Location property from each post object, and add annotation to your mapView. 然后,您可以使用每个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)")
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM