English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Navigator geolocation 属性

JavaScript Navigator オブジェクト

geolocation読み取り専属性は、ユーザーの位置を特定するために使用できるGeolocationオブジェクトを返します。

プライバシー上の理由から、ユーザーが位置情報の報告を許可する必要があります。

注意:この機能は、一部またはすべてのサポートブラウザのセキュリティコンテキスト(HTTPS)でのみ利用可能です。

私たちのHTML5地理位置ガイドラインで地理位置の詳細情報について学びます。

语法:

navigator.geolocation
<script> 
var x = document.getElementById("demo");
function getLocation () {
navigator.geolocation.getCurrentPosition(showLoc);
x.innerHTML = '位置情報を取得中...';
}
function showLoc (pos) {
x.innerHTML = "緯度: " + pos.coords.latitude +
  "<br>経度: " + pos.coords.longitude;
}
</script>
テストを見て‹/›

ブラウザの互換性

テーブルの数字は、geolocation属性を完全にサポートする最初のブラウザのバージョンを指定しています:

属性
geolocation53.51659

技術的詳細

返り値:地理位置オブジェクトへの参照

さらに詳しい例

この例では、すべてのNavigator属性を表示します:

var txt = "";
txt += "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt += "<p>Browser Name: " + navigator.appName + "</p>";
txt += "<p>Browser Version: " + navigator.appVersion + "</p>";
txt += "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt += "<p>Browser Language: " + navigator.language + "</p>";
txt += "<p>Browser Online: " + navigator.onLine + "</p>";
txt += "<p>Platform: " + navigator.platform + "</p>";
txt += "<p>User-agent header: " + navigator.userAgent + "</p>";
document.write(txt);
テストを見て‹/›

以下の例では、返された緯度と経度がGoogleマップ上に位置を表示するために使用されます:

<script>
function showLoc(pos) {
var latt = pos.coords.latitude;
var long = pos.coords.longitude;
var lattlong = new google.maps.LatLng(latt, long);
var options = {
center: lattlong,
zoom: 10,
mapTypeControl: true,
navigationControlOptions: {style:google.maps.NavigationControlStyle.SMALL}
};
var mapg = new google.maps.Map(x, options);
var mark = new google.maps.Marker({position:lattlong, map:mapg, title:"You are here!"});
}
</script>
テストを見て‹/›

地図上に位置を表示することは非常に楽しいタスクです。このサービスは地図内の正確な位置を提供するために使用されます。

地図上に結果を表示するには、地図サービスにアクセスする必要があります。例えばGoogle Mapsを使用します。

地図の機能はGoogleに位置するJavaScriptライブラリによって提供されています:

  1. < script src = “ https://maps.googleapis.com/maps/api/js?key= YOUR_KEY ” > </ script >

関連参考

Navigator 参考情報:navigator.appCodeName属性

Navigator 参考情報:navigator.appname属性

Navigator 参考情報:navigator.appVersion属性

Navigator 参考情報:navigator.language属性

Navigator 参考情報:navigator.onLine属性

Navigator 参考情報:navigator.platform属性

Navigator 参考情報:navigator.userAgent属性

JavaScript Navigator オブジェクト