zoom in to particular region in gmap4rails

2019-07-31 09:16发布

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条回答
甜甜的少女心
2楼-- · 2019-07-31 09:33

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);
   }
});
查看更多
登录 后发表回答