我创建了一个脚本,它将从sql获取lat和lon,并在嵌入的谷歌地图上放置一个标记。我想每秒运行一次脚本,如自动获取数据并自动放置标记。
<script>
var infoWindow= null;
var map = null;
var markersArray = [];
function initMap() {
var myLatlng = new google.maps.LatLng(14.657971, 120.976970);
var map = new google.maps.Map(document.getElementById('map_canvas'), {
center: myLatlng,
zoom: 16,
streetViewControl : false,
mapTypeId : google.maps.MapTypeId.HYBRID,
});
var infoWindow = new google.maps.InfoWindow;
updateMaps();
function updateMaps() {
// Change this depending on the name of your PHP or XML file
downloadUrl('phpsqlajax_genxml.php?t=', 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() {}
}
window.setInterval(updateMaps, 1000);
</script>
答案 0 :(得分:0)
var update;
function updateInterval() {
update = setInterval(function() {
updateMaps()
}, 1000);
}
updateInterval();
您必须使用JQuery在文档准备好时加载updateInterval()或在正文末尾写入<script>updateInterval();</script>
。