编程语言
首页 > 编程语言> > javascript – Google Maps V3:定期更新标记

javascript – Google Maps V3:定期更新标记

作者:互联网

我已经按照谷歌地图上的PHP / MYSQL教程找到了here.

我希望每隔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);
    }

标签:javascript,ajax,google-maps-api-3,google-maps-markers
来源: https://codeday.me/bug/20190927/1823662.html