我在 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/