I am trying to update markers on a map every X seconds. The map loads fine with the initial data but how to make only the markers refresh (and not the whole map)..
My code is pretty straight forward.. I have good knowledge of php and mysql but I lack enough java knowledge to pull this off.
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(51.337834, 5.222728),
zoom: 7
});
var infoWindow = new google.maps.InfoWindow;
downloadUrl('http://myurl.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
}
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);
}
function doNothing() {}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap">
</script>
I have a strong feeling I need to the use the setInterval function somewhere.. however I tried this a few times for example by putting it as this little part;
setInterval(function() {
downloadUrl('http://myurl.p....
}, 5000);
But no luck.. any ideas?
UPDATE
I managed to get it working using the below code.. I don't know if this is the most efficient way.. and now I get extra markers showing a trail.. so I still need a way to first clear all markers before putting the new ones;
downloadUrl('myurl.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
},5000);
});
}
You use a lot of variables which you only use once. You cold compress your code a bit. Moreover I added an array for your markers so you can clean them by a function. This function could also be called before you call downloadUrl
instead of adding it there:
var myMarkers = [];
downloadUrl('myurl.php', function(data) {
var xml = data.responseXML;
clearMarkers();
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = markerElem.getAttribute('name')
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = markerElem.getAttribute('address')
infowincontent.appendChild(text);
var icon = customLabel[markerElem.getAttribute('type')] || {};
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
myMarkers.push(marker);
});
}, 5000);
function clearMarkers() {
for (var i = 0; i < myMarkers.length; i++) {
myMarkers[i].setMap(null);
}
myMarkers.length = 0;
}
Should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.