title: Updating Google Map Marker
body: <p>I'm trying to make a Google map that updates a marker as a user's location is updated. I have most of it done, but I'm having one small issue. I want one marker that shows the starting point, which I have working, but the second point should keep moving, and it should allow multiple users to be tracked at once.</p>
  
  <p>I can get this to work for one user (sending GPS coordinates from Android app). It will set the start marker, and as their location changes the marker will move to reflect that. My problems occur when I start tracking a second user. The current position marker for the first user becomes the starting location for the second user. It 'jumps' from the first path to the other (see picture). I know this is partly due to the declaration of the 'marker1' variable at the top, but I've tried many things with no luck. I need to be able to create as many as I need for n number of users so I can't declare a bunch of variables for each one. <img src="https://i.stack.imgur.com/gWo2h.jpg" alt="enter image description here"></p>
  
  <p>You can see in the picture what is happening. This is the moment when a new user triggers the tracking function in the app. Before this second user activates the tracking function, the marker for the first user was moving properly. </p>
  
  <pre><code>    function initialize() {
      var myLatlng = new google.maps.LatLng(39, -86);
      var myOptions = {
          zoom: 6,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
      }       
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
  
      var loc = {};
      var mark = {};
      var markers = {};
      var marker1;
  
      $(function () {
          $(document).ready(function(){
              setInterval(function(){
                  $.ajax({                                      
                      url: 'api.php',                  //the script to call to get data          
                      //data: "",                       
                      dataType: 'json',                //data format      
                      success: function(data){          //on recieve of reply                          
  
  
                          var user_id = data[0];
                          var lati = data[1];              //get id
                          var longi = data[2];           //get name
  
                          var myLatlngt = new google.maps.LatLng(lati, longi);
  
                          if (typeof loc[user_id] === 'undefined') {
                              loc[user_id] = [];
                              }
  
                          //if (typeof markers[user_id] === 'undefined') {
                              //markers[user_id] = [];
                              //}
  
                          if (typeof mark[user_id] === 'undefined') {
                              mark[user_id] = myLatlngt;
                              }
  
                          loc[user_id].push(myLatlngt);
                          //markers[user_id].push(myLatlngt);
  
                          var x;
                          for (x in loc) {
                              var polyline = new google.maps.Polyline({
                                  map: map,
                                  path: loc[x],
                                  strokeColor: "#FF0000",
                                  strokeOpacity: 1.0,
                                  strokeWeight: 2
                                  });
                              polyline.setMap(map);
  
                              ///location variables
                              var start_loc = loc[user_id];
                              var start_marker = start_loc[0]; //start location of given user
                              var current_loc = start_loc[start_loc.length -1]; //last known location of given user
  
                              //set the start marker
                              var marker = new google.maps.Marker({
                                  position: start_marker,
                                  title: user_id
                              });
                              marker.setMap(map); 
  
                              //update the current location marker
                              if (marker1 != null) {
                                  marker1.setPosition(current_loc);
                              }
                              else {
                                  marker1 = new google.maps.Marker({
                                      position: current_loc,
                                      map: map
                                  });
  
                              }                   
                          }
                              //console.log('location :::', x);
                              console.log('Marker: ', mark);
                      } 
                  });
              }, 1000);
          });
      });
  }
  </code></pre>