alessandrofranchini
alessandrofranchini

Reputation: 23

Geoposition realtime JavaScript Language

I'm having a problem getting this to refresh every 200ms and update the position on the map. Could someone show me where I made a mistake?

<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
function init() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showMaps, getError);
  }
  else {
    alert("Geolocalizzazione non supportata dal browser");
  }
}

function getError(error) {
  switch(error.code) {
    case 0:
      document.getElementById("status").innerHTML = "Errore sconosciuto";
      break;
    case 1:
      document.getElementById("status").innerHTML = "Richiesta rifiutata     dall'utente";
      break;
    case 2:
      document.getElementById("status").innerHTML = "La posizione non può essere determinata!";
      break;
    case 3:
      document.getElementById("status").innerHTML = "Timeout Scaduto";
      break;
  }
}

function showMaps(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  var punto = new google.maps.LatLng(latitude,longitude);
  var punto2 = new google.maps.LatLng(43.771426,11.254018999999971);
  var punto3 = new google.maps.LatLng(43.7748248,11.234526599999981);

  options = {
    zoom: 13,
    center: punto,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  map_div = document.getElementById("my_map");
  map = new google.maps.Map(map_div, options);

  marker = new google.maps.Marker({
    position: punto,
    map: map,
    title: "Posizione Geolocalizzata"
  });

  marker2 = new google.maps.Marker({
    position: punto2,
    map: map,
    title: "Posizione Host1"
  });

  marker3 = new google.maps.Marker({
    position: punto3,
    map: map,
    title: "Posizione Host2"
  });
}

setInterval(showMaps(), 200);
</script>

<head>
<body onload="init();">

<p id="status"></p>
<div id="my_map"></div>

</body>

Upvotes: 2

Views: 91

Answers (1)

Robert Isaev
Robert Isaev

Reputation: 121

error happens in the last lines of the script, where you do

setInterval(showMaps(), 200);

because you call this function without required arguments.

setInterval(init, 1000)

should work.pay attention that i do (init, 1000) instead of (init(), 1000)

link (updated) http://codepen.io/robert-isaev/pen/yNPRgM

Upvotes: 1

Related Questions