--- title: Geolocation slug: Web/API/Geolocation tags: - API - Geolocation API - 参考 - 安全上下文 - 接口 - 高级 translation_of: Web/API/Geolocation --- <div>{{APIRef}}</div> <p><code><strong>Geolocation</strong></code> 接口是一个用来获取设备地理位置的可编程的对象,它可以让Web内容访问到设备的地理位置,这将允许Web应用基于用户的地理位置提供定制的信息。说实话:其实<code><strong>Geolocation</strong></code> 就是用来获取到当前设备的经纬度(位置)</p> <p>带有此接口的对象可以用由 {{domxref("Navigator")}}实现的属性{{domxref("NavigatorGeolocation.geolocation")}} 来获得。 </p> <div class="note"> <p><strong>注意:</strong>出于安全考虑,当一个Web页尝试获取地理位置信息时,会请求用户批准地理位置访问权限。要知道每个浏览器都有自己请求用户批准该权限的策略和方法。</p> </div> <h2 id="属性">属性</h2> <p><em><code>Geolocation</code> 接口不实现,也不继承任何属性。</em></p> <h2 id="方法">方法</h2> <p><em><em><code>Geolocation</code></em> <em>接口不继承任何方法。</em></em></p> <dl> <dt>{{domxref("Geolocation.getCurrentPosition()")}}</dt> <dd>确定设备的位置并返回一个携带位置信息的<em> </em>{{domxref("Position")}} 对象。</dd> <dt>{{domxref("Geolocation.watchPosition()")}}</dt> <dd>注册一个位置改变监听器,每当设备位置改变时,返回一个 <code>long</code> 类型的该监听器的ID值。</dd> <dt>{{domxref("Geolocation.clearWatch()")}}</dt> <dd>取消由 <code>watchPosition()注册的位置监听器。</code></dd> </dl> <h2 id="规范">规范</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>Initial specification.</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{ CompatibilityTable() }}</p> <div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>特性</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>基础支持</td> <td>5</td> <td>{{CompatGeckoDesktop("1.9.1")}}</td> <td>9</td> <td>10.60<br> 15.0取消支持<br> 16.0重新支持</td> <td>5</td> </tr> </tbody> </table> </div> <div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>特性</th> <th>Android</th> <th>Chrome for Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>基础支持</td> <td>{{CompatUnknown()}}</td> <td>{{CompatUnknown()}}</td> <td>{{CompatGeckoMobile("4")}}</td> <td>{{CompatUnknown()}}</td> <td>10.60</td> <td>{{CompatUnknown()}}</td> </tr> </tbody> </table> </div> <h2 id="参见">参见</h2> <ul> <li><a href="/zh-CN/docs/Web/API/Geolocation/Using_geolocation" title="/en-US/docs/WebAPI/Using_geolocation">使用地理位置定位</a></li> </ul>