当用户靠近时在 Firebase 中显示场地
Show Venues in Firebase When User is near
我正在尝试让我的地图显示场地,从 Firebase 中提取,距离我的用户半英里范围内,但我还没有到那儿。有人可以帮忙吗?
这就是我现在正在做的事情:
1) 在我的 viewDidLoad 中使用我的 Firebase 数据库中的数据填充地图和 tableView:
var posts = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
// Set up Map
mapView.delegate = self
mapView.userTrackingMode = MKUserTrackingMode.follow
// Setup TableView
tableView.delegate = self
tableView.dataSource = self
//Pulls TableData for UITableView
DataService.ds.REF_VENUE.observe(.value, with: { (snapshot) in
self.posts = [] // THIS IS THE NEW LINE
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
//Populates Map with annotations.
if let locationDict = snap.value as? Dictionary<String, AnyObject> {
let lat = locationDict["LATITUDE"] as! CLLocationDegrees
let long = locationDict["LONGITUDE"] as! CLLocationDegrees
let title = locationDict["NAME"] as! String
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
_ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
annotation.title = title.capitalized
self.mapView.addAnnotation(annotation)
}
}
}
self.tableView.reloadData()
})
}
2) 然后我的tableView就正常设置了:posts[indexPath.row], pulling from class 是专门为它量身定做的。
但是,回到问题,所有位置都显示出来了。地图上到处都是场地。谁能帮忙!!!
所以我无法指导您完成代码,因为我不是 IOS 开发人员。但要实现这一点,您需要创建某种地理定位查询。
在您的情况下,您希望根据半径过滤数据。 (循环查询)
为此,您需要有一个中心(可能是用户的位置或位置搜索的结果)以及每个位置到中心的距离。
如果你有这个,那么查询将如下所示:
if (distance <= radius) {
// location is in radius
}
我刚刚为此构建了一个 javaScript 库,也许这对你有帮助(你可以在那里看到如何计算两个位置之间的距离):
https://github.com/Orlandster1998/geo-on-fire/blob/master/src/gof-utils.js#L215
或者这里的文档:
https://github.com/Orlandster1998/geo-on-fire/wiki/Basic-usage#query-by-radius
现在,如果您有大量数据要查询,您绝对应该采用更高级的方法,使用某种地理哈希。
我正在尝试让我的地图显示场地,从 Firebase 中提取,距离我的用户半英里范围内,但我还没有到那儿。有人可以帮忙吗?
这就是我现在正在做的事情:
1) 在我的 viewDidLoad 中使用我的 Firebase 数据库中的数据填充地图和 tableView:
var posts = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
// Set up Map
mapView.delegate = self
mapView.userTrackingMode = MKUserTrackingMode.follow
// Setup TableView
tableView.delegate = self
tableView.dataSource = self
//Pulls TableData for UITableView
DataService.ds.REF_VENUE.observe(.value, with: { (snapshot) in
self.posts = [] // THIS IS THE NEW LINE
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
//Populates Map with annotations.
if let locationDict = snap.value as? Dictionary<String, AnyObject> {
let lat = locationDict["LATITUDE"] as! CLLocationDegrees
let long = locationDict["LONGITUDE"] as! CLLocationDegrees
let title = locationDict["NAME"] as! String
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
_ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
annotation.title = title.capitalized
self.mapView.addAnnotation(annotation)
}
}
}
self.tableView.reloadData()
})
}
2) 然后我的tableView就正常设置了:posts[indexPath.row], pulling from class 是专门为它量身定做的。
但是,回到问题,所有位置都显示出来了。地图上到处都是场地。谁能帮忙!!!
所以我无法指导您完成代码,因为我不是 IOS 开发人员。但要实现这一点,您需要创建某种地理定位查询。
在您的情况下,您希望根据半径过滤数据。 (循环查询)
为此,您需要有一个中心(可能是用户的位置或位置搜索的结果)以及每个位置到中心的距离。
如果你有这个,那么查询将如下所示:
if (distance <= radius) {
// location is in radius
}
我刚刚为此构建了一个 javaScript 库,也许这对你有帮助(你可以在那里看到如何计算两个位置之间的距离): https://github.com/Orlandster1998/geo-on-fire/blob/master/src/gof-utils.js#L215
或者这里的文档: https://github.com/Orlandster1998/geo-on-fire/wiki/Basic-usage#query-by-radius
现在,如果您有大量数据要查询,您绝对应该采用更高级的方法,使用某种地理哈希。