aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/geolocation/getcurrentposition/index.html
blob: f99f1cef81acb9d87178a592d27ced6e9fe3c13f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: Geolocation.getCurrentPosition()
slug: Web/API/Geolocation/getCurrentPosition
tags:
  - API
  - Geolocation
  - Geolocation API
  - Method
  - Reference
  - Secure context
  - getCurrentPosition
  - メソッド
translation_of: Web/API/Geolocation/getCurrentPosition
---
<p>{{securecontext_header}}{{ APIRef("Geolocation API") }}</p>

<p><span class="seoSummary"><strong><code>Geolocation.getCurrentPosition()</code></strong> メソッドは、デバイスの現在位置を取得するために使われます。</span></p>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate">navigator.geolocation.getCurrentPosition(<var>success</var>[, <var>error</var>[, [<var>options</var>]])</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><code><var>success</var></code></dt>
 <dd>コールバック関数で、 {{domxref("GeolocationPosition")}} オブジェクトを唯一の入力引数として受け取るものです。</dd>
 <dt><code><var>error</var></code> {{optional_inline}}</dt>
 <dd>任意のコールバック関数で、 {{domxref("GeolocationPositionError")}} オブジェクトを唯一の入力引数として受け取るものです。</dd>
 <dt><code><var>options</var></code> {{optional_inline}}</dt>
 <dd>任意の {{domxref("PositionOptions")}} オブジェクトです。<br>
 オプションには以下のものがあります。
 <ul>
  <li><code>maximumAge</code>: 整数 (ミリ秒) | infinity - キャッシュされた位置の最大寿命です。</li>
  <li><code>timeout</code>: 整数 (ミリ秒) - エラーコールバックが呼び出されるまでの時間で、 0 の場合は呼び出されません。</li>
  <li><code>enableHighAccuracy</code>: false | true</li>
 </ul>
 </dd>
</dl>

<h2 id="Examples" name="Examples"></h2>

<pre class="brush: js notranslate">var 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);
</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
   <th scope="col">状態</th>
   <th scope="col">備考</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('Geolocation')}}</td>
   <td>{{Spec2('Geolocation')}}</td>
   <td>初回定義</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

<p>{{Compat("api.Geolocation.getCurrentPosition")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li><a href="/ja/docs/Web/API/Geolocation_API/Using_the_Geolocation_API">Geolocation の使用</a></li>
 <li>{{domxref("Navigator.geolocation")}}</li>
</ul>