After receiving json data, my goal is to populate a map with markers that will cluster together if they are located too closely. To achieve this, I am utilizing specific extensions for Markers and Clusters available at this link.
I have written code that successfully accomplishes the task at hand, but there are a couple of issues that I'm facing:
1 - It appears that markers and clusters are stacking on top of each other. When using Chrome's inspector tool to remove one marker or cluster, another one seems to appear right beneath it. Additionally, as I zoom in or drag the map, more stacking occurs.
2 - While zooming out, some markers remain outside the cluster, yet the cluster count includes these outliers. This issue seems connected to the stacking problem mentioned in the previous point.
Below are the main sections of my code:
function initialize(lat, lng) {
window.lat = lat;
window.lng =lng;
var myLatlng = new google.maps.LatLng(lat,lng);
var mapOptions = {
mapTypeControl: false,
center: myLatlng,
zoom: 13,
maxZoom:18,
zoomControl: true,
mapTypeControl: true
};
map = new google.maps.Map(document.getElementById('map-full'), mapOptions);
google.maps.event.addListener(map, 'idle', function() {
$mapBounds = map.getBounds();
getJSONData($mapBounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize(<?=$location;?>));
function getJSONData(map_bounds){
var bounds = {
'swlat':map_bounds.getSouthWest().lat(),
'swlng':map_bounds.getSouthWest().lng(),
'nelat':map_bounds.getNorthEast().lat(),
'nelng':map_bounds.getNorthEast().lng()
};
data = {
'bounds': bounds,
}
$.ajax({
type: "POST",
dataType: 'json',
url: "<?=$data_URL;?>",
data: data,
success: function (json) {
populateMap(json, bounds);
}
});
}
function populateMap(data, bounds){
//// ADD Markers
var markerCluster = null;
var markers = [];
var infowindow = new google.maps.InfoWindow();
leftList(data); // adds properties to left list
for (var i = 0; i < data.length; i++) {
var latLng = new google.maps.LatLng(data[i].lat, data[i].lng);
// drop the marker
var marker = new MarkerWithLabel({
position: latLng,
map: map,
labelContent: data[i].price,
labelAnchor: new google.maps.Point(27, 35),
title: data[i].title,
labelClass: "map-markers",
infoData: data[i],
zIndex: i
});
google.maps.event.addListener(map, 'dragstart', function() {
infowindow.close();
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
var info_content = makeMarkerInfo(this.infoData, this.index);
infowindow.setContent(info_content);
infowindow.open(map,this);
});
markers.push(marker);
}
///// ADD CLUSTERS
var clusterOptions = {
zoomOnClick: false
}
markerCluster = new MarkerClusterer(map, markers, clusterOptions);
// Zoom in or show infowindow when click on Cluster
google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
if (map.getZoom() < map.maxZoom ){
map.setCenter(cluster.center_);
map.setZoom(map.getZoom() + 4);
} else {
var content = '';
// Convert lat/long from cluster object to a usable MVCObject
var info = new google.maps.MVCObject;
info.set('position', cluster.center_);
//Get markers
var marks_in_cluster = cluster.getMarkers();
console.log(marks_in_cluster);
for (var z = 0; z < marks_in_cluster.length; z++) {
content = makeClusterInfo(marks_in_cluster,z) ;
}
infowindow.close();
infowindow.setContent(content);
infowindow.open(map, info);
google.maps.event.addListener(map, 'zoom_changed', function() {
infowindow.close()
});
}
});
}