微信小程序地圖實現(xiàn)點擊(marker)氣泡展示(callout)距離,點擊回到當前位置

微信小程序地圖實現(xiàn)點擊氣泡,展示callout,開發(fā)過程中遇到的需求,大致就和共享單車那個差不多,在很多個marker中點擊一個marker,顯示不同顏色的marker,沒有點擊的則展示正常狀態(tài)的marker,不bb了,直接上圖看看:

image.png

image.png

大概就是這樣的,啷個實現(xiàn)呢,來,直接上代碼:
做這個之前先去下載一個這個東西 qqmap-wx-jssdk.js ( 官網(wǎng)下載地址https://lbs.qq.com/qqmap_wx_jssdk/index.html 以及 key
下載好之后,直接上代碼了,沒下載,我文章后面放代碼片斷,開發(fā)者工具直接打開就有了

上菜要分先后,那我先來html(wxml)

<view class="page-section-gap">
    <map id="myMap" 
        style="width: 100%; height: 100vh;"
        latitude="{{latitude}}" 
        longitude="{{longitude}}"
        markers="{{markers}}"
        controls="{{controls}}" 
        scale="{{scale}}" 
        bindmarkertap="bindmarkertap"
        bindcontroltap="controltap"
        bindcallouttap="clickCallout" 
        bindtap="clearMap" 
        polyline="{{polyline}}" 
        show-location
        show-scale show-compass>
    </map>
    <view class="contentBottomBox">
        <view class="location" bindtap="controltap">
            <image src="../../assets/Position.png" class="locationIcon"></image>
        </view>
    </view>
</view>

在上css(wxss)其實可以不上的,這樣為了美觀一點

.location {
  float: right;
  width: 80rpx;
  height: 80rpx;
  background: #FFFFFF;
  border-radius: 10rpx;
  margin-right: 20rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0px 2rpx 8rpx 0px rgba(0, 0, 0, 0.1);
}

.contentBottomBox {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}
.locationIcon {
  width: 42rpx;
  height: 42rpx;
}

最后上大菜了

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    mapKey: 'BURBZ-XE7K3-TS43N-YRIQT-XIMFS-72F4H',
    latitude: '',
    longitude: '',
    distance: '',
    distance: '',
    scale: 16,
    currMaker: {},
    markers: []
  },

  /**
   * 回到自己位置
   */
  controltap() {
    this.mapCtx.moveToLocation()
    this.getMyLocation()
    this.setData({
      scale: 17
    })
  },
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function (options) {
    var _this = this
    // 實例化API核心類
    qqmapsdk = new QQMapWX({
      key: _this.data.mapKey
    })
    this.getMyLocation()
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  // 獲取我的位置
  getMyLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        let arr = [{
          iconPath: '../../assets/location.png',
          width: 25,
          height: 25,
          address: '天府軟件園A1號',
          latitude: 30.55060000,
          longitude: 104.07125900,
          id: 900000000,
          callout: {
            display: 'BYCLICK'
          }
        }, {
          iconPath: '../../assets/location.png',
          width: 25,
          height: 25,
          address: '天府軟件園A3號',
          latitude: 30.53031088,
          longitude: 104.07234101,
          id: 900000001,
          callout: {
            display: 'BYCLICK'
          }
        }, {
          iconPath: '../../assets/location.png',
          width: 25,
          height: 25,
          address: '天府軟件園A2號',
          latitude: 30.52031088,
          longitude: 104.05234101,
          id: 900000002,
          callout: {
            display: 'BYCLICK'
          }
        }]
        that.setData({
          markers: arr
        })
      }
    })
  },
  // marker的點擊事件
  bindmarkertap(e) {
    console.log('e', e)
    let that = this
    let _markers = that.data.markers
    let markerId = parseInt(e.detail.markerId)
    wx.showLoading({
      title: `${markerId}`,
    })
    _markers.forEach(item => {
      console.log('item', item)
      if (parseInt(item.id) === markerId) {
        console.log('item', item)
        that.setData({
          currMaker: item
        })
      }
    })
    let currMaker = that.data.currMaker
    console.log('currMaker', that.data.currMaker)
    qqmapsdk.calculateDistance({
      to: [{
        latitude: currMaker.latitude,
        longitude: currMaker.longitude
      }],
      success: function (res) {
        let destinationDistance = res.result.elements[0].distance
        let distanceKm = `${(destinationDistance)}m` // 轉(zhuǎn)換成m
        let arr = []
        for (let i = 0; i < _markers.length; i++) {
          if (parseInt(_markers[i].id) === markerId) {
            arr.push({
              address: _markers[i].address,
              latitude: _markers[i].latitude,
              longitude: _markers[i].longitude,
              id: _markers[i].id,
              width: 40,
              height: 40,
              iconPath: '../../assets/location_point.png',
              callout: {
                content: `距您${distanceKm}`,
                display: 'ALWAYS',
                color: '#333333',
                bgColor: '#fff',
                padding: 10,
                borderRadius: 10,
                borderColor: '#fff',
                fontSize: 16,
                borderWidth: 5,
                textAlign: 'center',
              },
            })
          } else {
            arr.push({
              address: _markers[i].address,
              latitude: _markers[i].latitude,
              longitude: _markers[i].longitude,
              id: _markers[i].id,
              width: 25,
              height: 25,
              iconPath: '../../assets/location.png',
              callout: {
                display: 'BYCLICK'
              }
            })
          }
        }
        that.setData({
          markers: arr
        })
        wx.hideLoading({
          success: (res) => {
            console.log(arr)
          },
        })
      }
    })
  }
})

大功告成,開飯
還有那個點擊回到當前位置的,上面js里面有,添加一個點擊事件

/**
   * 回到自己位置
   */
  controltap() {
    this.mapCtx.moveToLocation()
    this.getMyLocation()
    this.setData({
      scale: 17
    })
  },
  // 初始化的時候生成定義一下
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  }

最后,也不耽擱大家時間,還要去創(chuàng)建新項目啥的,那么我把代碼片單分享到這里,
https://developers.weixin.qq.com/s/42Z8VYm87Jmt

直接在瀏覽器中打開這個就會跳轉(zhuǎn)到代碼片段里面看效果,我這里使用的加數(shù)據(jù),來展示效果,實際使用的時候用后端返回的數(shù)據(jù)即可,
項目能正常運行,有不足之處,還請大神指點一二。

親,覺得可以點個贊呀

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容