HTML5 Samples

Geolocation APIで現在地を取得する

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <title>Location API Sample</title>
  </head>
  <body>
    <h1>Location API Sample</h1>
    <div id="message"></div>
    <script>
      window.onload = function() {
          if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function(position) {
                  document.getElementById('message').innerHTML = 'You are at ' + position.coords.latitude + ', ' + position.coords.longitude + ' now';
              }, function(e) {
                  document.getElementById('message').innerHTML = typeof e == 'string' ? e : e.message;
              });
          } else {
              document.getElementById('message').innerHTML = 'Location APIがサポートされていません。';
          }
      };
    </script>
  </body>
</html>