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

标签 ios swift parse-platform location mapkit

我正在尝试查询存储在 Parse 后端的 PFGeoPoints 数组。我在 Parse 中有一个名为“Post”的 PFObject,并为其分配了“位置”、“标题”、“消息”等数据。

从我的应用程序发布后,所有内容都会发送到 Parse,并正确存储在后端。我在从 Parse 检索“Post”PFObject 的属性并将其存储到 map 上的 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、标题、正文),并将其转换为 map 上的 mkannotation。

这是我的 Parse 后端的照片,可以让您更好地了解:

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

最佳答案

根据您提供的屏幕截图,我假设您正在查询 Post 对象,它是一个 PFObject。因此,您可以从返回的 posts 数组中访问每个 post 对象。然后,您可以使用每个 post 对象中的 Location 属性,并向您的 map View 添加注释。

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)")
        }
    }

关于ios - 从 Parse 查询 GeoPoint 并将其作为 MKAnnotation 添加到 MapKit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807428/

相关文章:

ios - 添加 UITextField 会卡住应用程序

swift - 在 Swift 中使用 Restkit

ios - 表格 View 上下文菜单 : custom animation from row to a preview view controller

ios - Swift:只允许用户对 TableViewCell 中的项目投票一次

ios - NSXMLParser:非 ASCII 字符的意外结果

ios - UIPanGestureRecognizer 获取滑动距离(以像素为单位)

ios - Objective-C 中的常量和 "duplicate symbol"链接器错误

ios - 为什么调用 super 是此方法的最后一件事而不是第一件事?

ios - 解析 : Does not query saved objects in local datastore

javascript - 查找具有字段值的对象(这是一个指针)