Geolocation APIをお試し。
ブラウザがGeolocation APIに対応しているか:geolocation_api_check.html
if (navigator.geolocation) {
document.write('Geolocation APIに対応しています<br />');
}
else {
document.write('Geolocation APIに対応していません<br />');
}
getCurrentPositionで緯度・経度を取得:geolocation_api_getCurrentPosition.html
navigator.geolocation.getCurrentPosition(
successCallback, errorCallback, {timeout:60000}
);
function successCallback(position) {
var location = '緯度:' + position.coords.latitude + ' 経度:' + position.coords.longitude;
document.getElementById("location").innerHTML = location;
}
function errorCallback(error) {
var message = "";
switch (error.code) {
case error.POSITION_UNAVAILABLE:
message = "位置情報を取得できませんでした";
break;
case error.PERMISSION_DENIED:
message = "位置情報の取得許可を取得できませんでした";
break;
case error.TIMEOUT:
message = "位置情報を取得中にタイムアウトしました";
break;
}
document.getElementById("location").innerHTML = message;
}
参考サイト
関連エントリー