@Vue引入腾讯地图,绘制地图多边形
<template>
<div class="tmap">
<div class="btn">
<button @click="add">添加button>
<button @click="edit">编辑button>
<button @click="del">删除button>
div>
<div class="map" id="container" ref="tmap" style="width:100%;height:800px">div>
div>
template>
<script>
let editor, TMap, map, MultiPolygon, id=0;
export default {
name: "TMap",
data() {
return {
map: {},
center: {},
TMap: {},
marker: {},
keywords: "",
paths: [
[{"lat": 28.159730931106623, "lng": 112.99104925198526},{"lat": 28.155023400813775, "lng": 112.99139234751101},{"lat": 28.155817456440566, "lng": 112.999133443544},{"lat": 28.160222468268145, "lng": 112.99836147811993}],
],
};
},
props: {
tMapKey: {
default: "OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77",
type: String
}
},
mounted() {
this.loadScript(() => {
this.initTMap();
});
},
methods: {
initTMap() {
let tMapDom = document.getElementById("container");
this.TMap = window.TMap;
TMap = this.TMap;
let defaultcenter = new this.TMap.LatLng(28.156914, 112.996646);
this.map = new this.TMap.Map(tMapDom, {
center: defaultcenter,
zoom: 16,
pitch: 43.5,
rotation: 45
});
map = this.map;
this.initGeometry();
},
mapClick() {
this.map.on("click", event => {
window.console.log(event);
this.setMapCenter(event.latLng);
if (this.marker) {
this.marker.setMap(null);
this.marker = null;
}
let poi = event.poi;
if (poi) {
info
.setContent(poi.name)
.setPosition(poi.latLng)
.open();
} else {
info.close();
}
});
},
del(){
editor.delete();
},
add() {
editor.setActionMode(TMap.tools.constants.EDITOR_ACTION.DRAW);
},
edit() {
editor.setActionMode(TMap.tools.constants.EDITOR_ACTION.INTERACT);
},
pathElems(geometry){
var lngLat = [];
for (const item of geometry.paths) {
const lng = item.getLng();
const lat = item.getLat();
lngLat.push({
lat: lat,
lng: lng
});
}
return lngLat;
},
dataList(pathArr){
var pathList = [];
if(pathArr){
pathArr.forEach(item => {
var simplePath = [];
item.forEach( items => {
simplePath.push(new TMap.LatLng(items.lat, items.lng));
})
id = id + 1;
var path = {
id: id,
paths: simplePath,
styleId: 'highlight'
}
pathList.push(path);
});
console.log("pathList", pathList);
}
return pathList;
},
MultiPolygon(simplePath){
MultiPolygon = new TMap.MultiPolygon({
map,
styles: {
highlight: new TMap.PolygonStyle({
color: 'rgba(202, 67, 58, 0.1)',
showBorder: true,
borderColor: 'rgba(202, 67, 58, 0.8)',
}),
highlights: new TMap.PolygonStyle({
color: 'rgba(202, 67, 58, 0.3)',
showBorder: true,
borderColor: 'rgba(202, 67, 58, 1)',
})
},
geometries: simplePath
});
return MultiPolygon;
},
initGeometry() {
var pathArr = this.paths;
var simplePath = this.dataList(pathArr);
var overlay = this.MultiPolygon(simplePath);
editor = new TMap.tools.GeometryEditor({
map,
overlayList: [
{
overlay: overlay,
id: 'polygon',
drawingStyleId: 'highlight',
selectedStyleId: 'highlights'
}
],
actionMode: TMap.tools.constants.EDITOR_ACTION.INTERACT,
activeOverlayId: 'polygon',
selectable: true,
snappable: true
});
this.evtResult();
},
evtResult() {
editor.on('draw_complete', (geometry) => {
var lngLat = [this.pathElems(geometry)];
MultiPolygon.remove(geometry.id);
MultiPolygon.add(this.dataList(lngLat));
editor.setActionMode(TMap.tools.constants.EDITOR_ACTION.INTERACT);
});
let evtList = ['delete', 'adjust', 'split', 'union'];
evtList.forEach(evtName => {
editor.on(evtName + '_complete', evtResult => {
console.log(evtName, evtResult);
});
});
editor.on('split_fail', (res) => {
alert(res.message);
});
editor.on('union_fail', (res) => {
alert(res.message);
});
},
createMarker(defaultcenter) {
this.marker = new this.TMap.MultiMarker({
id: "marker-layer",
map: this.map,
styles: {
marker: new this.TMap.MarkerStyle({
width: 25,
height: 35,
anchor: { x: 16, y: 32 },
src:
"https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png"
})
},
geometries: [
{
id: "demo",
styleId: "marker",
position: defaultcenter,
properties: {
title: "marker"
}
}
]
});
},
change2D() {
this.map.setViewMode("2D");
},
change3D() {
this.map.setViewMode("3D");
this.map.setPitch(70);
},
getMapCenter() {
window.console.log(this.map.getCenter());
this.setMapCenter(this.map.getCenter());
},
setMapCenter(center) {
window.console.log(center);
this.map.easeTo(
{
center: new this.TMap.LatLng(center.getLat(), center.getLng()),
zoom: 17,
rotation: 90
},
{ duration: 2000 }
);
},
loadScript(callback) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = `https://map.qq.com/api/gljs?v=1.exp&key=${this.tMapKey}&callback=init&libraries=tools`;
document.body.appendChild(script);
if (script.readyState) {
script.onreadystatechange = function() {
if (
script.readyState == "complete" ||
script.readyState == "loaded"
) {
script.onreadystatechange = null;
callback && callback();
}
};
} else {
script.onload = function() {
callback && callback();
};
}
}
},
destroyed() {
this.map.destroy();
}
};
script>
<style>
style>