uni-app实现定位功能

定位功能需要项目支持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来调整地图上标记的位置

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值