Geolocation模块管理设备位置信息,用于获取地理位置信息,如经度、纬度等。通过plus.geolocation可获取设备位置管理对象
作者:互联网
方法:
- getCurrentPosition: 获取当前设备位置信息
- watchPosition: 监听设备位置变化信息
- clearWatch: 关闭监听设备位置信息
对象:
- Position: JSON对象,设备位置信息数据
- Address: JSON对象,地址信息
- Coordinates: JSON对象,地理坐标信息
- PositionOptions: JSON对象,监听设备位置信息参数
- GeolocationError: JSON对象,定位错误信息
回调方法:
- GeolocationSuccessCallback: 获取设备位置信息成功的回调函数
- GeolocationErrorCallback: 获取设备位置信息失败的回调函数
权限:
5+功能模块(permissions)
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// ...</span>
<span style="color:#ffa0a0">"permissions"</span><span style="color:#ffffff">:{</span>
<span style="color:#87ceeb">// ...</span>
<span style="color:#ffa0a0">"Geolocation"</span><span style="color:#ffffff">:</span> <span style="color:#ffffff">{</span>
<span style="color:#ffa0a0">"description"</span><span style="color:#ffffff">:</span> <span style="color:#ffa0a0">"位置信息"</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
</code></span></span>
getCurrentPosition
获取当前设备位置信息
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentPosition</span><span style="color:#ffffff">(</span><span style="color:#ffffff">successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> option</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
位置信息将通过手机GPS设备或其它信息如IP地址、移动网络信号获取,由于获取位置信息可能需要较长的时间,当成功获取位置信息后将通过successCB回调函数返回。
参数:
- successCB: ( GeolocationSuccessCallback ) 必选 获取设备位置信息成功回调函数
- errorCB: ( GeolocationErrorCallback ) 可选 获取设备位置信息失败回调函数
- options: ( PositionOptions ) 可选 获取设备位置信息的参数
返回值:
void : 无
平台支持:
- Android - 2.2+ (支持) :
支持
- iOS - 4.3+ (支持) :
支持
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#cccccc"><!DOCTYPE html></span>
<span style="color:#f0e68c"><strong><html></strong></span>
<span style="color:#f0e68c"><strong><head></strong></span>
<span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Geolocation Example</span><span style="color:#f0e68c"><strong></title></strong></span>
<span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#87ceeb">// 扩展API加载完毕后调用onPlusReady回调函数 </span><span style="color:#ffffff">
document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'plusready'</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span><span style="color:#ffffff">);</span>
<span style="color:#87ceeb">// 扩展API加载完毕,现在可以正常调用扩展API</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentPosition</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation\nLatitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">latitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nLongitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">longitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nAltitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">altitude</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation error: '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
<span style="color:#f0e68c"><strong></script></strong></span>
<span style="color:#f0e68c"><strong></head></strong></span>
<span style="color:#f0e68c"><strong><body</strong></span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong></body></strong></span>
<span style="color:#f0e68c"><strong></html></strong></span>
</code></span></span>
watchPosition
监听设备位置变化信息
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#98fb98">Number</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchPosition</span><span style="color:#ffffff">(</span><span style="color:#ffffff">successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> option</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
位置信息将通过手机GPS设备或其它信息如IP地址、移动网络信号获取。 当位置信息更新后将通过successCB回调函数返回。 位置信息获取失败则调用回调函数errorCB。
参数:
- successCB: ( GeolocationSuccessCallback ) 必选
设备位置信息更新成功回调函数
- errorCB: ( GeolocationErrorCallback ) 可选
获取设备位置信息失败回调函数
- option: ( PositionOptions ) 可选
监听设备位置信息的参数
返回值:
Number : 用于标识位置信息监听器,可通过clearWatch方法取消监听。
平台支持:
- Android - 2.2+ (支持) :
支持
- iOS - 4.3+ (支持) :
支持
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#cccccc"><!DOCTYPE html></span>
<span style="color:#f0e68c"><strong><html></strong></span>
<span style="color:#f0e68c"><strong><head></strong></span>
<span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Geolocation Example</span><span style="color:#f0e68c"><strong></title></strong></span>
<span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#87ceeb">// 扩展API加载完毕后调用onPlusReady回调函数 </span><span style="color:#ffffff">
document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'plusready'</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span><span style="color:#ffffff">);</span>
<span style="color:#87ceeb">// 扩展API加载完毕,现在可以正常调用扩展API</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchPosition</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation\nLatitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">latitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nLongitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">longitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nAltitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">altitude</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation error: '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
<span style="color:#f0e68c"><strong></script></strong></span>
<span style="color:#f0e68c"><strong></head></strong></span>
<span style="color:#f0e68c"><strong><body</strong></span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong></body></strong></span>
<span style="color:#f0e68c"><strong></html></strong></span>
</code></span></span>
clearWatch
关闭监听设备位置信息
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearWatch</span><span style="color:#ffffff">(</span><span style="color:#ffffff">watchId</span><span style="color:#ffffff">);</span>
</code></span></span>
参数:
- watchId: ( Number ) 必选
需要取消的位置监听器标识,调用watchPosition方法的返回值。
返回值:
void : 无
平台支持:
- Android - 2.2+ (支持) :
支持
- iOS - 4.3+ (支持) :
支持
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#cccccc"><!DOCTYPE html></span>
<span style="color:#f0e68c"><strong><html></strong></span>
<span style="color:#f0e68c"><strong><head></strong></span>
<span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Geolocation Example</span><span style="color:#f0e68c"><strong></title></strong></span>
<span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#87ceeb">// 扩展API加载完毕后调用onPlusReady回调函数</span><span style="color:#ffffff">
document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'plusready'</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span><span style="color:#ffffff">);</span>
<span style="color:#87ceeb">// 扩展API加载完毕,现在可以正常调用扩展API</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> wid </span><span style="color:#ffffff">=</span> <span style="color:#f0e68c"><strong>null</strong></span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
wid </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">watchPosition</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation\nLatitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">latitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nLongitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">longitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nAltitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">altitude</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation error: '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">});</span>
<span style="color:#ffffff">}</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> cancel</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearWatch</span><span style="color:#ffffff">(</span><span style="color:#ffffff">wid</span><span style="color:#ffffff">);</span><span style="color:#ffffff">
wid </span><span style="color:#ffffff">=</span> <span style="color:#f0e68c"><strong>null</strong></span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
<span style="color:#f0e68c"><strong></script></strong></span>
<span style="color:#f0e68c"><strong></head></strong></span>
<span style="color:#f0e68c"><strong><body</strong></span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong><input</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"button"</span> <span style="color:#bdb76b"><strong>value</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"Cancel"</span> <span style="color:#bdb76b"><strong>onclick</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"</span><span style="color:#ffffff">cancel</span><span style="color:#ffffff">();</span><span style="color:#ffa0a0">"</span><span style="color:#f0e68c"><strong>></input></strong></span>
<span style="color:#f0e68c"><strong></body></strong></span>
<span style="color:#f0e68c"><strong></html></strong></span>
</code></span></span>
Position
JSON对象,设备位置信息数据
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">Position</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Coordinates</span><span style="color:#ffffff"> coords</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> coordsType</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Number</span><span style="color:#ffffff"> timestamp</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Address</span><span style="color:#ffffff"> address</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> addresses</span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
</code></span></span>
属性:
- coords: (Coordinates 类型 )地理坐标信息,包括经纬度、海拔、速度等信息
- coordsType: (String 类型 )获取到地理坐标信息的坐标系类型
可取以下坐标系类型: "wgs84":表示WGS-84坐标系; "gcj02":表示国测局经纬度坐标系; "bd09":表示百度墨卡托坐标系,仅百度定位支持; "bd09ll":表示百度经纬度坐标系,仅百度定位支持。
- timestamp: (Number 类型 )获取到地理坐标的时间戳信息
时间戳值为从1970年1月1日至今的毫秒数。
- address: (Address 类型 )获取到地理位置对应的地址信息
获取地址信息需要连接到服务器进行解析,所以会消耗更多的资源,如果不需要获取地址信息可通过设置PositionOptions参数的geocode属性值为false避免获取地址信息。 如果没有获取到地址信息则返回undefined。
平台支持
- Android - 2.3+ (支持) :
使用系统定位模块无法获取位置信息。
- iOS - 5.1+ (支持) :
系统定位模块也支持获取位置信息。
- Android - 2.3+ (支持) :
- addresses: (String 类型 )获取完整地址描述信息
如果没有获取到地址信息则返回undefined。
平台支持
- Android - 2.3+ (支持) :
使用系统定位模块无法获取位置信息。
- iOS - 5.1+ (支持) :
系统定位模块也支持获取位置信息。
- Android - 2.3+ (支持) :
Address
JSON对象,地址信息
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">Address</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> country</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> province</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> city</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> district</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> street</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> streetNum</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> poiName</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> postalCode</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> cityCode</span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
</code></span></span>
属性:
- country: (String 类型 )国家
如“中国”,如果无法获取此信息则返回undefined。
- province: (String 类型 )省份名称
如“北京市”,如果无法获取此信息则返回undefined。
- city: (String 类型 )城市名称
如“北京市”,如果无法获取此信息则返回undefined。
- district: (String 类型 )区(县)名称
如“朝阳区”,如果无法获取此信息则返回undefined。
- street: (String 类型 )街道信息
如“酒仙桥路”,如果无法获取此信息则返回undefined。
- streetNum: (String 类型 )获取街道门牌号信息
如“3号”,如果无法获取此信息则返回undefined。
- poiName: (String 类型 )POI信息
如“电子城.国际电子总部”,如果无法获取此信息则返回undefined。
- postalCode: (String 类型 )邮政编码
如“100016”,如果无法获取此信息则返回undefined。
- cityCode: (String 类型 )城市代码
如“010”,如果无法获取此信息则返回undefined。
Coordinates
JSON对象,地理坐标信息
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">Coordinates</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> latitude</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> longitude</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> altitude</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> accuracy</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> altitudeAccuracy</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> heading</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#f0e68c"><strong>double</strong></span><span style="color:#ffffff"> speed</span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
</code></span></span>
属性:
- latitude: (Number 类型 )坐标纬度值
数据类型对象,地理坐标中的纬度值。
- longitude: (Number 类型 )坐标经度值
数据类型对象,地理坐标中的经度值。
- altitude: (Number 类型 )海拔信息
数据类型对象,如果无法获取此信息,则此值为空(null)。
- accuracy: (Number 类型 )地理坐标信息的精确度信息
数据类型对象,单位为米,其有效值必须大于0。
- altitudeAccuracy: (Number 类型 )海拔的精确度信息
数据类型对象,单位为米,其有效值必须大于0。如果无法获取海拔信息,则此值为空(null)。
- heading: (Number 类型 )表示设备移动的方向
数据类型对象,范围为0到360,表示相对于正北方向的角度。如果无法获取此信息,则此值为空(null)。如果设备没有移动则此值为NaN。
- speed: (Number 类型 )表示设备移动的速度
数据类型对象,单位为米每秒(m/s),其有效值必须大于0。如果无法获取速度信息,则此值为空(null)。
PositionOptions
JSON对象,监听设备位置信息参数
属性:
- enableHighAccuracy: (Boolean 类型 )是否高精确度获取位置信息
高精度获取表示需要使用更多的系统资源,默认值为false。
- timeout: (Number 类型 )获取位置信息的超时时间
单位为毫秒(ms),默认值为不超时。如果在指定的时间内没有获取到位置信息则触发错误回调函数。
- maximumAge: (Number 类型 )获取位置信息的间隔时间
单位为毫秒(ms),默认值为1000(即1秒)。调用plus.geolocation.watchPosition时为更新位置信息的间隔时间。 注意:在不同定位模块下支持范围值可能不同,如百度定位模块的间隔范围为大于等于1秒,如果设置的值小于最小值则使用最小值。
- provider: (String 类型 )优先使用的定位模块
可取以下供应者: "system":表示系统定位模块,支持wgs84坐标系; "baidu":表示百度定位模块,支持gcj02/bd09/bd09ll坐标系; "amap":表示高德定位模块,支持gcj02坐标系。 默认值按以下优先顺序获取(amap>baidu>system),若指定的provider不存在或无效则返回错误回调。 注意:百度/高德定位模块需要配置百度/高德地图相关参数才能正常使用。
平台支持
- Android - 2.2+ (支持)
- iOS - 4.5+ (支持) :
HBuilderX2.4.3+版本支持高德定位模块,默认使用优先级(amap>baidu>system); HBuilderX2.4.2及以前版本不支持高德定位模块,默认使用系统定位模块。
<span style="background-color:#333333"><code> <span style="color:#cccccc"><!DOCTYPE html></span> <span style="color:#f0e68c"><strong><html></strong></span> <span style="color:#f0e68c"><strong><head></strong></span> <span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span> <span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Geolocation Example</span><span style="color:#f0e68c"><strong></title></strong></span> <span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span> <span style="color:#f0e68c"><strong>></strong></span> <span style="color:#87ceeb">// 扩展API加载完毕后调用onPlusReady回调函数 </span><span style="color:#ffffff"> document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'plusready'</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span><span style="color:#ffffff">);</span> <span style="color:#87ceeb">// 扩展API加载完毕,现在可以正常调用扩展API</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">(){</span> <span style="color:#87ceeb">// 使用百度地图地位模块获取位置信息</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentPosition</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">){</span><span style="color:#ffffff"> alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation\nLatitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">latitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nLongitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">longitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nAltitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">altitude</span><span style="color:#ffffff">);</span> <span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff"> alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation error: '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">);</span> <span style="color:#ffffff">},{</span><span style="color:#ffffff">provider</span><span style="color:#ffffff">:</span><span style="color:#ffa0a0">'baidu'</span><span style="color:#ffffff">});</span> <span style="color:#ffffff">}</span> <span style="color:#f0e68c"><strong></script></strong></span> <span style="color:#f0e68c"><strong></head></strong></span> <span style="color:#f0e68c"><strong><body</strong></span> <span style="color:#f0e68c"><strong>></strong></span> <span style="color:#f0e68c"><strong></body></strong></span> <span style="color:#f0e68c"><strong></html></strong></span> </code></span>
- coordsType: (String 类型 )指定获取的定位数据坐标系类型
可取以下坐标系类型: "wgs84":表示WGS-84坐标系; "gcj02":表示国测局经纬度坐标系; "bd09":表示百度墨卡托坐标系; "bd09ll":表示百度经纬度坐标系; provider为"system"时,支持wgs84坐标系,默认使用"wgs84"坐标系; provider为"baidu"时,支持gcj02/bd09/bd09ll坐标系,默认使用"gcj02"坐标系; provider为"amap"时,支持gcj02坐标系,默认使用"gcj02"坐标系。 如果设置的坐标系类型provider不支持,则返回错误。
- geocode: (Boolean 类型 )是否解析地址信息
解析的地址信息保存到Position对象的address、addresses属性中,true表示解析地址信息,false表示不解析地址信息,返回的Position对象的address、addresses属性值为undefined,默认值为true。 如果解析地址信息失败则返回的Position对象的address、addresses属性值为null。
GeolocationError
JSON对象,定位错误信息
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>interface</strong></span> <span style="color:#98fb98">GeolocationError</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>const</strong></span> <span style="color:#98fb98">Number</span><span style="color:#ffffff"> PERMISSION_DENIED </span><span style="color:#ffffff">=</span> <span style="color:#cd5c5c">1</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>const</strong></span> <span style="color:#98fb98">Number</span><span style="color:#ffffff"> POSITION_UNAVAILABLE </span><span style="color:#ffffff">=</span> <span style="color:#cd5c5c">2</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>const</strong></span> <span style="color:#98fb98">Number</span><span style="color:#ffffff"> TIMEOUT </span><span style="color:#ffffff">=</span> <span style="color:#cd5c5c">3</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">Number</span><span style="color:#ffffff"> code</span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>readonly</strong></span><span style="color:#ffffff"> attribute </span><span style="color:#98fb98">String</span><span style="color:#ffffff"> message</span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
</code></span></span>
说明:
这里根据W3C规范定义了定位标准错误码,在此基础上5+ 扩展了错误码,参考 5+ API错误代码中的“Geolocation模块错误”
常量:
- PERMISSION_DENIED: (Number 类型 )访问权限被拒绝
系统不允许程序获取定位功能,错误代码常量值为1。
- POSITION_UNAVAILABLE: (Number 类型 )位置信息不可用
无法获取有效的位置信息,错误代码常量值为2。
- TIMEOUT: (Number 类型 )获取位置信息超时
无法在指定的时间内获取位置信息,错误代码常量值为3。
属性:
- code: (Number 类型 )错误代码
可取值:
- PERMISSION_DENIED - 用户拒绝授权
- POSITION_UNAVAILABLE - 位置服务不可用,如系统定位服务关闭
- TIMEOUT - 定位超时,定位时超过PositionOptions.timeout设置的时间触发,此时通常可以重试
- 其它错误 - 5+ 扩展错误码,参考 5+ API错误代码中的“Geolocation模块错误”
- message: (String 类型 )错误描述信息
详细错误描述信息。
GeolocationSuccessCallback
获取设备位置信息成功的回调函数
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onSuccess</span><span style="color:#ffffff">(</span><span style="color:#ffffff"> position </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// Get Position code.</span>
<span style="color:#ffffff">}</span>
</code></span></span>
参数:
- position: ( Position ) 必选 设备的地理位置信息,参考Position
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#cccccc"><!DOCTYPE html></span>
<span style="color:#f0e68c"><strong><html></strong></span>
<span style="color:#f0e68c"><strong><head></strong></span>
<span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Geolocation Example</span><span style="color:#f0e68c"><strong></title></strong></span>
<span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#87ceeb">// 扩展API加载完毕后调用onPlusReady回调函数 </span><span style="color:#ffffff">
document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'plusready'</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span><span style="color:#ffffff">);</span>
<span style="color:#87ceeb">// 扩展API加载完毕,现在可以正常调用扩展API</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentPosition</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation\nLatitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">latitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nLongitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">longitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nAltitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">altitude</span><span style="color:#ffffff">);</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation info: '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">));</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Gelocation Error: code - '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">code </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'; message - '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
<span style="color:#f0e68c"><strong></script></strong></span>
<span style="color:#f0e68c"><strong></head></strong></span>
<span style="color:#f0e68c"><strong><body</strong></span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong></body></strong></span>
<span style="color:#f0e68c"><strong></html></strong></span>
</code></span></span>
GeolocationErrorCallback
获取设备位置信息失败的回调函数
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onGeolocationError</span><span style="color:#ffffff">(</span> <span style="color:#98fb98">GeolocationError</span><span style="color:#ffffff"> error </span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// Handle error</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> code </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">.</span><span style="color:#ffffff">code</span><span style="color:#ffffff">;</span> <span style="color:#87ceeb">// 错误编码</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> message </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">;</span> <span style="color:#87ceeb">// 错误描述信息</span>
<span style="color:#ffffff">}</span>
</code></span></span>
参数:
- error: ( GeolocationError ) 必选 获取位置操作的错误信息
可通过error.code(Number类型)获取错误编码; 可通过error.message(String类型)获取错误描述信息。
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#cccccc"><!DOCTYPE html></span>
<span style="color:#f0e68c"><strong><html></strong></span>
<span style="color:#f0e68c"><strong><head></strong></span>
<span style="color:#f0e68c"><strong><meta</strong></span> <span style="color:#bdb76b"><strong>charset</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"utf-8"</span><span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong><title></strong></span><span style="color:#ffffff">Geolocation Example</span><span style="color:#f0e68c"><strong></title></strong></span>
<span style="color:#f0e68c"><strong><script</strong></span> <span style="color:#bdb76b"><strong>type</strong></span><span style="color:#ffffff">=</span><span style="color:#ffa0a0">"text/javascript"</span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#87ceeb">// 扩展API加载完毕后调用onPlusReady回调函数 </span><span style="color:#ffffff">
document</span><span style="color:#ffffff">.</span><span style="color:#ffffff">addEventListener</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'plusready'</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>false</strong></span><span style="color:#ffffff">);</span>
<span style="color:#87ceeb">// 扩展API加载完毕,现在可以正常调用扩展API</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> onPlusReady</span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">geolocation</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getCurrentPosition</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">p</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Geolocation\nLatitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">latitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nLongitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">longitude </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'\nAltitude:'</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> p</span><span style="color:#ffffff">.</span><span style="color:#ffffff">coords</span><span style="color:#ffffff">.</span><span style="color:#ffffff">altitude</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Gelocation Error: code - '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">code </span><span style="color:#ffffff">+</span> <span style="color:#ffa0a0">'; message - '</span> <span style="color:#ffffff">+</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">);</span>
<span style="color:#f0e68c"><strong>switch</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">code</span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>case</strong></span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">PERMISSION_DENIED</span><span style="color:#ffffff">:</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'User denied the request for Geolocation.'</span><span style="color:#ffffff">);</span>
<span style="color:#f0e68c"><strong>break</strong></span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>case</strong></span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">POSITION_UNAVAILABLE</span><span style="color:#ffffff">:</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'Location information is unavailable.'</span><span style="color:#ffffff">);</span>
<span style="color:#f0e68c"><strong>break</strong></span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>case</strong></span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">TIMEOUT</span><span style="color:#ffffff">:</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'The request to get user location timed out.'</span><span style="color:#ffffff">);</span>
<span style="color:#f0e68c"><strong>break</strong></span><span style="color:#ffffff">;</span>
<span style="color:#f0e68c"><strong>case</strong></span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">UNKNOWN_ERROR</span><span style="color:#ffffff">:</span><span style="color:#ffffff">
alert</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'An unknown error occurred.'</span><span style="color:#ffffff">);</span>
<span style="color:#f0e68c"><strong>break</strong></span><span style="color:#ffffff">;</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span> <span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
<span style="color:#f0e68c"><strong></script></strong></span>
<span style="color:#f0e68c"><strong></head></strong></span>
<span style="color:#f0e68c"><strong><body</strong></span> <span style="color:#f0e68c"><strong>></strong></span>
<span style="color:#f0e68c"><strong></body></strong></span>
<span style="color:#f0e68c"><strong></html></strong></span>
</code></span></span>
标签:geolocation,String,Geolocation,attribute,Number,信息,获取,readonly 来源: https://blog.csdn.net/qq_40575646/article/details/122812578