其他分享
首页 > 其他分享> > Geolocation模块管理设备位置信息,用于获取地理位置信息,如经度、纬度等。通过plus.geolocation可获取设备位置管理对象

Geolocation模块管理设备位置信息,用于获取地理位置信息,如经度、纬度等。通过plus.geolocation可获取设备位置管理对象

作者:互联网

方法:

对象:

回调方法:

权限:

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回调函数返回。

参数:

返回值:

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">},</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>

uni-app使用plus注意事项

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。

参数:

返回值:

Number : 用于标识位置信息监听器,可通过clearWatch方法取消监听。

平台支持:

示例:

<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>

uni-app使用plus注意事项

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>

参数:

返回值:

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>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>

uni-app使用plus注意事项

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>

属性:

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>

属性:

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>

属性:

PositionOptions

JSON对象,监听设备位置信息参数

属性:

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模块错误”

常量:

属性:

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>

参数:

返回值:

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>

uni-app使用plus注意事项

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>

参数:

返回值:

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