简体   繁体   English

使用ruby / rails geocoder gem获取request.location附近的位置

[英]Get locations nearby request.location with ruby/rails geocoder gem

How would you get locations that are nearby your current location with geocoder gem for ruby on rails 3.2.x? 您如何获得位于当前位置附近的位置,使用地理编码器gem for ruby​​ on rails 3.2.x?

I know that to find locations near one of your other locations you would use 我知道要找到您将使用的其他位置附近的位置

@myClass.nearbys(50)

but I would like them nearby the browsers location (request.location) instead of nearby another one of my locations. 但我希望他们附近的浏览器位置(request.location)而不是附近的另一个位置。

Such that it would be similar to 这样就好了

request.location.nearby(50)

or something to that effect. 或者那种效果。

How can this be achieved? 怎么能实现这一目标?

Thanks 谢谢

If you have the longitude and latitude of the user's location, you can make a query like so 如果您拥有用户位置的经度和纬度,则可以进行类似的查询

location_info = request.location

@locations = Location.near([location_info.latitude, location_info.longitude], radius_distance_you_want_to_include)

It would also be expected that your table, ie in the above example Location there should be lat and lon fields. 这也将预计你的表,即在上面的例子中Location应该有latlon领域。

Is this something you were looking for? 这是你要找的东西吗?

EDIT: I'm not sure if request.location will work in the model. 编辑:我不确定request.location是否可以在模型中工作。 In case it doesn't, you can always set it as a parameter into a model method inside your controller so that your logic stays separated. 如果没有,您可以随时将其作为参数设置到控制器内的模型方法中,以便逻辑保持分离。 But if you can, keep request.location in the model. 但是如果可以,请在模型中保留request.location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM