我每隔X秒尝试更新地图上的标记。地图可以使用初始数据加载,但如何只刷新标记(而不是整个地图) 我的代码非常简单。我对php和mysql有很好的了解但是我缺乏足够的java知识来解决这个问题。
<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>
我有一种强烈的感觉,我需要在某处使用setInterval函数..但是我尝试了几次,例如把它作为这个小部分;
setInterval(function() {
downloadUrl('http://myurl.p....
}, 5000);
但没有运气..任何想法?
更新
我设法使用下面的代码让它工作..我不知道这是否是最有效的方式..现在我得到额外的标记显示一条线索...所以我仍然需要一种方法来在放入新标记之前清除所有标记;
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);
});
}
答案 0 :(得分:1)
您使用了许多只使用一次的变量。你冷压缩你的代码了一下。此外,我为您的标记添加了一个数组,以便您可以通过函数清除它们。在致电downloadUrl
而不是将其添加到那里之前,也可以调用此函数:
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;
}
应该工作。