Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:51:00+00:00 2026-06-13T08:51:00+00:00

I have the next code to retrieve some data from a xml file. The

  • 0

I have the next code to retrieve some data from a xml file. The problem is that when I want to refresh the markers, the response that I get is a duplicated marker in the map. How can I update the points without having duplicated markers?

Best regards

//<![CDATA[
  // this variable will collect the html which will eventually be placed in the       side_bar 
  var side_bar_html = ""; 

  // arrays to hold copies of the markers and html used by the side_bar 
  // because the function closure trick doesnt work there 
  var gmarkers = []; 

 // global "map" variable
  var map = null;
  var markerclusterer = null;

// A function to create the marker and set up the event window function 



function createMarker(latlng, imei, html, estado, alias, speed, timestamp) {
if(estado == 1)
  image = '/artworks/icons/truck_green3.png';
else
  image = '/artworks/icons/truck_red.png';
var textoLabel= "this is the text"
var contentString = html;
var marker = new MarkerWithLabel({
    position: latlng,
    icon: image,
    // map: map,
    labelContent: textoLabel,
   labelAnchor: new google.maps.Point(40, 0),
   labelClass: "labels", // the CSS class for the label
   labelStyle: {opacity: 0.50},
    zIndex: Math.round(latlng.lat()*-100000)<<5
    });


google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(contentString); 
    infowindow.open(map,marker);
    });
// save the info we need to use later for the side_bar
gmarkers.push(marker);
// add a line to the side_bar html
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + imei + '<\/a><br>';
}

    // This function picks up the click and opens the corresponding info window
function myclick(i) {
  google.maps.event.trigger(gmarkers[i], "click");
}



function initialize() {
  // create the map
  var myOptions = {
    zoom: 12,
    center: new google.maps.LatLng(37.169619,-3.756981),
    mapTypeControl: true,
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    navigationControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"),
                            myOptions);
}
    function getMarkers() {
  google.maps.event.addListener(map, 'click', function() {
    infowindow.close();
    });
  // Read the data from example.xml
  downloadUrl("vehiculos.asp", function(doc) {
    var xmlDoc = xmlParse(doc);
    var markers = xmlDoc.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      // obtain the attribues of each marker
      var lat = parseFloat(markers[i].getAttribute("lat"));
      var lng = parseFloat(markers[i].getAttribute("lng"));
      var point = new google.maps.LatLng(lat,lng);
      var imei = markers[i].getAttribute("imei");
      var alias = markers[i].getAttribute("alias");
      var speed= markers[i].getAttribute("speed");
      var timestamp= markers[i].getAttribute("timestamp");
      var estado= markers[i].getAttribute("estado");
      var conectado= markers[i].getAttribute("conectado");
var html="<b>"+alias+"</b><br> a una velocidad de "+speed+" km/h <br/>     ultima posicion  a las: "+timestamp;

      // create the marker
      var marker = createMarker(point,alias+" "+imei,html,estado, speed, timestamp );
    }

    markerCluster = new MarkerClusterer(map, gmarkers);
    // put the assembled side_bar_html contents into the side_bar div
    document.getElementById("side_bar").innerHTML = side_bar_html;
  });
}

var infowindow = new google.maps.InfoWindow(
  { 
    size: new google.maps.Size(150,50)
 });

  // Removes the overlays from the map, but keeps them in the array.
  function clearOverlays() {
    getMarkers(null);
  }

  // Deletes all markers in the array by removing references to them.
  function deleteOverlays() {
    clearOverlays();
    getMarkers = [];
  }
setInterval(clearOverlays, 3000);
setInterval(deleteOverlays, 4000);
setInterval(getMarkers, 5000);    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:51:01+00:00Added an answer on June 13, 2026 at 8:51 am

    Give your markers unique ids (maybe one of your existing attributes is already unique). If the unique id already exists, move that marker to the new location (or just don’t add it again),if it doesn’t, create a new marker.

    If they don’t move, you can also avoid duplicates by checking the distance between new markers and all your existing markers, if it is less than a “same marker” threshold (say ~0.1 meters), don’t add it again.

    There is a problem with your clearOverlays method (getMarkers(null) just calls getMarkers):

    // Removes the overlays from the map, but keeps them in the array.
    function clearOverlays() {
      for (var i=0; i<gmarkers.length; i++)
      {
         gmarkers[i].setMap(null);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some relatively simple code I have made that is supposed to retrieve
I have the next code in javascript. I deleted some unnecessary items because it
I have the next code: Process p = Runtime.getRuntime().exec(args); and I want my program
I have the next code that was given to me to split up a
I have to improve some code where an Oracle stored procedure is called from
I have the code as per below. This retrieves some data to use for
im trying to retrieve some data using a reader in vb.net. I dont have
I have a Java JDBC application that uses multiple threads to retrieve information from
Consider a hypothetical case where I have to retrieve some details from the database
I now have a working JSON formatted file from my PHP scripts. The next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.