Aayush Khandelwal
Aayush Khandelwal

Reputation: 1071

zoom in to particular region in gmap4rails

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

Upvotes: 1

Views: 630

Answers (1)

apneadiving
apneadiving

Reputation: 115541

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);
   }
});

Upvotes: 2

Related Questions