--- title: Geolocation.getCurrentPosition() slug: Web/API/Geolocation/getCurrentPosition tags: - API - Geolocation - Geolocation API - Method - Reference - Secure context - getCurrentPosition - メソッド translation_of: Web/API/Geolocation/getCurrentPosition ---
{{securecontext_header}}{{ APIRef("Geolocation API") }}
Geolocation.getCurrentPosition() メソッドは、デバイスの現在位置を取得するために使われます。
navigator.geolocation.getCurrentPosition(success[, error[, [options]])
successerror {{optional_inline}}options {{optional_inline}}maximumAge: 整数 (ミリ秒) | infinity - キャッシュされた位置の最大寿命です。timeout: 整数 (ミリ秒) - エラーコールバックが呼び出されるまでの時間で、 0 の場合は呼び出されません。enableHighAccuracy: false | truevar options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('Geolocation')}} | {{Spec2('Geolocation')}} | 初回定義 |
{{Compat("api.Geolocation.getCurrentPosition")}}