在Swift中将Parse GeoPoint转换为CLLocation [英] Converting Parse GeoPoint into a CLLocation in Swift
问题描述
我正在Swift中工作.
I am working in Swift.
在我的Parse后端中,我有一个名为location的键,该键具有许多geoPoint值,其中包括纬度和经度点.我想查询/获取所有这些点并将它们放置在数组中,以便随后将它们用作地图上的不同注释.
In my Parse backend I have a key called locations which has a number of geoPoint values of which are latitude and longitude points. I want to query/fetch all these points and place them into an array so I can then use them as different annotations on a map.
我在查询位置时遇到麻烦,因此无法将其用作地图的CLLocation.
I am having trouble querying the locations so that they can be used as a CLLocation for the map.
如果有人可以帮助我做到这一点,将不胜感激.
If anyone could help me do this it would be much appreciated.
推荐答案
您可以将变量创建为PFGeoPoint,并将解析后的数据放入此变量中:
You can create a variable as a PFGeoPoint and you can put your data from parse into this variable:
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")
}
}
在您需要该位置的班级中,只需添加以下行即可:
And in your class where you need the location, just add these line:
var latitude: CLLocationDegrees = descLocation.latitude
var longtitude: CLLocationDegrees = descLocation.longitude
var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)
因此您可以使用以下代码向地图添加注释:
So you can add annotation to your map using this code:
@IBOutlet var map: MKMapView!
var annotation = MKPointAnnotation()
annotation.title = "Test"
annotation.coordinate = location
map.addAnnotation(annotation)
这篇关于在Swift中将Parse GeoPoint转换为CLLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!