Search code examples
javascriptgoogle-mapsmarkerspiderfier

how to add Spiderfier in Googlemaps api v3?


I am new to googlemaps. I have already read the Overlapping Marker Spiderfier documentation but I am still confused about how to implement it into my maps.

Here is my attempt:

function initialize() {
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 7,
        center: new google.maps.LatLng(40.0000, 48.0000),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        zoomControl: true
    });
    var locations = [[40.0000, 48.0000], [40.0000, 48.0000], [40.0000, 48.0000]];
    var marker, i;
    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][0], locations[i][1]),
            map: map
        });
    }
}
google.maps.event.addDomListener(window, 'load', initialize);

You can find this code in jsfiddle.


Solution

  • Here is your code updated...

    function initialize() {
    
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 7,
          center: new google.maps.LatLng(40.0000, 48.0000),
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          disableDefaultUI: true,
          zoomControl: true
        });
        var locations =[ [40.0000, 48.0000],[40.0000, 48.0000],[40.0000, 48.0000]];
    
        oms = new OverlappingMarkerSpiderfier(map,
            {markersWontMove: true, markersWontHide: true, keepSpiderfied: true, circleSpiralSwitchover: 40 });
    
        var marker, i;
          for (i = 0; i < locations.length; i++) {  
             marker = new google.maps.Marker({
               position: new google.maps.LatLng(locations[i][0], locations[i][1]),
               map: map
             });
             oms.addMarker(marker);
          }
    }
    google.maps.event.addDomListener(window, 'load', initialize);