我有一个NSManagedObject派生类(实体),其实例在本地SQL-lite存储中持久化。该类还具有经度和纬度属性,我需要根据距特定坐标的距离来获取实体。我试图用自定义函数来使用NSPredicate,但是我找不到有关如何实现函数的文档(...如果它支持的话)。有没有人有关于如何对Core Data实体执行这种动态过滤的想法?我尝试使用NSPredicate withBlock,但它不适用于在SQL-Lite数据库上保存的对象。请帮忙。通过距动态位置的距离过滤核心数据实体
0
A
回答
3
你不能用Core Data来做到这一点。在获取实体之后,您可以使用Transient属性来建模与动态位置的距离,甚至可以根据该Transient属性对项目进行排序。但是如果它们是持久性属性,则只能从持久性存储中获取属性。
实际上,如果坐标是索引的,我发现在矩形窗口中查询点非常快。如果是未投影数据(纯粹经纬度,不是UTM或类似网格),则通过获取动态位置的经纬度,加/减搜索半径,为经度窗口调整余弦(纬度)调整窗口来构建窗口。
如果真的不够快,你可以在你的实体上存储一个geohash,它给你一个字符串,你可以做一个前缀搜索。讨论请参阅https://en.wikipedia.org/wiki/Geohash。或者你可以实现一个真正的空间索引。但是我从来没有发现核心数据性能问题,因此值得实施这两种方法之一。
0
我将数据模型中的位置存储为纬度/经度坐标。然后,我写了一些辅助扩展名来查找纬度/经度坐标的半方矩形区域并按此查询。这极大地限制了结果集,所以如果您需要按位置排序,则可以使用CLLocation距离计算器对结果对象进行排序。
我创建一个CLCircularRegion,你可以看到我的buildPredicate()
函数创建查询。
下面是我使用的代码:
斯威夫特3
extension CLLocationDegrees {
static var north: CLLocationDegrees {
return 90.0
}
static var south: CLLocationDegrees {
return -90.0
}
static var east: CLLocationDegrees {
return 180.0
}
static var west: CLLocationDegrees {
return -180.0
}
var radians: Double {
return Double.pi * self/180.0
}
}
extension CLLocationCoordinate2D {
var metersPerDegreeLatitude: CLLocationDistance {
return 111319.4907932736
}
var metersPerDegreeLongitude: CLLocationDistance {
return max(0.0, cos(self.latitude.radians) * self.metersPerDegreeLatitude)
}
}
extension CLCircularRegion {
var northernmostLatitude: CLLocationDegrees {
let longitude = self.center.latitude + self.radius/self.center.metersPerDegreeLatitude
return min(longitude, .north)
}
var southernmostLatitude: CLLocationDegrees {
let longitude = self.center.latitude - self.radius/self.center.metersPerDegreeLatitude
return max(longitude, .south)
}
var easternmostLongitude: CLLocationDegrees {
guard self.northernmostLatitude <= .north else {
return .east
}
guard self.southernmostLatitude >= .south else {
return .east
}
return min(.east, self.center.longitude + self.radius/(self.center.metersPerDegreeLongitude + 0.0001))
}
var westernmostLongitude: CLLocationDegrees {
guard self.northernmostLatitude <= .north else {
return .west
}
guard self.southernmostLatitude >= .south else {
return .west
}
return max(.west, self.center.longitude - self.radius/(self.center.metersPerDegreeLongitude + 0.0001))
}
func buildPredicate(latitudeName: String = "latitude", longitudeName: String = "longitude") -> NSPredicate {
let args = [self.southernmostLatitude, self.northernmostLatitude, self.westernmostLongitude, self.easternmostLongitude]
return NSPredicate(format: "\(latitudeName) >= %@ && \(latitudeName) <= %@ && \(longitudeName) >= %@ && \(longitudeName) <= %@", argumentArray: args)
}
}
相关问题
- 1. 核心数据中的位置通过NSFetchedResultsController按距离排序?
- 2. 核心位置距离过滤器电池寿命
- 3. ios核心位置距离过滤器如何工作?
- 4. iphone核心位置:距离过滤器它是如何工作的?
- 5. 使用iphone核心位置数据的距离计算
- 6. iOS NSPredicate通过父实体过滤核心数据
- 7. 我想通过距离蟒蛇质心的距离对数据进行分类
- 8. 核心数据通过实体分类
- 9. 地图点过滤距离
- 10. 核心数据,结果中过滤掉子实体设置
- 11. 计算距离位置的距离 - iPhone
- 12. GeoDjango距离过滤器存储在模型中的距离值
- 13. 如何通过不在关系中的实体过滤核心数据
- 14. 如何从核心位置获取gps点的距离?
- 15. 核心数据过滤
- 16. 核心数据。过滤nstableview
- 17. 用户的位置距离我目前的位置的距离
- 18. 在距离半径内过滤位置GeoPoint - Firestore
- 19. 在jQuery中为长距离短距离设置动画位置
- 20. ef核心linq过滤子实体
- 21. 如何通过elasticsearch-py中的地理距离进行过滤?
- 22. Azure的搜索距离过滤
- 23. 条件过滤/ r中subseting在直线距离数据数据
- 24. Swift MKMapItems距用户的距离位置
- 25. 对多个实体的核心数据过滤
- 26. 核心数据过滤抽象的实体
- 27. 另一个实体上的核心数据过滤器
- 28. 通过距离参考点的距离查找全部
- 29. 核心数据动态实体
- 30. 通过公路测量两个位置之间的距离
不正是我需要的... TX – Sergiob