如何从PFGeopoint(Parse.com和Swift)检索位置,并使用Xcode 6.2在地图上显示

这里有一些答案,但涉及到不同的问题,都在客观的c。 我用这个保存在一个用户的parsing类“职位”职位:

var locationManager = CLLocationManager() var lat = locationManager.location.coordinate.latitude var lon = locationManager.location.coordinate.longitude let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon) let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId println("****** this is my geoPoint: \(myGeoPoint)") func sendPosition(userOfPosition: User) { let takePosition = PFObject(className: "Position") takePosition.setObject(myParseId, forKey: "who") //who takePosition.setObject(myGeoPoint, forKey: "where") takePosition.saveInBackgroundWithBlock(nil) } sendPosition(currentUser()!) 

所以这是我的结果: 在这里输入图像说明

那么我想在地图上显示他们,但是怎么样? 不明白如何从“where”列中取回经纬度,下面的代码不起作用:

  import UIKit import MapKit class MapViewController: UIViewController { @IBOutlet weak var mapView: MKMapView! var locationManager : CLLocationManager! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. var locationManager = CLLocationManager() var lat = locationManager.location.coordinate.latitude var lon = locationManager.location.coordinate.longitude let location = CLLocationCoordinate2D(latitude: lat, longitude: lon) let span = MKCoordinateSpanMake(0.05, 0.05) let region = MKCoordinateRegionMake(location, span) mapView.setRegion(region, animated: true) let anotation = MKPointAnnotation() anotation.setCoordinate(location) anotation.title = "my title" anotation.subtitle = " my subtitle" mapView.addAnnotation(anotation) println("****** Welcome in MapViewController") //MARK: (471) Crossing Positions //******************************************************* let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon) let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId println("****** this is my geoPoint from map view controller: \(myGeoPoint)") // // var inspector = PFQuery(className:"GameScore") // inspector.saveInBackgroundWithBlock { // (success: Bool, error: NSError?) -> Void in // if (success) { // // The object has been saved. // var the = inspector.objectId // } else { // // There was a problem, check error.description // } // } // // // func filterByProximity() { PFQuery(className: "Position") .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: 500.0) //(474) .findObjectsInBackgroundWithBlock ({ objects, error in if let proximityArray = objects as? [PFObject] { println("****** here the proximity matches: \(proximityArray)") for near in proximityArray { println("here they are \(near)") if let position = near["where"] as! PFGeoPoint { let theirLat = position.latituide let theirLon = position.longitude } let theirLat = near["where"].latitude as Double let theirlong = near["where"].longitude as Double let location = CLLocationCoordinate2DMake(theirLat, theirlong) let span = MKCoordinateSpanMake(0.05, 0.05) let region = MKCoordinateRegionMake(location, span) self.mapView.setRegion(region, animated: true) let theirAnotation = MKPointAnnotation() theirAnotation.setCoordinate(location) self.mapView.addAnnotation(anotation) } } }) } filterByProximity() // //update my position // // func exists() { // PFQuery(className:"Position") // .whereKey("who", containsString: myParseId) // .findObjectsInBackgroundWithBlock({ // thisObject, error in // if let result = thisObject as? [PFObject] { // println("here the result: \(result)") // // let gotTheId = result[0].objectId // println("ecco l'id singolo \(gotTheId)") // // //******** update function ******** // var query = PFQuery(className:"Position") // query.getObjectInBackgroundWithId(gotTheId) { // (usingObject: PFObject?, error: NSError?) -> Void in // if error != nil { // println(error) // } else if let objectToupdate = usingObject { // println("else occurred") // objectToupdate["where"] = myGeoPoint // println("position should be updated") // objectToupdate.saveInBackgroundWithBlock(nil) // println("position should be saved") // // } // } // //******** end update function ******** // } // }) // } // // exists() //******************************************************* } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

在第一个答案后更新

试图检查可选项,但有这样的消息: 在这里输入图像说明 之后不倒下来双: 在这里输入图像说明在这里输入图像说明

'where'是一个PFGeoPoint,你可以调用它们的经度和纬度

尝试这样的事情 – 我不是百分之百确定的Swift语法

 if(geoPoint) { if(geoPoint!.latitude) { let latitude = geoPoint!.latitude as double; } } 

这工作。 这既是一个可选项,也是一个可变因素,我使用的是错误的:

  //MARK: (471) Crossing Positions //******************************************************* let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon) let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId var radius = 100.0 println("****** this is my geoPoint from map view controller: \(myGeoPoint)") //MARK: *** let's look for other users *** var nearArray : [CLLocationCoordinate2D] = [] func filterByProximity() { PFQuery(className: "Position") .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: radius) //(474) .findObjectsInBackgroundWithBlock ({ objects, error in if let proximityArray = objects as? [PFObject] { // println("****** here the proximity matches: \(proximityArray)") for near in proximityArray { // println("here they are \(near)") let position = near["where"] as? PFGeoPoint var theirLat = position?.latitude //this is an optional var theirLong = position?.longitude //this is an optional var theirLocation = CLLocationCoordinate2D(latitude: theirLat!, longitude: theirLong!) nearArray.append(theirLocation) if nearArray.isEmpty { println("*** ERROR! anyone close by ***") } else { for person in nearArray { let span = MKCoordinateSpanMake(2.50, 2.50) let region = MKCoordinateRegionMake(theirLocation, span) self.mapView.setRegion(region, animated: true) let theirAnotation = MKPointAnnotation() theirAnotation.setCoordinate(theirLocation) theirAnotation.title = near["who"] as String self.mapView.addAnnotation(theirAnotation) } } } println("****** in a radius of \(radius) there are \(nearArray.count) bikers ******") } }) } filterByProximity()