小程序定位及按钮背景图片
使用了2中方式的定位
第一种是,点击定位按钮,自动获取位置信息返回到小程序页面上。位置准确率不高,有偏差
第二种,点击定位按钮,跳转到地图选点,选点后返回数据到原来的页面。位置准确度高,但是可以随意选点。
以下代码还涉及到图片按钮的写法,我使用的是给按钮设置透明,加载按钮背景图片。小程序使用的图片必须是网页图片,我使用的是云开发里面存放的一个小图标。
<view class="content">
<view class="demo-text-1" >*</view>
<view class="demo-text-2" > 位置:</view>
<view class="demo-text-3" >
<text bindtap="getUserLocation" >{{address}}</text>
</view>
<view class="demo-text-4" style=" background- color: red;" >
<button class="imgtb"style="background-image:url(https://7179-qyww-umgn9-1301480214.tcb.qcloud.la/img/marker_red.png?sign=4c58fdb6ffde9ef6045dba095df1b78e&t=1583974406);" bindtap="getditu" plain="true"></button>
</view>
</view>
button按钮的样式
button.imgtb{
height: 40rpx;
width: 40rpx;
border:none;
background-repeat:no-repeat;
background-size: 40rpx 40rpx;
background-position:10%;
}
地图的2种实现方式
首先data里面进行数据初始化
data: {
address:'点击获取位置信息',
然后,事件写法
getditu:function(e){
var me = this;
// 地理位置
wx.getLocation({
type: 'wgs84',
success(res) {
console.log(res)
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
// 调用接口转换成具体位置
demo.reverseGeocoder({
location: {
latitude: res.latitude,
longitude: res.longitude
},
success: function (res) {
console.log('位置获取成功',res.result)
me.setData({
address:res.result.address
})
},
fail: function (res) {
console.log(res);
},
})
}
})
},
/**
*获取用户当前地理位置
*/
getUserLocation() {
var that = this
wx.chooseLocation({
success: function (res) {
console.log(res)
that.setData({
latitude2: res.latitude,
longitude2: res.longitude,
name: res.name,
address: res.address
})
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},

浙公网安备 33010602011771号