微信小程序地圖實(shí)現(xiàn)點(diǎn)擊(marker)氣泡展示(callout)距離絮短,點(diǎn)擊回到當(dāng)前位置

微信小程序地圖實(shí)現(xiàn)點(diǎn)擊氣泡,展示callout昨忆,開發(fā)過(guò)程中遇到的需求丁频,大致就和共享單車那個(gè)差不多,在很多個(gè)marker中點(diǎn)擊一個(gè)marker邑贴,顯示不同顏色的marker席里,沒有點(diǎn)擊的則展示正常狀態(tài)的marker,不bb了拢驾,直接上圖看看:

image.png

image.png

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

上菜要分先后,那我先來(lái)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)其實(shí)可以不上的架忌,這樣為了美觀一點(diǎn)

.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({

  /**
   * 頁(yè)面的初始數(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)聽頁(yè)面加載
   */
  onLoad: function (options) {
    var _this = this
    // 實(shí)例化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號(hào)',
          latitude: 30.55060000,
          longitude: 104.07125900,
          id: 900000000,
          callout: {
            display: 'BYCLICK'
          }
        }, {
          iconPath: '../../assets/location.png',
          width: 25,
          height: 25,
          address: '天府軟件園A3號(hào)',
          latitude: 30.53031088,
          longitude: 104.07234101,
          id: 900000001,
          callout: {
            display: 'BYCLICK'
          }
        }, {
          iconPath: '../../assets/location.png',
          width: 25,
          height: 25,
          address: '天府軟件園A2號(hào)',
          latitude: 30.52031088,
          longitude: 104.05234101,
          id: 900000002,
          callout: {
            display: 'BYCLICK'
          }
        }]
        that.setData({
          markers: arr
        })
      }
    })
  },
  // marker的點(diǎn)擊事件
  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)
          },
        })
      }
    })
  }
})

大功告成褐啡,開飯
還有那個(gè)點(diǎn)擊回到當(dāng)前位置的,上面js里面有鳖昌,添加一個(gè)點(diǎn)擊事件

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

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

直接在瀏覽器中打開這個(gè)就會(huì)跳轉(zhuǎn)到代碼片段里面看效果,我這里使用的加數(shù)據(jù)糕档,來(lái)展示效果莉恼,實(shí)際使用的時(shí)候用后端返回的數(shù)據(jù)即可,
項(xiàng)目能正常運(yùn)行速那,有不足之處俐银,還請(qǐng)大神指點(diǎn)一二。

親端仰,覺得可以點(diǎn)個(gè)贊呀

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捶惜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子荔烧,更是在濱河造成了極大的恐慌吱七,老刑警劉巖汽久,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異踊餐,居然都是意外死亡景醇,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門吝岭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)三痰,“玉大人,你說(shuō)我怎么就攤上這事窜管【泼伲” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵微峰,是天一觀的道長(zhǎng)舷丹。 經(jīng)常有香客問(wèn)我,道長(zhǎng)蜓肆,這世上最難降的妖魔是什么颜凯? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮仗扬,結(jié)果婚禮上症概,老公的妹妹穿的比我還像新娘。我一直安慰自己早芭,他們只是感情好彼城,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著退个,像睡著了一般募壕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上语盈,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天舱馅,我揣著相機(jī)與錄音,去河邊找鬼刀荒。 笑死代嗤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的缠借。 我是一名探鬼主播干毅,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼泼返!你這毒婦竟也來(lái)了硝逢?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎趴捅,沒想到半個(gè)月后垫毙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體霹疫,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡拱绑,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丽蝎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片猎拨。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖屠阻,靈堂內(nèi)的尸體忽然破棺而出红省,到底是詐尸還是另有隱情,我是刑警寧澤国觉,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布吧恃,位于F島的核電站,受9級(jí)特大地震影響麻诀,放射性物質(zhì)發(fā)生泄漏痕寓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一蝇闭、第九天 我趴在偏房一處隱蔽的房頂上張望呻率。 院中可真熱鬧,春花似錦呻引、人聲如沸礼仗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)元践。三九已至,卻和暖如春童谒,著一層夾襖步出監(jiān)牢的瞬間卢厂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工惠啄, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留慎恒,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓撵渡,卻偏偏與公主長(zhǎng)得像融柬,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子趋距,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容