所以我尝试使用用户当前位置创建注释。在将我的应用程序连接到 Parse 之前,我可以将信息保存到 NSUserDefaults 中,并且它工作得很好。现在我正在使用 Parse,我无法弄清楚如何在一个 View Controller 中调用在 Parse 中保存的坐标,以便我可以在不同的 map View Controller 中将坐标显示为注释。我当前使用的代码是
let userLoc = NSUserDefaults.StandardUserDefaults().dictionaryForKey("Location") as! [String : NSNumber]
Let userLat = userLoc["lat"]
Let userLng = userLoc["lng"]
var latitude:CLLocationDegrees = userLat as! CLLocationDegrees
var longitude:CLLocationDegrees = userLng as! CLLocationDegrees
那么我该如何修改这段代码才能显示我保存到 Parse 的坐标呢? 谢谢
最佳答案
我认为this answer就是您要找的
代码:
//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()
关于swift - 通过从 Parse 获取数据在 MKMapKit 上显示注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085968/