在網(wǎng)上搜索了好久(包括地圖api)都沒有類似的效果(也可能是我自己沒找到吧)队魏,所以寫篇文章記錄下喳魏,主要難點:
1悲柱、都是用maker點標(biāo)記的位置(小車和景點)民晒,怎么實現(xiàn)小車在景點的上方呢芙贫?
2搂鲫、如果小車在景點的上方的時候,除了第一次點擊的是景點坐標(biāo)磺平,后面每次點擊都是小車的坐標(biāo)了魂仍,那怎么實現(xiàn)小車的移動行走呢?
帶著這幾個問題看下面代碼,會更好懂拣挪,不懂的點贊擦酌,評論區(qū)見
現(xiàn)在效果圖:
- 圖一:走過的路線藍色圖標(biāo)顯示,沒走過的按鈕棕色圖標(biāo)顯示
-
圖二:走完全部路線后的效果
-
圖三 :也可以給map加上polyline屬性菠劝,具體實現(xiàn)看效果圖赊舶,也可以走一次出一條線
直接出代碼,不懂的評論區(qū)見
- js
// index.js
// 獲取應(yīng)用實例
const app = getApp()
const util = require('../../utils/util');
// 引入騰訊地圖SDK核心類
var QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
const moveId = 9999
const durationTime = 2000
Page({
data: {
location: {
latitude: 30.561226249180805,
longitude: 114.40526962280273
},
markers: [{
id: 1,
callout: {
content: '群英薈萃',
padding: 10,
borderRadius: 2,
display: 'ALWAYS',
},
latitude: 30.5927057909617,
longitude: 114.41642761230469,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
},
{
id: 2,
callout: {
content: '武漢歡樂谷',
padding: 10,
borderRadius: 2,
display: 'ALWAYS',
},
latitude: 30.59167141444739,
longitude: 114.3951416015625,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
},
{
id: 3,
callout: {
content: '東湖飛鳥世界',
padding: 10,
borderRadius: 2,
display: 'ALWAYS',
},
latitude: 30.573050749535245,
longitude: 114.37625885009766,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
},
{
id: 4,
callout: {
content: '東湖',
padding: 10,
borderRadius: 2,
display: 'ALWAYS',
},
latitude: 30.551322121233678,
longitude: 114.36527252197266,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
},
{
id: 5,
callout: {
content: '廟湖',
padding: 10,
borderRadius: 2,
display: 'ALWAYS',
},
latitude: 30.525596233215623,
longitude: 114.37952041625977,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
},
{
id: 6,
callout: {
content: '風(fēng)箏山',
padding: 10,
borderRadius: 2,
display: 'ALWAYS',
},
latitude: 30.530919389805096,
longitude: 114.42054748535156,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
}, {
id: 7,
callout: {
content: '情人坡',
padding: 10,
borderRadius: 2,
display: 'ALWAYS'
},
latitude: 30.524856882846002,
longitude: 114.44114685058594,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
}, {
id: 8,
callout: {
content: '蝴蝶谷',
padding: 10,
borderRadius: 2,
display: 'ALWAYS'
},
latitude: 30.57260735678382,
longitude: 114.42672729492188,
iconPath: './imgs/location_normal.png',
width: '34px',
height: '34px',
}, {
id: moveId,
latitude: '',
longitude: '',
iconPath: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/car.png',
width: '30px',
height: '30px',
},
],
polyline: [
// {
// points: [{
// latitude: 30.5927057909617,
// longitude: 114.41642761230469,
// }, //群英薈萃
// {
// latitude: 30.59167141444739,
// longitude: 114.3951416015625,
// }, //武漢歡樂谷
// {
// latitude: 30.573050749535245,
// longitude: 114.37625885009766,
// } //東湖飛鳥
// , {
// latitude: 30.551322121233678,
// longitude: 114.36527252197266,
// } //東湖
// , {
// latitude: 30.525596233215623,
// longitude: 114.37952041625977,
// } //廟湖
// , {
// latitude: 30.530919389805096,
// longitude: 114.42054748535156,
// } //風(fēng)箏山
// , {
// latitude: 30.524856882846002,
// longitude: 114.44114685058594,
// } //情人坡
// , {
// latitude: 30.57260735678382,
// longitude: 114.42672729492188,
// }, //蝴蝶谷
// {
// latitude: 30.5927057909617,
// longitude: 114.41642761230469,
// } //群英薈萃
// ],
// color: '#9cc7f9',
// width: 3
// }
],
scale: 12.5, //12.5
isClick: true,
nextId: null,
tempList: [],
mapCtx: null,
startId: 1,
isGo:false
},
onLoad() {
// 實例化API核心類
qqmapsdk = new QQMapWX({
key: app.globalData.TXMapKey,
});
this.data.tempList = this.data.markers
this.data.mapCtx = wx.createMapContext('map', this);
wx.showModal({
title: '提示',
content: '請在地圖上選擇一個地點作為起點赶诊!',
showCancel:false,
success (res) {
if (res.confirm) {
console.log('用戶點擊確定')
}
}
})
},
// 標(biāo)注點擊回調(diào)
onTapMarker(event) {
const version = wx.getSystemInfoSync().SDKVersion;
var id = event.detail.markerId
var length = this.data.markers.length;
var maxId = this.data.markers[length - 2].id;
console.log("maxId==", maxId)
if (util.compareVersion(version, '2.13.0') < 0) {
// 如果希望用戶在最新版本的客戶端上體驗?zāi)男〕绦蛄剑梢赃@樣子提示
wx.showToast({
title: '當(dāng)前微信版本過低,無法使用該功能甫何,請升級到最新微信版本后重試出吹。',
icon: 'none'
});
return;
}
if (this.data.nextId) { //不是首次
// this.runRouterLater()
} else { //首次點擊
this.data.startId = id;
this.runRouter(id)
}
},
/**
* Go按鈕
*/
onGo(){
if (!this.data.nextId) {
wx.showToast({
title: '請在地圖上選擇一個地點作為起點!',
icon: 'none'
});
return;
}
if (!this.data.isClick) {
return;
}
this.data.isClick = false
this.runRouterLater()
},
/**
* 二次之后的點擊
*/
runRouterLater() {
var id = this.data.nextId;
var length = this.data.markers.length;
var maxId = this.data.markers[length - 2].id;
var list = [];
for (var i = 0; i < this.data.markers.length; i++) {
if (id == this.data.markers[i].id) {
this.data.markers[i].iconPath = "./imgs/Marker1_Normal@3x.png"
var list = [];
list.push({
latitude: this.data.markers[i].latitude,
longitude: this.data.markers[i].longitude
})
if (id == maxId) {
list.push({
latitude: this.data.markers[0].latitude,
longitude: this.data.markers[0].longitude
})
this.data.nextId = this.data.markers[0].id
} else {
list.push({
latitude: this.data.markers[i + 1].latitude,
longitude: this.data.markers[i + 1].longitude
})
this.data.nextId = this.data.markers[i + 1].id
}
console.log("move==", id + '->' + this.data.nextId)
this.data.mapCtx.moveAlong({
markerId: moveId,
path: list,
duration: durationTime,
autoRotate: true,
success: () => {
if (this.data.nextId == this.data.startId) {
wx.showToast({
title: '完成此次行程',
icon: 'none',
});
this.setData({
isClick: false,
isGo:true
});
} else {
this.setData({
isClick: true
});
}
},
});
this.setData({
markers: this.data.markers,
});
}
}
},
/**
* 首次點擊
* @param {}} data
*/
runRouter(data) {
var id = data;
var length = this.data.markers.length;
var maxId = this.data.markers[length - 2].id;
var list = [];
for (var i = 0; i < this.data.markers.length; i++) {
if (id == this.data.markers[i].id) {
this.data.markers[i].iconPath = "./imgs/Marker1_Normal@3x.png"
this.data.markers[i].isClick = true
var list = [];
list.push({
latitude: this.data.markers[i].latitude,
longitude: this.data.markers[i].longitude
})
if (id == maxId) {
list.push({
latitude: this.data.markers[0].latitude,
longitude: this.data.markers[0].longitude
})
this.data.nextId = this.data.markers[0].id
} else {
list.push({
latitude: this.data.markers[i + 1].latitude,
longitude: this.data.markers[i + 1].longitude
})
this.data.nextId = this.data.markers[i + 1].id
}
console.log("move==", id + '->' + this.data.nextId)
this.data.mapCtx.moveAlong({
markerId: moveId,
path: list,
duration: durationTime,
autoRotate: true,
success: () => {
this.setData({
isClick: true
});
},
});
this.setData({
markers: this.data.markers,
});
}
}
}
})
- wxml
<map id="map" class="map" markers="{{markers}}" latitude="{{location.latitude}}" longitude="{{location.longitude}}"
scale="{{scale}}" bindmarkertap="onTapMarker" polyline="{{polyline}}" enable-zoom="{{false}}"
enable-scroll="{{false}}" enable-poi="{{false}}"></map>
<view class="button_content">
<button catchtap="onGo" disabled="{{isGo}}" style="height:6vh">Go</button>
</view>
- wxss
.container{
display: flex;
flex-direction: column;
}
.map {
width:100%;
height: 90vh;
}
.button_content{
height: 10vh;
display: flex;
flex-direction: row;
align-items: center;
}