RealmGeoQueries通过Realm Cocoa简化了空间查询。在没有官方功能的条件下,这个库提供了进行邻近搜索的可能性。模型类中无需包含Geohash或其他类型的索引,因为它只需要纬度和经度属性。
如何开始
使用CocoaPods安装
platform :ios, '9.0'
pod "RealmGeoQueries"
使用Carthage安装
将 mhergon/RealmGeoQueries
项目添加到您的 Cartfile
.
github "mhergon/RealmGeoQueries"
从 Carthage/Build 到您的 Xcode 项目的“通用”设置中的“链接框架和库”部分拖动 GeoQueries.framework
、RealmSwift.framework
和 Realm.framework
。
仅限 iOS/tvOS/watchOS:在您应用的目标的“构建过程”设置标签页中,点击“+”图标并选择“新建运行脚本阶段”。创建一个包含以下内容的运行脚本
/usr/local/bin/carthage copy-frameworks
并将您想使用的框架路径添加到“输入文件”下,例如。
$(SRCROOT)/Carthage/Build/iOS/GeoQueries.framework
$(SRCROOT)/Carthage/Build/iOS/Realm.framework
$(SRCROOT)/Carthage/Build/iOS/RealmSwift.framework
手动安装
下载(右键点击)并将其添加到您的项目中。
要求
版本 | 语言 | 最低iOS目标 |
---|---|---|
1.3 | Swift 4.x / Realm 3.x | iOS 9 |
1.2 | Swift 3.0 / Realm 2.x | iOS 9 |
1.1 | Swift 2.x / Realm 2.x | iOS 8 |
使用方法
首先,导入模块;
import GeoQueries
模型必须有经纬度键,分别为 "lat" 和 "lng"。您可以使用其他的属性名(使用 "latitudeKey" 和 "longitudeKey" 参数)。
使用 MapView MKCoordinateRegion 进行搜索;
let results = try! Realm()
.findInRegion(type: YourModelClass.self, region: mapView.region)
围绕中心点搜索半径(单位:米);
let results = try! Realm()
.findNearby(YourModeltype: Class.self, origin: mapView.centerCoordinate, radius: 500, sortAscending: nil)
使用半径过滤 Realm 结果(单位:米);
let results = try! Realm()
.objects(YourModelClass.self)
.filter("type", "restaurant")
.filterGeoRadius(center: mapView.centerCoordinate, radius: 500, sortAscending: nil)
查看 GeoQueries.swift
以获取更多选项。
联系方式
许可证
Apache License 2.0 许可。
版权所有 2017 Marc Hervera。