在index.html中引入js
<script charset="utf-8" src="https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>

跨域
安装: npm install vue-jsonp
然后在main.js里导入挂载:
import { VueJsonp } from 'vue-jsonp' //一定要加个花括号{VueJsonp },不然会报错
Vue.use(VueJsonp)
用颜色区分行政区划
<template>
<div id="container"></div>
</template>
<script>
export default {
name: "Tendmap",
data() {
return {
modelMsg: false,
address: "",
mapList: [],
mapVal: "",
colors: ['#72A1A2','#56564C','#3593C9','#075279','#F37B84','#E27B2D','#9B4401','#B481B3','#C59C42','#EFDDAF'],
url: "https://apis.map.qq.com/ws/district/v1/getchildren",
queryParams: {
id: "420600",
get_polygon: 2,
key: "OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77",
output: "jsonp",
},
};
},
created() {
this.initMap(32.018555, 112.155652);
},
methods: {
initMap(lat, lng) {
let that = this;
if (that.mapVal) {
that.mapVal.destroy();
}
this.$nextTick(() => {
var center = new TMap.LatLng(lat, lng);
var map = new TMap.Map("container", {
center: center,
zoom: 9,
pitch: 43.5,
rotation: 45,
viewMode: "2D",
});
var ps = [];
var colors = that.colors;
that
.$jsonp(that.url, that.queryParams)
.then((res) => {
var resData = res.result[0];
for (var a = 0; a < resData.length; a++) {
console.log(resData);
var resDataPolygon = resData[a].polygon[0];
var path = [];
for (var b = 0; b < resDataPolygon.length; b = b + 2) {
path.push(
new TMap.LatLng(resDataPolygon[b + 1], resDataPolygon[b])
);
}
ps.push(path);
}
var polygon = new TMap.MultiPolygon({
map,
styles: {
polygon0: new TMap.PolygonStyle({
color: colors[0],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon1: new TMap.PolygonStyle({
color: colors[1],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon2: new TMap.PolygonStyle({
color: colors[2],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon3: new TMap.PolygonStyle({
color: colors[3],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon4: new TMap.PolygonStyle({
color: colors[4],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon5: new TMap.PolygonStyle({
color: colors[5],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon6: new TMap.PolygonStyle({
color: colors[6],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon7: new TMap.PolygonStyle({
color: colors[7],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
polygon8: new TMap.PolygonStyle({
color: colors[8],
showBorder: true,
borderColor: "rgba(255,255,255,255)",
borderWidth: 3,
borderDashArray: [5, 5],
}),
},
geometries: [
{
id: "polygon0",
styleId: "polygon0",
paths: ps[0],
},
{
id: "polygon1",
styleId: "polygon1",
paths: ps[1],
},
{
id: "polygon2",
styleId: "polygon2",
paths: ps[2],
},
{
id: "polygon3",
styleId: "polygon3",
paths: ps[3],
},
{
id: "polygon4",
styleId: "polygon4",
paths: ps[4],
},
{
id: "polygon5",
styleId: "polygon5",
paths: ps[5],
},
{
id: "polygon6",
styleId: "polygon6",
paths: ps[6],
},
{
id: "polygon7",
styleId: "polygon7",
paths: ps[7],
},
{
id: "polygon8",
styleId: "polygon8",
paths: ps[8],
},
],
});
})
.catch((err) => {
console.log(err);
});
});
},
},
};
</script>
<style lang="less">
</style>
效果图
