[英]Update google java map with mysql data every X seconds
I am trying to update markers on a map every X seconds.我正在尝试每 X 秒更新一次地图上的标记。 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.我的代码非常简单。我对 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>
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 函数。但是我尝试了几次,例如将其作为这个小部分;
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:也可以在调用
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;
}
Should work.应该管用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.