zoom in to particular region in gmap4rails

2019-07-31 08:57发布

问题:

How to zoom in to a particular coordinates in gmaps4rails. suppose at the index page of my application i want to show addresses of all users but want that initially the map should focus on USA and with a certain zoom level

Thanks in advance

回答1:

After your gmaps call:

<% content_for :scripts do %>
    <script type="text/javascript" charset="utf-8">
      Gmaps.map.callback = function() {
        setTimeout(function() {
          var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(10, 0), new google.maps.LatLng(0, 10));
          Gmaps.map.serviceObject.panToBounds(bounds);
       }, 50);
      }
    </script>
<% end %>

The zoom will be adapted to the bounds passed, otherwise add:

Gmaps.map.serviceObject.setZoom(your_int_value);

A cleaner approach is to use the callback provided by google:

Gmaps.map.callback = function() {
   google.maps.event.addListenerOnce(Gmaps.map.getMapObject(), 'idle', function(){
     var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(10, 0), new google.maps.LatLng(0, 10));
     Gmaps.map.serviceObject.panToBounds(bounds);
   }
});