【问题标题】:Filter only nearest locations from DB仅从数据库中过滤最近的位置
【发布时间】:2017-09-19 09:05:06
【问题描述】:
我有一个 Firebase 数据库,用户在使用应用程序时正在更新他的位置(纬度和经度)。同时,用户可以看到其他用户的所有位置,这些用户正在以相同的方式更新他们的位置。
我想显示与用户 X 距离内的所有用户位置。是否有任何算法或其他过滤方法?现在,如果用户在美国,他将看到所有位置事件,他可以看到来自法国的活跃用户的位置。
我正在使用 MapKit 和 CoreLocation
【问题讨论】:
标签:
swift
firebase
firebase-realtime-database
core-location
coordinate
【解决方案1】:
您可以在CLLocation 对象上使用distance,并打印出您想要的距离内的对象。
类似这样的:
let userLocation = CLLocation(latitude: userLat, longitude: userLong)
let anotherLocation = CLLocation(latitude: anotherLat, longitude: anotherLong)
let distance = anotherLocation.distance(from: userLocation)
if distance < desiredDistance {
// within range
}