ios - 在 Swift 中将 Parse GeoPoint 转换为 CLLocation

标签 ios xcode swift parse-platform

我在 Swift 工作。

在我的 Parse 后端,我有一个名为 locations 的键,它有许多 geoPoint 值,其中的纬度和经度点。我想查询/获取所有这些点并将它们放入一个数组中,这样我就可以将它们用作 map 上的不同注释。

我在查询位置时遇到问题,无法将它们用作 map 的 CLLocation。

如果有人能帮助我做到这一点,我将不胜感激。

最佳答案

您可以创建一个变量作为 PFGeoPoint,您可以将解析的数据放入此变量中:

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

    }

在您需要位置的类(class)中,只需添加以下行:

    var latitude: CLLocationDegrees = descLocation.latitude
    var longtitude: CLLocationDegrees = descLocation.longitude

    var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)

因此您可以使用以下代码向您的 map 添加注释:

    @IBOutlet var map: MKMapView!
    var annotation = MKPointAnnotation()
    annotation.title = "Test"
    annotation.coordinate = location
    map.addAnnotation(annotation)

关于ios - 在 Swift 中将 Parse GeoPoint 转换为 CLLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679432/

相关文章:

android - 具有 Firebase 身份验证用户的 Firebase 数据库数据结构

xcode - 新解析强制 "optional",如何查询 PFUser?

objective-c - 如何在Xcode4中启用 "Fix It"功能?

ios - 在 UITableViewCell 中显示多个方框

ios - "self"在 Swift 中有什么用?

ios - 使用 Swift for iOS 以编程方式创建和存储 PDF 文档

ios - ARKit – Session 的配置检查

ios - Facebook iOS SDK : Tagging friends?

ios - 随机运动/湍流 - SWIFT

iphone - 在运行时检查 "Release Build"