GoogleMaps - 带有群集和多群集解决方案的gmaps.js

时间:2014-09-30 03:25:01

标签: google-maps google-maps-api-3

我正在使用gmaps.js和gmaps.js以及marker clusterer。在我的情况下,我可能有多个具有完全相同位置的标记,但实际上代表不同的数据。为了解决这个问题,我试图实施solution found here on SO - 特别是Nathan Colgate的解决方案。

这个想法是当群集上达到最大缩放时,它将执行multiChoice功能。我有这部分工作。我无法工作的是显示带有该功能的infoWindow。

我的目标是在此群集上显示infoWindow,以显示有关每个标记的信息(特别是每个标记的infoWindow内容(这将包含特定于其的其他详细信息)。

JS:

//create the map
var map = new GMaps({
    el: '#map_canvas_main',
    lat: response.results[0].lat,
    lng: response.results[0].lng,
    zoom: 5,
    maxZoom: 15,
    panControl: false,
    markerClusterer: function(map) {
        markerCluster = new MarkerClusterer(map, [], {
            title: 'Location Cluster',
            maxZoom: 15
        });

        // onClick OVERRIDE
        markerCluster.onClick = function(clickedClusterIcon) { 
            return multiChoice(clickedClusterIcon.cluster_); 
        }

        return markerCluster;
    }
});

//loop through array
for(var i = 0; i < response.results.length; i++)
{
    //create marker image
    var markerLoc = {
        url: '/custom/plugins/gmaps/images/marker-red.png',
        size: new google.maps.Size(24, 30), //size
        origin: new google.maps.Point(0, 0), //origin point
        anchor: google.maps.Point(9, 30) // offset point
    };

    //add marker
    map.addMarker({
        lat: response.results[i].lat,
        lng: response.results[i].lng,
        icon: markerLoc,
        title: response.results[i].ip_address,
        infoWindow: {
          content: '<p>'+response.results[i].ip_address+'</p>'
          //add more details later
        }
    }); 
}

//cluster function to do stuff
function multiChoice(clickedCluster)
{
    //clusters markers      
    var markers = clickedCluster.getMarkers();

    //console check
    console.log(clickedCluster);
    console.log(markers);

    if (markers.length > 1)
    {           
        //content of info window
        var infowindow = new google.maps.InfoWindow({
            content: ''+
                '<p>'+markers.length+' = length</p>'+
                '<p>testing blah blah</p>'
        });

        //show the window
        //infowindow.open(??????????);

        return false;
    }
    return true;
};

1 个答案:

答案 0 :(得分:0)

终于弄明白这个了解更多...现在有道理,但之前没有。这是新的&#39;显示&#39;功能被OP中的功能替换。当然,还需要进行一些其他更改...例如,在新信息窗口中显示所有聚类标记数据,但这是让窗口工作的要点。

//集群功能做东西 function multiChoice(clickedCluster) {     //簇标记
    var markers = clickedCluster.getMarkers();

if (markers.length > 1)
{
    //create the info window
    var infoWindow = new google.maps.InfoWindow({
        content: ''+
            '<p>'+markers.length+' = length</p>'+
            '<p>testing blah blah</p>',
        position: clickedCluster.center_
    });

    //display the infowindow
    infoWindow.open(clickedCluster.map_);

    return false;
}
return true;

};