Gmaps4rails V2 - change default zoom
remove:
handler.fitMapToBounds();
Replace with:handler.getMap().setZoom(yourValue);
Adjust Zoom Gmaps4Rails
You're simply missing that seeing the whole map means providing a very small number, not a big one!
<%= gmaps({
"map_options" => {"auto_adjust" => false, "zoom" => 0 },
"markers" => {"data" => @json }
})
%>
Zoom bar disfigured in Google-Maps-for-Rails (gmaps4rails)
Add this to your css... there you will also have an issue with the terrain and overlays dropdowns...
#mapCanvas label { width: auto; display:inline; }
#mapCanvas img { max-width: none; }
Changing zoom causes all hidden markers to re-appear on Google Map (Gmaps4rails)
Basically, when you use a clusterer, it stores all markers.
When you zoom out, it sets their map to
null
and adds cluster pics.When you zoom in, it removes the cluster pic and sets the map again in the markers.
marker.setMap(null)
, it's not enough: you have to remove the markers from the clusterer too!_.each markers, (marker, i) ->
marker.setMap(null)
handler.clusterer.removeMarker(marker)
But I also needed this scenario, so just do:_.each markers, (marker, i) ->
handler.removeMarker(marker)
Or even:handler.removeMarkers(markers)
gmaps4rails single marker auto zoom
Ok, the interface changed a lot since I posted the answer you quote.
<%= gmaps("markers" => {"data" => @json, "options" => {"auto_zoom" => false} }) %>
<% content_for :scripts do %>
<script type="text/javascript" charset="utf-8">
Gmaps.map.callback = function() {
if (Gmaps.map.markers.length == 1) {
//only one marker, choose the zoom level you expect
setTimeout(function() { Gmaps.map.serviceObject.setZoom(5);}, 50);
}
else{
//more than one marker, let's auto_zoom
Gmaps.map.map_options.auto_zoom = true;
Gmaps.map.adjustMapToBounds();
}
}
</script>
<% end %>
Google Maps, Ruby on Rails, Zoom level with one marker
This behavior is due to the auto_zoom
built-in function in the google maps api.
One work around to this is to set it to false
in the gmaps
method:
<%= gmaps({
"map_options" => { "auto_zoom" => false},
"markers" => { "data" => @json }
})
%>
And then use the gmaps4rails_callback
to fit your needs (be sure to have at least version 0.7.9)<script type="text/javascript" charset="utf-8">
function gmaps4rails_callback() {
if (Gmaps4Rails.markers.length == 1) {
//only one marker, choose the zoom level you expect
Gmaps4Rails.map.setZoom(2);
}
else{
//more than one marker, let's auto_zoom
Gmaps4Rails.map_options.auto_zoom = true;
Gmaps4Rails.adjust_map_to_bounds();
}
}
</script>
Related Topics
Where Are Keywords Defined in Ruby
Convert Durations in Ruby - Hh:Mm:Ss.Sss to Milliseconds and Vice Versa
Ruby Fails on Osx Lion with Rbenv
How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby
Many to Many Table with an Extra Column in Rails
Error While Starting Puma Server with Workers
Rails Redirecting Invalid Route to Root
Problem with Vim's Ruby Plugin
Why The Unit Test Frameworks in Fortran Rely on Ruby Instead of Fortran Itself
Gem Ransack Doesn't Return Any Results When Searched with Full Name
Sinatra on Nginx Configuration - What's Wrong
Belongs_To Presence in Rails 5 Not Working
Algorithm to Render a Horizontal Binary-Ish Tree in Text/Ascii Form
Adding Two Activerecord::Relation Objects
How to Change "3 Errors Prohibited This Foobar from Being Saved" Validation Message in Rails