微信小程序的获取位置、导航

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
    })
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值