通过从 Parse 获取数据在 MKMapKit 上显示注释
Displaying Annotations on MKMapKit by taking data from Parse
所以我正在尝试使用用户当前位置创建注释。在我将我的应用程序连接到 Parse 之前,我可以将信息保存到 NSUserDefaults
并且它运行良好。现在我正在使用 Parse,我无法弄清楚如何在一个视图控制器中调用我在 Parse 中保存的坐标,以便我可以在不同的地图视图控制器中将坐标显示为注释。我当前使用的代码是
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 的坐标?
谢谢
我想 就是您要找的东西
代码:
//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()
所以我正在尝试使用用户当前位置创建注释。在我将我的应用程序连接到 Parse 之前,我可以将信息保存到 NSUserDefaults
并且它运行良好。现在我正在使用 Parse,我无法弄清楚如何在一个视图控制器中调用我在 Parse 中保存的坐标,以便我可以在不同的地图视图控制器中将坐标显示为注释。我当前使用的代码是
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 的坐标? 谢谢
我想
代码:
//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()