我有一個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. ios核心位置距離過濾器如何工作?
- 2. iphone核心位置:距離過濾器它是如何工作的?
- 3. 使用iphone核心位置數據的距離計算
- 4. iOS NSPredicate通過父實體過濾核心數據
- 5. 地圖點過濾距離
- 6. 計算距離位置的距離 - iPhone
- 7. 核心數據,結果中過濾掉子實體設置
- 8. GeoDjango距離過濾器存儲在模型中的距離值
- 9. 如何從核心位置獲取gps點的距離?
- 10. 用戶的位置距離我目前的位置的距離
- 11. 在距離半徑內過濾位置GeoPoint - Firestore
- 12. 核心數據過濾
- 13. 在jQuery中爲長距離短距離設置動畫位置
- 14. Azure的搜索距離過濾
- 15. 實體框架核心過濾器DbSet
- 16. ef核心linq過濾子實體
- 17. 條件過濾/ r中subseting在直線距離數據數據
- 18. Swift MKMapItems距用戶的距離位置
- 19. 通過距離參考點的距離查找全部
- 20. 對多個實體的核心數據過濾
- 21. 核心數據動態實體
- 22. Azure搜索 - OrderBy可過濾字段,然後通過距離
- 23. Python中的多維核心距離
- 24. 按距離過濾Python地理編碼
- 25. 過濾MySQL查詢距離計算
- 26. 如何通過Haversine計算短距離和長距離?
- 27. 如何通過指定位置來放大距離?
- 28. Sails.js通過距離地理位置座標找到
- 29. 通過json計算用戶位置和餐廳位置之間的距離(url)
- 30. Neo4j的空間,以便通過距離
不正是我需要的... TX – Sergiob