zoom in to particular region in gmap4rails

柔情痞子 提交于 2019-12-11 07:57:12

问题


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


来源:https://stackoverflow.com/questions/12974802/zoom-in-to-particular-region-in-gmap4rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!