[英]RoR: How to get lat and lng using geokit-rails
I want to display a google maps centered at the user's location. 我想显示一个以用户所在位置为中心的Google地图。 I am willing to use
gmaps4rails
and geokit-rails
gems, because I guess those are the best ones. 我愿意使用
gmaps4rails
和geokit-rails
gems,因为我猜那些是最好的。
What I have so far is the map initialization: 到目前为止,我所要做的是地图初始化:
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script>
$(document).ready(function(){
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers([
{
"lat": 0,
"lng": 0,
"picture": {
"url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
"width": 36,
"height": 36
},
"infowindow": "hello!"
}
]);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
});
</script>
which works fine. 效果很好。 Now the problem is to get the latitude and longitud based on user IP.
现在的问题是基于用户IP获取纬度和经度。 This is what I am trying, but I am getting no result.
这是我正在尝试的方法,但没有任何结果。 In the controller I try to get the location using geokit gem, but it fails:
在控制器中,我尝试使用geokit gem获取位置,但失败:
def view
@location = IpGeocoder.geocode('181.169.135.95') #I know here I should use `request.remote_ip`, but as I am on localhost I can't
end
I got this from geokit github , but it is not working. 我是从geokit github上获得的 ,但是它不起作用。
You can obtain the location for an IP at any time using the geocoder as in the following example:
您可以使用地理编码器随时获取IP的位置,如以下示例所示:
location = IpGeocoder.geocode('12.215.42.19')
位置= IpGeocoder.geocode('12 .215.42.19')
where Location is a GeoLoc instance containing the latitude, longitude, city, state, and country code.
其中Location是一个GeoLoc实例,其中包含纬度,经度,城市,州和国家/地区代码。 Also, the success value is true.
同样,成功值是真实的。
I am getting the following error: uninitialized constant WineryController::IpGeocoder
. 我收到以下错误:
uninitialized constant WineryController::IpGeocoder
。 Can anyone explain me what does this mean, and how to go ahead? 谁能解释我的意思,以及如何继续? I suspect this might be something really stupid, but I am still a novice in rails.
我怀疑这可能确实很愚蠢,但是我仍然是菜鸟的新手。
Try something like this: 尝试这样的事情:
application_controller.rb application_controller.rb
class ApplicationController < ActionController::Base
# Auto-geocode the user's ip address and store in the session.
geocode_ip_address
def geokit
@location = session[:geo_location] # @location is a GeoLoc instance.
end
end
Then in your view you can display the IP address by accessing the values of the hash: 然后,在您的视图中,可以通过访问哈希值显示IP地址:
<%= [@location['lat'],@location['lng']] %>
The key lesson here is that a Ruby session is a hash data-type. 这里的关键课程是Ruby会话是哈希数据类型。 Consequently, the values of the hash must be retrieved by calling the hash key, such as
@location['lat']
and @location['lng']
. 因此,必须通过调用哈希键(例如
@location['lat']
和@location['lng']
来检索哈希值。
More info: Ruby - getting value of hash 更多信息: Ruby-获取哈希值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.