Using Casey P Thomas' solution to handling multiple markers in a single location:
It works great and combines the content into a single info window, however there does seem to be a geocoding limit on the api which is breaking the map. I dont even need to geocode the addresses, as I have a list of co-ords that I am passing in.
So my question is, how could I bypass the geocoding function from the JS and build the map that way?
Thanks
Code I am using:
var map;
//marker clusterer
var mc;
var mcOptions = {gridSize: 20, maxZoom: 17};
//global infowindow
var infowindow = new google.maps.InfoWindow();
//geocoder
var geocoder = new google.maps.Geocoder();
//Content and Geos
var address = new Array(<?php echo $tweetgeos;?>);
var content = new Array(<?php echo $tweets;?>);
function createMarker(latlng,text) {
var marker = new google.maps.Marker({
position: latlng
});
///get array of markers currently in cluster
var allMarkers = mc.getMarkers();
//check to see if any of the existing markers match the latlng of the new marker
if (allMarkers.length != 0) {
for (i=0; i < allMarkers.length; i++) {
var existingMarker = allMarkers[i];
var pos = existingMarker.getPosition();
if (latlng.equals(pos)) {
text = text + " & " + content[i];
}
}
}
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
infowindow.setContent(text);
infowindow.open(map,marker);
});
return marker;
}
function geocodeAddress(address,i) {
geocoder.geocode( {'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = createMarker(results[0].geometry.location,content[i]);
mc.addMarker(marker);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function initialize(){
var options = {
zoom: 5,
center: new google.maps.LatLng(51.50733,-0.12768),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), options);
//marker cluster
mc = new MarkerClusterer(map, [], mcOptions);
for (i=0; i<address.length; i++) {
geocodeAddress(address[i],i);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
You can initialize your markers directly with your coordinates and without calling Geocoding API.
// Change address Array to an Array of JS Objects
var address = [
{
latitude: lat1,
longitude: lng1,
},
{
latitude: lat2,
longitude: lng2,
},
...
];
// In function:createMarker()
var myLatLng = new google.maps.LatLng(latlng.latitude, latlng.longitude);
var marker = new google.maps.Marker({
position: myLatLng,
});
// In function:initialize()
for (i=0; i<address.length; i++) {
createMarker(address[i], content[i]);
}