Stefan Maier
Stefan Maier

Reputation: 78

Automatic refresh of Google Maps Markers

i hope you can help me with my following problem. since days I´m tinkering on it, but it won´t run...

I´m writing a little web-app based on Google Maps API on wich i can mark my actual position on a map with a modified marker-symbol an so on. Everything works great so far. I receive my geolocation via HTML5-Geolocation function and write the values in to a mysql database. In a separate function (see below) i read out this data an set the marker with my modified symbol on the map.

The next I wan´t to solve is to remove the marker after about 40 minutes automatically from the map. For this I created a separate table on my database which gets filled with the marker-information from the main table. This works so far, I solved it with a php-script and a cron-job. Then the entry from the main table becomes deleted. What makes me insane is to remove the marker after the 40 minutes from the map. I tried several things, from creating arrays with the data of the "to-remove-table", i played around with the setMap-function of Google Maps API, but I´m still on the line.

Here is my code where I read out the data from database and set the marker on the map:

function markPolitesse() {

  var infoWindow_spotted = new google.maps.InfoWindow;

  downloadUrl("phpsqlajax_genxml2.php", function(data) {

  var xml = data.responseXML;
  var markers = xml.documentElement.getElementsByTagName("politesse_spotted");

  for (var i = 0; i < markers.length; i++) {
    var number = markers[i].getAttribute("number");
    var city = markers[i].getAttribute("city");
    var zipcode = markers[i].getAttribute("zipcode");
    var street = markers[i].getAttribute("street");
    var type = markers[i].getAttribute("street");
    var point = new google.maps.LatLng(
      parseFloat(markers[i].getAttribute("lat")),
      parseFloat(markers[i].getAttribute("lng")));
    var html = "<b>" + city + "</b> <br/>" + zipcode + "</b> <br/>" + type + "</b> <br/>";    

    var marker = new google.maps.Marker({
        map: map,
        position: point,
        title: number,
        icon: politessenImage
      });

    bindInfoWindow(marker, map, infoWindow_spotted, html);
     }
  });
}

The main- and the "remove-table" contain the following fields: number, city, zipcode, street, streetnumber, coordinates_lat, coordinates_lng, time, date

I hope you have a impulse for me I would be thankful...

Regards, Stefan

Upvotes: 0

Views: 1838

Answers (1)

David Nguyen
David Nguyen

Reputation: 8528

Store the marker in a global variable/outside the function, then in your function do the following:

marker.setMap(null);
// set up new marker

Then assign your new marker.

Upvotes: 1

Related Questions