Gmaps4rails V2 - 更改默认缩放



我是一个彻头彻尾的菜鸟,正在构建我的第一个rails应用程序。 我成功地实现了Google-maps-for-rails V2,使用apneadiving在youtube上的教程:http://www.youtube.com/watch?v=R0l-7en3dUw&feature=youtu.be

我的

问题是,对于我的每张地图,我只显示一个标记,当地图加载时,它会调整为完全缩放。

搜索四周,似乎有很多早期版本的Gmaps4rails的解决方案,但是对于V2,并通过javascript创建地图,我似乎找不到有效的解决方案。

作为参考,我的代码如下:

视图:

    <script type="text/javascript">
      handler = Gmaps.build('Google');
      handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers(<%=raw @hash.to_json %>);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
      }); </script> 

控制器:

def show
  @pins = Pin.find(params[:id])
  @hash = Gmaps4rails.build_markers(@pins) do |pin, marker|
    marker.lat pin.latitude
    marker.lng pin.longitude
  end
end

我尝试通过控制台进行更改: gmap.setzoom(12),正如一些帖子所建议的那样,但没有任何运气。

任何帮助都非常感谢

对我有用的答案:将视图更改为:

  <script type="text/javascript">
  handler = Gmaps.build('Google');
  handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
  handler.getMap().setZoom(12);
  }); </script> 

感谢您的帮助!

删除:

handler.fitMapToBounds();

替换为:

handler.getMap().setZoom(yourValue);
Just Try This,
<script type="text/javascript">
      handler = Gmaps.build('Google');
      handler.buildMap({ provider: { Zoom: 3 }, internal: {id: 'map'}}, function(){
      markers = handler.addMarkers(<%=raw @hash.to_json %>);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
      }); </script> 

handler.getMap().setZoom(yourValue);

它应该工作.....

另一种选择是使用 handler.map.centerOn(marker):

      handler = Gmaps.build('Google');
      handler.buildMap({
      provider: {
        draggable: false,
        Zoom: 15
      },
      internal: {
        id: 'map'
      }
    },
    function(){
      marker = handler.addMarker(
        {
          "lat": lat,
          "lng": lng,
          "picture": {
            "url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
            "width":  36,
            "height": 36
          },
          "infowindow": "hello!"
        }
      );
      handler.map.centerOn(marker);
    }
  );

最新更新