Can you help me please to create marker cluster with MarkerClustererPlus. I load the data with:
layer = map.data.loadGeoJson('resources/data.geojson');
and the markers are visible, but i have not idea how to create markercluster. Do i have to parse the data.geojson-file into an array? Thanks.
function initialize() {
map = new google.maps.Map(document.getElementById('map'), mapOptions);
layer = map.data.loadGeoJson('resources/data.geojson');
map.data.setStyle({icon: icon});
map.data.addListener('click', function(event) {
var myHTML = event.feature.getProperty('name');
infobox.setContent("<div style='width:150px; text-align: center;'>"+myHTML+"</div>");
infobox.setPosition(event.feature.getGeometry().get());
infobox.setOptions({pixelOffset: new google.maps.Size(0,0)});
infobox.open(map);
});
google.maps.event.addListener(map, "click", function(){
infobox.close();
});
map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');
}
google.maps.event.addDomListener(window, 'load', initialize);
Here is a piece of my GeoJson-File (this piece and the whole file is tested by GeoJSONLint.
{
"type": "FeatureCollection",
"icon": "resources/icon.png",
"features": [
{
"type": "Feature","properties": {"name":"Bielefeld"},
"geometry": {"type": "Point","coordinates":[8.528849, 52.030656]}
},
{
"type": "Feature","properties": {"name":"Herford"},
"geometry": {"type": "Point","coordinates":[8.676780, 52.118003]}
},
{
"type": "Feature","properties": {"name":"Guetersloh"},
"geometry": {"type": "Point","coordinates":[8.383353, 51.902917]}
}
]
}
The shapes created for the data-layer are not accessible via the API, but you must have a reference to the markers to be able to add them to the clusterer.
Possible solution:
Observe the
addfeature
-event of the data and create your own markers. The markers created for the data-layer hide(either via thevisible
-style set to false or remove the feature completely when you don't need to access it later)Example:
Demo: http://jsfiddle.net/doktormolle/myuua77p/
Of course this solution creates some overhead. When there are only Points inside the FeatureCollection you better parse the geoJSON on your own instead of using the data-layer
create a MarkerClusterer to manage the markers.
add each marker to it when the data layer fires the
addfeature
event.hide the data layer markers.
working jsfiddle
code snippet:
Rather old question, but there is no longer any need to take that detour adding regular markers and clearing them from the data layer (especially if you need to later get the map content as GeoJSON again after editing the map).
You can use https://github.com/Connum/data-layer-clusterer, which I just enhanced to make it work for LineString and Polygon features as well.
Here's a working example: