0

So, I'm trying to update the markers/users position every one five second in order for the user to be updated live with their position. I can get the current positions and coordinates to update with the right interval but then I want the marker on the Google Map to update, I cannot understand where to put in my coordinates in the Google Maps code in order to live update?

    var x=document.getElementById("demo");

    function startGeolocation() {
      var options;
      navigator.geolocation.getCurrentPosition(geoSuccess, options);
      setTimeout(startGeolocation, 1000);
    }

    function geoSuccess(position) {
      var gpsPosition = position;
      var coordinates = gpsPosition.coords;
      myLat = coordinates.latitude;
      myLong = coordinates.longitude;
      //x.innerHTML="Latitude: " + myLat + 
      //"<br>Longitude: " + myLong;
      }

     var map;

     function initialize() {
     var mapOptions = {
     zoom: 18,
     mapTypeId: google.maps.MapTypeId.ROADMAP
     };
     map = new google.maps.Map(document.getElementById('map-canvas'),
     mapOptions);

   // Try HTML5 geolocation
   if(navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(function(position, options) {
   //trying to pass in myLat and myLong from the live update above
   var pos = new google.maps.LatLng(position.coords.myLat,
                                   position.coords.myLong);

  var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'You are here'
  });

  map.setCenter(pos);
  }, function() {
  handleNoGeolocation(true);
 });
 } else {
// Browser doesn't support Geolocation
 handleNoGeolocation(false);
 }
 }

 function handleNoGeolocation(errorFlag) {
 if (errorFlag) {
 var content = 'Error: The Geolocation service failed.';
 } else {
 var content = 'Error: Your browser doesn\'t support geolocation.';
 }

 var options = {
 map: map,
 position: new google.maps.LatLng(60, 105),
 content: content
 };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
 }

 google.maps.event.addDomListener(window, 'load', initialize);
thegrinner
  • 11,546
  • 5
  • 41
  • 64
Chilibiff
  • 119
  • 1
  • 13

1 Answers1

0

You are using the wrong function. You should be using setInterval();

setTimeout(expression, timeout); runs the code/function once after the timeout.

setInterval(expression, timeout); runs the code/function in intervals, with the length of the timeout between them.

https://stackoverflow.com/a/2696711/2332336

Community
  • 1
  • 1
Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Thanks but that doesn't solves the problem where I should put in my coordinates in the Google Maps Javascript code, anyone know? – Chilibiff Oct 16 '13 at 14:16