I had a similar problem on a project before.
Zoom management
Depending on the zoom used by the user I had to show way too much markers.
So I had to find a way to limit the numbers of markers displayed depending on the zoom level that was used.
I used the markerManager plugin (http://google-maps-utility-library-v3.googlecode.com/svn/tags/markermanager/1.0/docs/examples.html).
function initialize() {
var mapOptions = {
scaleControl: true,
center: new google.maps.LatLng(50, 3),
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
mgr = new MarkerManager(map);
};
Then when you add a new marker you use the manager to set a minimum and maximum zoomLevel where the marker is visible.
The marker won't be displayed if not in the zoom range.
function add_marker(lat, lng, zoomMin, zoomMax, img, path, camPos) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
icon: "file:///" + path + "/images/" + img + ".png"
});
google.maps.event.addListener(marker, 'click', function () {
form1.jsCamPos = camPos;
form1.changePosition();
});
mgr.addMarker(marker, zoomMin, zoomMax);
}
Thanks to that you can set all your detailled marker at the max zoom level and for higher zoom levels instead of displaying all the markers add some more marker to suggest your user to zoom in in order to see the detailled markers.
This will greatly improve your application reactivity.
Markers map bounds filtering
Another way to do would be to load asynchronously the markers depending on the bounds of the map.
You can for example add a listener to the bounds_changed event which is fired each time the map is moved, then you get the new bounds and call a webService which will give you all the markers inside the new bounds.
google.maps.event.addListener(map, 'bounds_changed', function () {
var lat0 = map.getBounds().getNorthEast().lat();
var lng0 = map.getBounds().getNorthEast().lng();
var lat1 = map.getBounds().getSouthWest().lat();
var lng1 = map.getBounds().getSouthWest().lng();
var parameters = "{'lat0':'" + lat0 + "','lng0':'" + lng0 + "','lat1':'" + lat1 + "','lng1':'" + lng1 + "'}";
/*
The webService should reply something like this :
[
{"lat":50.5215,"lng":2.515,"title":"marker1"},
{"lat":50.5126,"lng":2.5165,"title":"marker2"},
{"lat":50.5127,"lng":2.54564,"title":"marker3"}
];
*/
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$.each (msg, function (idx) {
console.log (msg[idx].lat);
console.log (msg[idx].lng);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(msg[idx].lat, msg[idx].lng),
title: msg[idx].title,
map:map
});
});
},
error: function(e){
alert('error!');
}
});
});