ruby-on-rails – 如何将spiderfier js与gmaps4rails gem集成? (同一纬度上的多个标记,lng)

我正在努力将spiderfier
https://github.com/jawj/OverlappingMarkerSpiderfier
https://github.com/apneadiving/Google-Maps-for-Rails整合

在我的map.html.haml中:

= gmaps(:markers => {:data => @map, :options => { "rich_marker" => true, :raw => '{ animation: google.maps.Animation.DROP }' } }, :map_options => { :draggable => true, :auto_zoom => false, :zoom => 9, :disableDefaultUI => false, :scrollwheel => true, :disableDoubleClickZoom => true, :custom_infowindow_class => "province" })

- content_for :scripts do
:javascript
    Gmaps.map.infobox = function(boxText) {
        return {
             content: boxText
            ,disableAutoPan: false
            ,maxWidth: 0
            ,pixelOffset: new google.maps.Size(-140, -50)
            ,alignBottom: true
            ,zIndex: 999
            ,hideCloseButton: false
            ,boxStyle: { 
                background: "white"
                ,width: "280px"
                ,padding: "10px"
                ,border: "1px solid #b2b2b2"
                ,arrowStyle: 0
                ,arrowPosition: 50
                ,arrowSize: 20
                 }

            ,infoBoxClearance: new google.maps.Size(10, 10)
            ,isHidden: false
            ,pane: "floatPane"
            ,enableEventPropagation: false

     }};

这很好,但如何整合蜘蛛侠https://github.com/jawj/OverlappingMarkerSpiderfier#how-to-use

最佳答案 这是一个使用旧版Gmaps 4 Rails的老问题,但是对于有兴趣在最新版本中使用这两个库的人来说,这里是如何做到的.

此示例基于documentation sample code

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers([
    {
      "lat": 0,
      "lng": 0,
      "picture": {
        "url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
        "width":  36,
        "height": 36
      },
      "infowindow": "hello!"
    }
  ]);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();

  // Create a OverlappingMarkerSpiderfier instance
  var oms = new OverlappingMarkerSpiderfier(handler.getMap(), {
    keepSpiderfied: true
    // Other options you need
  });

  // Track each marker with OMS
  _.each(markers, function(marker) {
    oms.addMarker(marker.getServiceObject());
  });
});

这里重要的是要了解Gmaps4Rails在其自己的类中包装的本机Google对象.

如果您需要在标记上单击事件,请向oms添加单个处理程序,如in the doc所述.

点赞