Skip to content

Instantly share code, notes, and snippets.

@jayluxferro
Created August 19, 2018 12:31
Show Gist options
  • Save jayluxferro/8eabc45e73cd37571471fa4ff80aab60 to your computer and use it in GitHub Desktop.
Save jayluxferro/8eabc45e73cd37571471fa4ff80aab60 to your computer and use it in GitHub Desktop.
Google maps autoupdate
<!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>Drifter</title>

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        //<![CDATA[
        var markers = [];
        setInterval(function () {
            DeleteMarkers();
            BindMarker();
        }, 5000);

        function DeleteMarkers() {
            //Loop through all the markers and remove
            for (var i = 0; i < markers.length; i++) {
                markers[i].setMap(null);
            }


            markers = [];
        };
        
        var customIcons = {
            blue: {
                icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
                shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            red: {
                icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
                shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            }
        };

        var map = null;
        var infoWindow = null;
        function load() {
            map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(-29.86519774, 30.98538962),
                zoom: 18,
                mapTypeId: 'terrain'
            });


            infoWindow = new google.maps.InfoWindow;

            // Change this depending on the name of your PHP file
            BindMarker();
        }

        function BindMarker() {
            downloadUrl("genxml.php", function (data) {
                var xml = data.responseXML;
                var markers = xml.documentElement.getElementsByTagName("marker");

                for (var i = 0; i < markers.length; i++) {

                    var point = new google.maps.LatLng(
                        parseFloat(markers[i].getAttribute("lat")),
                        parseFloat(markers[i].getAttribute("lon")));


                    var icon = customIcons["blue"] || {};
                    var marker = new google.maps.Marker({
                        map: map,
                        position: point,
                        icon: icon.icon,
                        shadow: icon.shadow
                    });

                    markers.push(marker);


                    bindInfoWindow(marker, map, infoWindow);
                }
            });
        }

        function bindInfoWindow(marker, map, infoWindow) {
            google.maps.event.addListener(marker, 'click', function () {

                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.önreadystatechange = 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: 1000px; height: 600px"></div>
  </body>

</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment