定位功能需要项目支持https协议,不能是http协议,这是定位功能的前提,即使使用高德或是百度的sdk也一样。
uni-app的内置浏览器也能够实现定位功能。
方法1:可以直接使用uni-app的官方接口
uni.getLocation()
示例如下:
uni.getLocation({
type: 'gcj02', // gcj02 返回国测局坐标
success: function(res) {
console.log(res);
this.latitude = res.latitude;
this.longitude = res.longitude;
},
});
方法2:使用map组件的show-location属性
<<!-- HTML -->>
<map id="popMap" :style="{width: divWidth,height:divHeight}"
:latitude="latitude"
:longitude="longitude"
show-location
scale="8"
:markers="covers"
@regiοnchange="regionChange">
<cover-view class="auto-location-button" @tap="onAutoLocation">
<!-- 自动定位 -->
<view class="iconfont icon-dingwei">
</view>
</cover-view>
</map>
// js方法
onAutoLocation() {
// map组件的定位功能,配合show-location使用
const mapContext = uni.createMapContext('map', this);
mapContext.moveToLocation();
},
这种方法,会直接生成蓝色的定位标志,上面的方法可以根据自己的markers来调整地图上标记的位置