标记的实时更新,无需加载页面Google Maps API V3

时间:2015-04-09 04:58:52

标签: javascript html css google-maps google-maps-api-3

我正在使用Google Maps API V3标记php / sql。 我的问题是我希望根据我的sql db中存储的数据实时更新标记,因为我需要它用于我的capstone项目的应急响应系统。但我看到2或3个线程有同样的问题,但当我尝试使用那些代码时没有任何反应。请有人告诉我该怎么做。我差不多连续做了一天,但没有任何反应。

我想要达到的是this

这是我的代码。

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL & Google Maps Example</title>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
    <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      restaurant: {
        icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png'
      },
      bar: {
        icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;
      // Change this depending on the name of your PHP fil
     downloadUrl("poy.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + point;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon
          });
          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() {}


    //]]>

  </script>

  </head>

  <body onload="load()">
  <div id="map" style="width: 700px; height: 500px"></div>
  </body>

</html>

2 个答案:

答案 0 :(得分:0)

这是未经测试但您应该明白这一点。在load()功能中,设置触发下载的时间间隔。

var map, infoWindow, intervalId;

var customIcons = {
    restaurant: {
        icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png'
    },
    bar: {
        icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png'
    }
};

function load() {
    map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
    });

    infoWindow = new google.maps.InfoWindow;

    // Trigger downloadUrl at an interval
    intervalId = setInterval(triggerDownload, 5000);
}

function bindInfoWindow(marker, map, infoWindow, html) {
    google.maps.event.addListener(marker, 'click', function () {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
    });
}

function triggerDownload() {
    // Change this depending on the name of your PHP file
    downloadUrl("poy.php", function (data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
            var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));
            var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + point;
            var icon = customIcons[type] || {};
            var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon
            });
            bindInfoWindow(marker, map, infoWindow, html);
        }
    });
}

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() {}

这将每5秒(5000毫秒)下载一次XML。

如果您想停止此过程,可以致电

clearInterval(intervalId);

警告:您正在使用异步的XMLHttpRequest。这意味着您可以在完成上一个请求之前调用downloadUrl,这可能会导致不必要的行为。

答案 1 :(得分:0)

这段代码非常适合我:

    <script type="text/javascript">
    //<![CDATA[

   var map, infoWindow, intervalId;

var customIcons = {
    restaurant: {
        icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png'
    },
    bar: {
        icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png'
    }
};

function load() {
    map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
    });

    infoWindow = new google.maps.InfoWindow;

    // Trigger downloadUrl at an interval
    intervalId = setInterval(triggerDownload, 1000);
}

function bindInfoWindow(marker, map, infoWindow, html) {
    google.maps.event.addListener(marker, 'click', function () {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
    });
}

function bindInfoWindow(marker, map, infoWindow, html) {
    google.maps.event.addListener(marker, 'click', function () {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
    });
}

var markersArray = [];

function clearOverlays() {
  for (var i = 0; i < markersArray.length; i++ ) {
   markersArray[i].setMap(null);
  }
}

function triggerDownload() {
    clearOverlays();
    // Change this depending on the name of your PHP file
    downloadUrl("poy.php", function (data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
            var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));
            var html = "<b>" + name + "</b> <br/>" + address + "<br/>" + point;
            var icon = customIcons[type] || {};
            var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon
            });
            markersArray.push(marker);
            bindInfoWindow(marker, map, infoWindow, html);
        }
    });
}

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>