LoginSignup
5
6

More than 5 years have passed since last update.

navigator.geolocationでの位置情報の利用

Posted at

地図サービス等で位置情報を利用したい。
そんなときは navigator.geolocation を利用することで現在位置の緯度・経度の取得が可能です。

使い方

sample.js
successCallback= function(position)
{
  alert('緯度:' + position.coords.latitude);
  alert('経度:' + position.coords.longitude);
}
errorCallback = function(error) {}

if (navigator.geolocation) {
  /* Geolocation APIを利用できる環境向けの処理 */
  navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
} else {
  /* Geolocation APIを利用できない環境向けの処理 */
}

上記サンプルのような形で現在位置の緯度や経度の取得ができます。

注意点

1) このAPIはSSL環境下でしか位置情報を取得できません。
httpや不正なSSL証明書では使えないのでご注意を!

2) 位置情報の制度は使用したデバイスのGPS制度や通信環境に依存します。

3) デバイス・ブラウザによっては取得時にGPSへのアクセス許可を求めるダイヤログが表示され、許可しないと使用できません。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6