地理位置实时JavaScript语言

时间:2015-06-24 22:56:18

标签: javascript jquery html css

我在每200ms刷新一次并更新地图上的位置时遇到问题。有人能告诉我我犯了哪个错误吗?

<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>

1 个答案:

答案 0 :(得分:1)

错误发生在脚本的最后几行(

setInterval(showMaps(), 200);

因为您在没有必需参数的情况下调用此函数。

setInterval(init, 1000)

应该work.pay注意我做(init,1000)而不是(init(),1000)

link(已更新)http://codepen.io/robert-isaev/pen/yNPRgM