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

时间:2013-07-25 20:37:38

标签: ruby-on-rails google-maps google-maps-api-3 gmaps4rails

我正在努力将spiderfier https://github.com/jawj/OverlappingMarkerSpiderfierhttps://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

1 个答案:

答案 0 :(得分:2)

使用旧版本的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