如何检查经纬度是否在城市范围内 [英] How to check if lat long is in the city limits

查看:164
本文介绍了如何检查经纬度是否在城市范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查我的经/纬度是否在城市范围内,例如,大伦敦位于以下位置:

How can I check if my lat/long is in the city limits or example, Greater London is enclosed by:

[bbox = -0.489,51.28,0.236 ,51.686]

[bbox=-0.489,51.28,0.236,51.686]

来源:

http://wiki.openstreetmap.org/wiki/Bounding_Box

如何检查位置(纬度/经度):

How can I check if a location (lat/lon):

51.55238,0.047032

已经有宝石吗?或最好的方法是什么?

Is there a gem already for this? Or what would be the best way to do this?

赏金更新:

我有一个可行的解决方案,但我感觉没有正确的一个,我正在使用geocoder gem,我的用户有geocoded_by和lat / long属性。所以这是我的做法:

I have a working solution but I feel its not the right one, I'm using geocoder gem, and my user has geocoded_by, and lat/long attributes. So here is how I do it :

def self.user_from_dublin(user_id)
  near([53.349937,-6.261917], 15).pluck(:id).include?(user_id)
end

因此,这会加载来自都柏林的所有用户,并将该用户的ID与都柏林的那些用户进行比较。如果找到该ID,则认为该用户来自都柏林。

So this loads all users from Dublin and compares the id of the user to those users in Dublin. If the id is found the user is considered to be from Dublin.

我认为如果有更多用户,此方法将无法正常工作。

I don't think this will work well when there is more users.

更新

我正在使用postgres数据库

I'm using postgres database

推荐答案

如果是布尔型,为什么不使用 where 将结果限制为仅用户。这样,它将只涉及一个为空或大小为1(您的用户是唯一记录)的SQL查询。

If its a boolean why not use where to limit the results to only your user. This way it will just involve one SQL query that will either be empty or have a size of 1 (your user being the only record)

def self.user_from_dublin(user_id)
  !(near([53.349937,-6.261917], 15).where(id: user_id).empty?)
end

空?在您的用户不在边界内时为真,因此开头的将对此取反,当用户不在边界内时,您的函数将返回 false

empty? will be true when your user is not within your boundary, so the ! at the beginning will negate this and your function will return false when the user is not within the boundary.

这篇关于如何检查经纬度是否在城市范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆