【问题标题】:Draggable true in rails map not work?Rails 地图中的可拖动 true 不起作用?
【发布时间】:2016-09-21 09:07:05
【问题描述】:

我使用 gmaps4rails。我在rails中实例化地图并且它可以工作但是当我通过属性draggable:true时地图不起作用。这是我的代码。

handler = Gmaps.build('Google');
  handler.buildMap({ provider: {maxZoom: 5}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers([
        {
          "lat": 22,
          "lng": 22,
          "picture": {
            "width":  32,
            "height": 16
          },
          "infowindow": "hello!",
        },
        {
           "draggable": true
        }
      ]
      );
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
      handler.map.centerOn({ lat: 22, lng: 22 })
  });

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 gmaps4rails


    【解决方案1】:

    我相信,如果您将其中一些代码移至控制器(根据 Gmaps4Rails 指南),您可能会有更好的运气。例如,试试这样:

    def index
      @geolocations = [{:lat => 22, :lng => 22}]
      @hash = Gmaps4rails.build_markers(@geolocations) do |geolocation, marker|
        marker.lat geolocation[:lat]
        marker.lng geolocation[:lng]
        marker.infowindow ("hello!")
        marker.picture({"width" => 32, "height" => 16})
      end
    end
    

    然后对于 JS:

    var mapOptions = {};
    var handler = Gmaps.build('Google');
    handler.buildMap({ provider: {maxZoom: 5}, internal: {id: 'map'}}, function(){
        var markers = handler.addMarkers(<%= raw @hash.to_json %>, {draggable: true});
        handler.bounds.extendWith(markers);
        handler.map.centerOn([22, 22]);
        handler.fitMapToBounds();
    });
    

    有关使用处理程序的更多信息:https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Js-Methods

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多