This is based on the Google Maps API v3. The page refreshes every 10 seconds and updates the marker positions on the map with auto centre and auto zoom. The following are the issues that I face:
- Every time the page loads(every 10 seconds) map is centered to (47.6145, -122.3418) initially and then centers and zoom based on the markers positions. If uncomment the following the map doesn't load at all:
center: new google.maps.LatLng(47.6145, -122.3418),
How can I stop the map loading initial lat and long coordinates (47.6145, -122.3418) every time the map refreshes.
2. Is there a way to control the zoom levels?
Here is the code based on the google maps api v3
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 14,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
downloadUrl("phpsqlajax_genxml2.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
var UID = markers[i].getAttribute("UID");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("latitude")),
parseFloat(markers[i].getAttribute("longitude")));
var html = "<b>" + UID + "</b> <br/>";
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bounds.extend(point);
bindInfoWindow(marker, map, infoWindow, html);
}
map.fitBounds(bounds);
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
setInterval(load, 10000);
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}