<template>
<view>
<map :longitude="longitude" :latitude="latitude" :markers="markers" :polyline="polyline"
style="width: 100%; height: 100vh;"></map>
</view>
</template>
<script>
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
export default {
data() {
return {
longitude: 121.46196, // 经度
latitude: 31.222352, // 纬度
markers: [], // 标记点
polyline: [], // 线路
qqmapsdk: null,
}
},
onReady() {
this.qqmapsdk = new QQMapWX({
key: '5555555555555555' // 必填
});
console.log('进来了开始执行代码');
// https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodDirection 腾讯地图api
this.qqmapsdk.direction({
mode: 'driving',
from: {
latitude: this.latitude,
longitude: this.longitude
},
to: {
latitude: 39.908860,
longitude: 116.397390
},
success: (res) => {
let polyline = [];
//坐标解压(返回的点串坐标,通过前向差分进行压缩)
let list = []
var coors = res.result.routes[0].polyline;
for (var i = 2; i < coors.length; i++) {
coors[i] = coors[i - 2] + coors[i] / 1000000;
list.push(coors[i])
}
console.log('list:',list);
for (var i = 0; i < list.length; i += 2) {
var lat = list[i];
var lng = list[i + 1];
polyline.push({
latitude: lat,
longitude: lng,
});
}
// https://developers.weixin.qq.com/miniprogram/dev/component/map.html#%E5%8A%9F%E8%83%BD%E6%8F%8F%E8%BF%B0
// 微信小程序api
this.polyline = [{
points: polyline,//线的经纬度数组
color: '#FF0000DD',//划线的颜色
width: 4,//线条粗细
dottedLine: false,
arrowLine: true
}]
},
fail: (error) => {
console.log(error)
}
})
},
methods: {
aaa() {
// 获取当前位置
uni.getLocation({
type: 'gcj02',
success: (res) => {
console.log('res:', res);
this.longitude = res.longitude
this.latitude = res.latitude
this.markers.push({
id: 0,
longitude: res.longitude,
latitude: res.latitude,
iconPath: '/static/logo.jpg',
width: 32,
height: 32
})
},
fail(err) {
console.log('err', err);
}
})
},
}
}
</script>
uniapp腾讯地图规划线路
于 2023-07-26 16:17:39 首次发布