在Swift中将Parse GeoPoint转换为CLLocation

我在Swift工作。

在我的Parse后端,我有一个叫做位置的键,它有一些geoPoint值,它们是纬度和经度点。 我想查询/获取所有这些点,并将它们放入一个数组,以便我可以在地图上使用它们作为不同的注释。

我无法查询位置,以便它们可以用作地图的CLLocation。

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

你可以创build一个variables为PFGeoPoint,你可以把你的数据parsing到这个variables中:

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) 

因此,您可以使用以下代码将注释添加到地图中:

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