![](/img/trans.png)
[英]Error converting CLLocationCoordinate2D to CLLocation in Swift
[英]Converting Parse GeoPoint into a CLLocation in Swift
我在Swift公司工作。
在我的Parse后端中,我有一個名為location的鍵,它具有許多geoPoint值,其中包括緯度和經度點。 我想查詢/獲取所有這些點並將它們放置在數組中,以便隨后將它們用作地圖上的不同注釋。
我在查詢位置時遇到麻煩,因此它們可以用作地圖的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")
}
}
在需要位置的班級中,只需添加以下行:
var latitude: CLLocationDegrees = descLocation.latitude
var longtitude: CLLocationDegrees = descLocation.longitude
var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)
因此,您可以使用以下代碼向地圖添加注釋:
@IBOutlet var map: MKMapView!
var annotation = MKPointAnnotation()
annotation.title = "Test"
annotation.coordinate = location
map.addAnnotation(annotation)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.