<view class="wrapper">
<view class="page-body">
<view class="page-body-wrapper">
<form bindsubmit="openLocation">
<view class="page-body-form">
<text class="page-body-form-key">经度</text>
<input class="page-body-form-value" type="text" value="{{location.longitude}}" name="longitude" />
<text class="page-body-form-key">维度</text>
<input class="page-body-form-value" type="text" value="{{location.latitude}}" name="latitude" />
</view>
<text>地址:{{location.address}}</text>
<view class="page-body-buttons">
<button class="page-body-button" type="primary" bindtap="getLocation">获取位置</button>
<button class="page-body-button" type="primary" formType="submit">查看位置</button>
<button class="page-body-button" type="primary" bindtap="chooseLocation">选择位置</button>
</view>
</form>
</view>
</view>
</view>
wxss部分代码
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}
.userinfo-avatar {
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}
.userinfo-nickname {
color: #aaa;
}
.usermotto {
margin-top: 20px;
}
.wrapper{
height: 100%;
background:#fff;
}
.page-body-form-value{
font-size: 14px;
color:darkturquoise;
font-weight: bold;
padding-left: 15px;
border: 1px solid rgb(72, 165, 131);
border-radius: 4px;
height: 30px;
line-height: 30px;
}
.page-body-form-key{
font-size:14px;
padding: 10px;
margin-top:15px;
font-family: "Microsoft Yahei";
font-weight: bold;
height: 30px;
line-height: 30px;
}
.page-body-button{
margin-top:20px;
line-height: 2;
}
js代码
//获取经纬度
getLocation: function (e) {
console.log(e)
var that = this
wx.getLocation({
success: function (res) {
// success
console.log(res)
that.setData({
hasLocation: true,
location: {
longitude: res.longitude,
latitude: res.latitude
}
})
}
})
},
//根据经纬度在地图上显示
openLocation: function (e) {
console.log("openLocation" + e)
var value = e.detail.value
wx.openLocation({
longitude: Number(value.longitude),
latitude: Number(value.latitude)
})
},
//选择位置位置
chooseLocation: function (e) {
console.log(e)
var that = this
wx.chooseLocation({
success: function (res) {
// success
console.log(res),
console.log(res.name)
that.setData({
hasLocation: true,
location: {
longitude: res.longitude,
latitude: res.latitude,
address: res.address
}
})
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
}
最终效果图