iPhone地理半径搜索;使用 Core Data 还是 SQLite?
我正在编写一个具有 ca 的应用程序。数据库中有 7000 家欧洲餐厅,我想显示距离用户最近的餐厅列表,例如所有距离用户 5 公里半径内的餐厅。
我可以在服务器上进行搜索,但它需要互联网。是否可以将数据保存在iPhone上并查询数据库?我在核心数据或 iPhone 的 SQLite 中找不到类似的参考资料。
我真的需要用毕达哥拉斯之类的东西自己编程并计算每个查询到每个餐厅的距离吗?或者还有其他办法吗?
[更新] 我想像在服务器上那样使用它(但要在 iPhone 上进行):SELECT * FROMrestaurant WHERE ST_Distance_Sphere(geo, ST_GeomFromText(POINT(55.98767 57.12345), -1) ) < 5000
我希望用户即使没有互联网连接也能够找到一家餐馆,例如当您在国外时。
I am writing an application which has ca. 7000 european restaurants in the database and I want to show a list of the nearest ones to the user, for example all of them which are in a radius of 5km to the user.
I could do the search on the server but it then requires internet. Is it possible to save the data on the iPhone and query the database? I can't find any references for something like that in core data or iPhones SQLite.
Do I really need to program it myself with Pythagoras and stuff and calculate the distance to every restaurant on every query? Or is there some other way?
[update] I'd like to use it like I do already on the server (but to do it on the iPhone): SELECT * FROM restaurants WHERE ST_Distance_Sphere(geo, ST_GeomFromText(POINT(55.98767 57.12345), -1)) < 5000
I want the user to be able to find a restaurant even if she has no internet connection, for example when you're in a foreign country.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读纬度和经度计算。纬度和经度本身就是表示为球面上的弧的距离。如果您有一个由纬度和经度表示的位置数据库,则可以执行提取来仅查找位于用户当前位置的南北纬度几度和东西经度几度范围内的记录。
因此,您最终会得到一个类似于(伪代码)的谓词:
...这将创建一个逻辑框,该逻辑框将返回该框内的所有餐厅记录。然后,如果您需要计算精确距离,您只需对 7,000 条记录中的少数记录进行计算即可。
我建议阅读 位置服务,因为它提供了很多处理位置计算的工具。
至于是否使用纯 SQL 还是核心数据,请查看这个之前的答案 简而言之,如果您已经了解 C SQL api 并且您的数据简单、大型且静态,那么 SQL 是一个不错的选择。如果您可以花时间学习 Core Data 并且您的数据复杂且动态,那么无论数据大小如何,Core Data 都是更好的选择。
Read up on latitude and longitude calculations. Latitude an longitude are themselves distances expressed as arcs upon a spherical surface. If you have a database of locations expressed by latitude and longitude, then you can perform a fetch to find only those records that fall within a few degrees latitude north and south and a few degrees longitude east and west of the users current location.
So you would end up with a predicate that would be something like (psuedo-code):
... this would create a logical box which would return all restaurant records that fell within the box. Then if you needed to calculate the exact distances you would only have to perform calculation on a handful of records out of the 7,000 you have.
I would recommend reading up on Location Services because it provides a lot of tools for handling location calculations.
As to whether to use plain SQL or Core Data, check out this previous answer on the subject. In brief, if you already know the C SQL api and your data is simple, large and static, then SQL is a good choice. If you can take time to learn Core Data and your data is complex and dynamic, then Core Data is the better choice regardless of the size of the data.
您可以使用iOS 版 GeoFire。它使用 Firebase 来存储数据,但由于 Firebase 维护本地缓存,因此在没有网络连接的情况下它仍然可以工作。它将处理 GeoQueries 并在项目进入和离开查询时实时通知您。
[免责声明:我在 Firebase 工作]
You could use GeoFire for iOS. It uses Firebase to store the data, but since Firebase maintains a local cache, it will still work without a network connection. It will handle both the GeoQueries as well as notifying you in realtime as items enter and leave your query.
[disclaimer: I work at Firebase]