WXML:
<view class="item">
<view class="map_container">
<map class='map' longitude='{{longitude}}' latitude='{{latitude}}' scale='{{scale}}' markers='{{markers}}'
controls="{{controls}}" bindcontroltap="bindcontroltap" polyline='{{polyline}}' circles="{{circles}}"
bindmarkertap='bindmarkertap' show-location></map>
</view>
<!-- 以下是导航部分 -->
<view class='list-guide'>
<!-- <view class="item-top">
<view class="item-top-title">门店名称:{{cabinetInfo.name}}</view>
</view>
<view class="address-bottom">地址:{{cabinetInfo.province}} {{cabinetInfo.city}} {{cabinetInfo.region}}
{{cabinetInfo.address}}</view>-->
<view bindtap="onGuideTap" data-latitude='{{cabinetInfo.latitude}}' data-longitude='{{cabinetInfo.longitude}}'
data-bankName='{{cabinetInfo.name}}'>
<image src='../../../images/navigation.png' class='list-guide-imgae'></image>
<text class='list-guide-text'>导航</text>
</view>
</view>
</view>
bindcontroltap="bindcontroltap"
回到当前位置的事件data-latitude='{{cabinetInfo.latitude}}' data-longitude='{{cabinetInfo.longitude}}' data-bankName='{{cabinetInfo.name}}'
此处的latitude、longitude、bankName
以及上处都是在data中声明的
WXSS:
/* pages/map/map.wxss */
.map_container {
height: 260px;
width: 100%;
}
.map {
height: 100%;
width: 100%;
}
.list-guide {
display: flex;
flex-direction: row;
justify-content: space-around;
border-top: 1px solid #ededed;
height: 80rpx;
}
.list-guide-imgae {
height: 70rpx;
width: 70rpx;
margin-right: 20px;
vertical-align: middle;
}
.list-guide-text {
vertical-align: middle;
line-height: 90rpx;
font-size: 35rpx;
}
JS:
/**
* 页面的初始数据
*/
data: {
cabinet: null,
orders: null,
date: null,
latitude: "",
longitude: "",
scale: 14, //缩放比例
markers: [],
//controls控件 是左下角圆圈小图标,用户无论放大多少,点这里可以立刻回到当前定位(控件
//(更新一下,即将废弃,建议使用 cover-view 代替))
controls: [{
id: 1,
iconPath: '../../../images/map_position.png',
position: {
left: 15,
top: 260 - 50,
width: 20,
height: 20
},
clickable: true
}],
},
//------------------获取地图
getLocationMap: function () {
var that = this
wx.getLocation({
type: 'gcj02',
success: function (res) {
that.setData({
//当前的经纬度
latitude: res.latitude,
longitude: res.longitude,
markers: [{
//将要去往的地方的描述
latitude: that.data.cabinet.latitude,
longitude: that.data.cabinet.longitude,
callout: {
content: that.data.cabinet.name,
bgColor: "#fff",
padding: "5px",
borderRadius: "2px",
borderWidth: "1px",
borderColor: "#07c160",
}
}]
})
}
})
},
//----当前位置
bindcontroltap(e) {
var that = this;
if (e.controlId == 1) {
that.setData({
latitude: that.data.cabinet.latitude,
longitude: that.data.cabinet.longitude,
scale: 14,
})
}
},
//-------导航
onGuideTap: function (e) {
var lat = Number(e.currentTarget.dataset.latitude);
var lon = Number(e.currentTarget.dataset.longitude);
var bankName = e.currentTarget.dataset.bankname;
wx.openLocation({
type: 'gcj02',
latitude: lat,
longitude: lon,
name: bankName,
scale: 28
})
}