Google Maps V3: Updating Markers Periodically

后端 未结 2 1706
温柔的废话
温柔的废话 2020-12-06 15:47

I\'ve followed the PHP/MYSQL tutorial on Google Maps found here.

I\'d like the markers to be updated from the database every 5 seconds or so.

<
相关标签:
2条回答
  • 2020-12-06 16:03

    Please note I have not tested this as I do not have a db with xml handy

    First of all you need to split your load() function into a function that initializes the map & loads the markers on domready and a function that you will use later to process the xml & update the map with. This needs to be done so you do not reinitialize the map on every load.

    Secondly you need to decide what to do with markers that are already drawn on the map. For that purpose you need to add them to an array as you add them to the map. On second update you have a choice to either redraw the markers (rebuild the array) or simply update the existing array. My example shows the scenario where you simply clear the old markers from the screen (which is simpler).

    //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);
        }
    
    0 讨论(0)
  • 2020-12-06 16:09
    setInterval(function() { 
        downloadUrl("conection/cargar_tecnicos.php", function(data) {
    
            var xml = data.responseXML;
             markers = xml.documentElement.getElementsByTagName("marker");
             removeAllMarkers();
            for (var i = 0; i < markers.length; i++) {
                  var name = markers[i].getAttribute("name");
                var fecha = markers[i].getAttribute("fecha");
                var id_android = markers[i].getAttribute("id_android");
                var celular = markers[i].getAttribute("celular");
                var id = markers[i].getAttribute("id");
                var logo = markers[i].getAttribute("logo");
                var type = markers[i].getAttribute("type");
                var point = new google.maps.LatLng(
                        parseFloat(markers[i].getAttribute("lat")),
                        parseFloat(markers[i].getAttribute("lng")));
    
               var html = "<div class='infowindow'>"
                            +"<br/><div style='text-align:center;'><img src="+logo+"><br/>"    
                            +"<b>" + name + "</b></div><br/>"
                            +"<br/><label><b>Celular:</b></label>" + celular+""
                            +"<br/><label><b>Id Android:</b></label>" + id_android+""
                            +"<br/><label><b>Fecha y Hora:</b></label>" + fecha+""
                            +"<br/><br/><div style='text-align:center;'><a><input style=';' id='pop' type='image' value='"+id+"' class='ASD' img src='img/vermas.png' title='Detalles'/></a></div></div>";
                var icon = customIcons[type] || {};
                 marker[i] = new google.maps.Marker({ 
                    position: point,
                    icon: icon.icon,
                    shadow: icon.shadow,
                    title:name
                });
    
                openInfoWindow(marker[i], map, infoWindow, html);   
              marker[i].setMap(map);
            }
        });
    
    },10000);
    }
    function removeAllMarkers(){// removes all markers from map
        for( var i = 0; i < marker.length; i++ ){
                marker[i].setMap(null);
    }
    }
    
    0 讨论(0)
提交回复
热议问题