uni-app获取当前位置
<p>uniapp获取当前城市:</p><p>官方api:uni.getLocation()<br>获取当前的地理位置、速度。 在微信小程序中,当用户离开应用后,此接口无法调用,除非申请后台持续定位权限;当用户点击“显示在聊天顶部”时,此接口可继续调用。</p>
<p>例:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uni.getLocation({
type: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wgs84</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
success: function (res) {
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">当前位置的经度:</span><span style="color: rgba(128, 0, 0, 1)">'</span> +<span style="color: rgba(0, 0, 0, 1)"> res.longitude);
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">当前位置的纬度:</span><span style="color: rgba(128, 0, 0, 1)">'</span> +<span style="color: rgba(0, 0, 0, 1)"> res.latitude);
}
});</span></pre>
</div>
<p> </p>
<p>成功回调函数中会返回当前经纬度等信息</p>
<p>如果想获取当前省市区信息,可以设置参数 geocode 为 true,该属性仅APP端支持</p>
<p>例:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uni.getLocation({
type: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wgs84</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,<br> geocode:true,
success: function (res) {
console.log(</span><span style="color: rgba(0, 0, 0, 1)">res.address);</span><span style="color: rgba(0, 0, 0, 1)">
}
});</span></pre>
</div>
<p> </p>
<p> APP端还可使用 plus.geolocation 获取中文地址</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">plus.geolocation.getCurrentPosition( function(position){
console.log( position.addresses);
}, function ( e ) {
console.log( e.message );
},{geocode:</span><span style="color: rgba(0, 0, 255, 1)">true</span>});</pre>
</div>
<p>其他端可使用地图开放平台获取SDK:</p>
<p>https://ask.dcloud.net.cn/article/35070</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/angenstern/p/11647549.html
頁:
[1]