我一直在 这里 找到有关Google Maps上的PHP / MYSQL教程。
我希望每5秒钟左右从数据库更新一次标记。
据我了解,我需要使用Ajax定期更新标记,但是我很难理解要在哪里添加函数以及在哪里使用 setTimeout() 等
我发现的所有其他示例并没有真正说明正在发生的事情,一些有用的指导会很棒!
这是我的代码(与Google示例相同,但有一些 var 更改):
function load() { var map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(37.80815648152641, 140.95355987548828), zoom: 13, mapTypeId: 'roadmap' }); var infoWindow = new google.maps.InfoWindow; // Change this depending on the name of your PHP file downloadUrl("nwmxml.php", function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var host = markers[i].getAttribute("host"); var type = markers[i].getAttribute("active"); var lastupdate = markers[i].getAttribute("lastupdate"); var point = new google.maps.LatLng( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); var html = "<b>" + "Host: </b>" + host + "<br>" + "<b>Last Updated: </b>" + lastupdate + "<br>"; var icon = customIcons[type] || {}; var marker = new google.maps.Marker({ map: map, position: point, icon: icon.icon, shadow: icon.shadow }); bindInfoWindow(marker, map, infoWindow, html); } }); } function bindInfoWindow(marker, map, infoWindow, html) { google.maps.event.addListener(marker, 'click', function() { infoWindow.setContent(html); 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() {}
我希望有人能帮助我!
请注意,我没有对此进行测试,因为我没有方便的xml数据库
首先,您需要将load()函数拆分为用于初始化地图并在domready上加载标记的函数,以及稍后将用于处理xml和更新地图的函数。需要这样做,因此您不必在每次加载时都重新初始化地图。
其次,您需要决定如何处理地图上已经绘制的标记。为此,您需要在将它们添加到地图时将它们添加到数组。在第二次更新中,您可以选择重绘标记(重建阵列)或仅更新现有阵列。我的示例显示了一个场景,您只需从屏幕上清除旧标记即可(这比较简单)。
//global array to store our markers var markersArray = []; var map; function load() { map = new google.maps.Map(document.getElementById("map"), { center : new google.maps.LatLng(37.80815648152641, 140.95355987548828), zoom : 13, mapTypeId : 'roadmap' }); var infoWindow = new google.maps.InfoWindow; // your first call to get & process inital data downloadUrl("nwmxml.php", processXML); } function processXML(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName("marker"); //clear markers before you start drawing new ones resetMarkers(markersArray) for(var i = 0; i < markers.length; i++) { var host = markers[i].getAttribute("host"); var type = markers[i].getAttribute("active"); var lastupdate = markers[i].getAttribute("lastupdate"); var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); var html = "<b>" + "Host: </b>" + host + "<br>" + "<b>Last Updated: </b>" + lastupdate + "<br>"; var icon = customIcons[type] || {}; var marker = new google.maps.Marker({ map : map, position : point, icon : icon.icon, shadow : icon.shadow }); //store marker object in a new array markersArray.push(marker); bindInfoWindow(marker, map, infoWindow, html); } // set timeout after you finished processing & displaying the first lot of markers. Rember that requests on the server can take some time to complete. SO you want to make another one // only when the first one is completed. setTimeout(function() { downloadUrl("nwmxml.php", processXML); }, 5000); } //clear existing markers from the map function resetMarkers(arr){ for (var i=0;i<arr.length; i++){ arr[i].setMap(null); } //reset the main marker array for the next call arr=[]; } function bindInfoWindow(marker, map, infoWindow, html) { google.maps.event.addListener(marker, 'click', function() { infoWindow.setContent(html); 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); }