我的Rails代码中有以下视图:
<%= gmaps4rails(@ map,{"processing"=>'json','auto_adjust'=> false,'auto_zoom'=> false,'zoom'=> 10})%>
我的地图上只有一个标记.当我显示地图时,尽管我在上面设置了选项,但缩放始终设置为最大值.我忘记了一个选择吗?
我在当前项目中使用gmaps4rails非常棒的gem,并且必须在DB中导入超过一千个应该充当gmappable的对象.
然而,并非所有人都有正确的地址!因此,当我尝试导入时,在第一个无法使用Gmaps4rails :: GeocodeStatus进行地理编码的地址上失败.
是否有可能在可能的情况下跳过错误和地理编码?
我想出了这个:
acts_as_gmappable :process_geocoding => false
before_save :prepare_gmaps
private
def prepare_gmaps
begin
data = Gmaps4rails.geocode(address).first
self.latitude= data[:lat]
self.longitude= data[:lng]
rescue Gmaps4rails::GeocodeStatus
end
end
Run Code Online (Sandbox Code Playgroud) validation geocoding ruby-on-rails ruby-on-rails-3 gmaps4rails
如何将地图类型设置为HYBRID或SATELLITE?我认为传递给提供者哈希是一些选择,但如果是这种情况我找不到合适的哈希.
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();
});
Run Code Online (Sandbox Code Playgroud)
谢谢
看完这个答案后,我仍然无法将标记显示在页面中央.
我的代码如下;
在视图中
<div class="Flexible-container">
<!--Google Maps Div-->
<div id="map" style='width: 425; height: 425px;' frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></div>
</div>
<!-- End of Responsive iFrame -->
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.map.centerOn(markers[0]);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.map.serviceObject.setZoom(16);
});
</script>
Run Code Online (Sandbox Code Playgroud)
在控制器中
def edit
@user = current_user
@hash = Gmaps4rails.build_markers(@user) do |u, marker|
marker.lat u.latitude
marker.lng u.longitude
marker.infowindow u.name
end
end
Run Code Online (Sandbox Code Playgroud)
CSS
/* Flexible iFrame */
.Flexible-container {
position: relative;
padding-bottom: …Run Code Online (Sandbox Code Playgroud) 我正在开发一个使用gmaps4rails的应用程序(https://github.com/apneadiving/Google-Maps-for-Rails).我想在标记中添加一个事件监听器,以便在地图旁边创建一个可视化(以及在地图上显示信息窗口).是否可以使用gmaps4rails为每个标记添加事件监听器?
使用Rails 3.2.13,ruby 1.9.3p374和Gmaps4rails 1.5.6.
我一直在讨论一些事情,起初,我认为应该是相当直接的:我想显示一个标记,当点击时,将重定向到特定路径,在同一窗口中加载该路径.
Gmaps4rails在他们的描述中没有简单的选择.所以我花了无数个小时搜索网页,以及我在StackOverflow中找到的两个最佳点击:这闻起来像正确的路径,但这个似乎也很有用.
最后,我的控制器看起来像:
@gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker |
marker.title house.reference
marker.json({:link => polymorphic_url(house, :routing_type => :path)})
end
Run Code Online (Sandbox Code Playgroud)
这是正确的(我认为)在处理过的html中生成以下内容:
<script src="//maps.google.com/maps/api/js?v=3.8&sensor=false&client=&key=&libraries=geometry&language=&hl=&region=" type="text/javascript"></script>
<script type="text/javascript">
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.auto_zoom = false;
Gmaps.map.map_options.zoom = 3;
Gmaps.map.initialize();
Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}];
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
Gmaps.oldOnload = window.onload;
window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };
</script>
Run Code Online (Sandbox Code Playgroud)
标记出现在正确的位置.但是现在,解释第一个相关引用上的描述,我直接在视图上键入,确保它将出现在所有其他javascripts Gmaps4Rails转储到生成的html中:
<script type="text/javascript">
function redirect_to(url) { …Run Code Online (Sandbox Code Playgroud) 我是中国的学生.并使用gmap4rails宝石时,我有一个疑难问题.要设置的茂中心和缩放一样,官方指导报价
https://developers.google.com/maps/documentation/javascript/tutorial?#HelloWorld
所以像我的代码图片显示.但它不起作用.谷歌的问题,但没有得到答案.更多的是,我想把语言设置为中文我喜欢这样做,但它没有用.
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8
};
handler = Gmaps.build('Google',mapOptions);
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?
我试图从文档中搜索答案,但找不到..
当我通过AJAX在地图上添加标记时,如何在Gmaps4rails中启用聚类.我的观点是这样的:
<%= gmaps( map_options: { zoom: 15, auto_adjust: false } ) %>
Run Code Online (Sandbox Code Playgroud)
并在我的JavaScript中我添加这样的标记:
$.getJSON(path, { lat: lat, lng: lng, user_lat: user_lat, user_lng: user_lng }, function(markers_json) {
Gmaps.map.replaceMarkers(markers_json);
});
Run Code Online (Sandbox Code Playgroud)
我只想将do_clustering选项设置为true,但因为我在地图gmaps()调用中没有标记json,所以我也无法添加标记选项.
如果我单击某些内容将5个标记添加到地图上,例如100英里 - 将添加标记,并缩小地图以适应所有标记.
如果我然后单击显示标记的内容,例如2英里......标记会正确消失,但地图不会缩放.
我正在使用2.1.1
这是我的javascript调用:
Gmaps.store.handler.removeMarkers(Gmaps.store.markers);
Gmaps.store.markers = Gmaps.store.handler.addMarkers(<%= raw @hash.to_json %>, {
draggable: false,
animation: google.maps.Animation.DROP
});
Gmaps.store.handler.bounds.extendWith(Gmaps.store.markers);
Gmaps.store.handler.fitMapToBounds();
Run Code Online (Sandbox Code Playgroud)
我错过了一个电话吗?我想removeMarkers现在清除了群集器?
javascript ruby-on-rails google-maps-api-3 gmaps4rails gmaps4rails2
gmaps4rails 2 + rails 4
def CommonUtils.get_gmap_hash(object)
Gmaps4rails.build_markers(object) do |user, marker|
marker.lat user.latitude
marker.lng user.longitude
marker.infowindow user.location
binding.pry
marker.picture({
:picture => "/img/blank.png",
:width => 32,
:height => 32
})
marker.json({:title => user.title})
end
end
Run Code Online (Sandbox Code Playgroud)
这里的marker.picture方法不打自己也不知道为什么?有帮助吗?
和默认标记即将到来我想要不同的标记(blank.png).
blank.png位于assets/images/img/blank.png中
gmaps4rails ×10
google-maps ×4
javascript ×3
ruby ×3
geocoding ×1
gmaps4rails2 ×1
validation ×1