还是在HistoryMap.vue中实现最初的设计。
Vue适合搭建交互简单的前端工程,像是整合leaflet这样,涉及到地图交互,组件之间的数据通信,挺麻烦的,最好还是用原生的来写。
不管如何,我们把这个工程完成吧。
因为通信比较复杂,所以取消了drawer抽屉移出属性table,直接用的leaflet的bindPopup。
文件结构长这样。Icon文件夹中是点图标,一个红色,一个黑色。
所有内容都写在HistoryMap.vue中,其他的没有变化。
主要注意三点。
一是,每次重新加载的时候,对全部覆盖物会有一个移除的过程。
二是,地图加载覆盖物,后加载的会压在之前加载的上面,所以程序里,做了三次循环,先加载polygon,再加载point。
三是,覆盖物会横跨几个朝代,所以为了避免重复加载,用数组cntyindexlist[]、prefptsindexlist[]、prefpgnindexlist[]记录geometry对象的唯一索引gid,先判断geometry对象有没有被加载过,没有被加载过,再加载。
import { treedata } from '../js/treedata.js'
import 'leaflet/dist/leaflet.css'
import 'leaflet/dist/leaflet.js'
import { TXTileLayer } from '../js/txTileLayer.js'
import axios from 'axios'
export default {
data() {
return {
map: null,
defaultProps: {
children: 'children',
label: 'label'
},
treedata: treedata
};
},
methods: {
initLeaflet() {
this.map = L.map('map', {
center: [36.0, 114.0],
zoom: 4,
maxZoom: 23,
minZoom: 3
})
//按照新定义瓦片规则加载底图
let txurl = 'http://rt1.map.gtimg.com/realtimerender/?z={z}&x={x}&y={y}&type=vector&style=1&v=1.1.1'
let getUrlArgs = function(tilePoint) {
return {
z: tilePoint.z,
x: tilePoint.x,
y: Math.pow(2, tilePoint.z) - 1 - tilePoint.y
}
}
let options = {
subdomain: '012',
getUrlArgs: getUrlArgs
}
const txMap = new TXTileLayer(txurl, options)
txMap.addTo(this.map)
},
changecheck(e, data) {
if (e['level'] === 1) {
e['ischeck'] = !e['ischeck'];
for (var i = 0; i < e['children'].length; i++) {
e['children'][i]['ischeck'] = e['ischeck'];
for (var j = 0; j < e['children'][i]['children'].length; j++) {
e['children'][i]['children'][j]['ischeck'] = e['ischeck'];
}
}
} else if (e['level'] === 2) {
e['ischeck'] = !e['ischeck'];
e['children'][0]['ischeck'] = e['ischeck'];
e['children'][1]['ischeck'] = e['ischeck'];
e['children'][2]['ischeck'] = e['ischeck'];
} else {
e['ischeck'] = !e['ischeck'];
}
// console.log(e);
this.getdata();
},
getdata() {
//这步是把除底图外的所有覆盖物清空。
for (var a in this.map._layers) {
if (!this.map._layers[a]._container) {
this.map.removeLayer(this.map._layers[a])
}
};
var urlbase = 'http://localhost:8081/history/geometry?'
var dynastylist = this.treedata[0]['children']
//定义黑点
var blackIcon = L.icon({
iconUrl: require('../icon/black.png'), //icon图片
iconSize: [8, 8], //icon的尺寸
iconAnchor: [4, 4] //锚点在icon上的坐标,左下角为原点
});
//定义红点
var redIcon = L.icon({
iconUrl: require('../icon/red.png'), //icon图片
iconSize: [12, 12], //icon的尺寸
iconAnchor: [6, 6] //锚点在icon上的坐标,左下角为原点
});
var blackpointlist = [];
var cntyindexlist=[];
var redpointlist = [];
var prefptsindexlist=[];
var polygonlist = [];
var prefpgnindexlist=[];
//prefpgn
for (var i = 0; i < dynastylist.length; i++) {
if (dynastylist[i]['children'][2]['ischeck'] == true) {
var url = urlbase + 'category=prefpgn&start=' + dynastylist[i]['timerange'][0] + '&end=' + dynastylist[i]['timerange'][1];
var dynastypolygon = [];
var polygonindex = 0;
axios.get(url).then((response) => {
var prefpgnlist = response.data['list']
for (var j = 0; j < prefpgnlist.length; j++) {
if(prefpgnindexlist.indexOf(prefpgnlist[j]['gid'])>0){
continue;
}
prefpgnindexlist.push(prefpgnlist[j]['gid']);
var mutlipolygon = prefpgnlist[j]['geometry']['coordinates'];
for (var m = 0; m < mutlipolygon.length; m++) {
var polygonpath = [];
var polygon = mutlipolygon[m];
for (var n = 0; n < polygon.length; n++) {
var ringpath = [];
var ring = polygon[n];
for (var p = 0; p < ring.length; p++) {
ringpath.push([ring[p][1], ring[p][0]])
}
polygonpath.push(ringpath);
}
dynastypolygon[polygonindex] = L.polygon(polygonpath, {
color: '#C00000',
fillColor: '#C00000',
fillOpacity: 0.5
}).addTo(this.map);
dynastypolygon[polygonindex].bindTooltip(prefpgnlist[j]['namech']);
dynastypolygon[polygonindex].bindPopup('namech:' + prefpgnlist[j]['namech'] +
'
nameft:' + prefpgnlist[j]['nameft'] +
'
namepy:' + prefpgnlist[j]['namepy'] +
'
typech:' + prefpgnlist[j]['typech'] +
'
presloc:' + prefpgnlist[j]['presloc'] +
'
begyr:' + prefpgnlist[j]['begyr'] +
'
begrule:' + prefpgnlist[j]['begrule'] +
'
begchgty:' + prefpgnlist[j]['begchgty'] +
'
endyr:' + prefpgnlist[j]['endyr'] +
'
endrule:' + prefpgnlist[j]['endrule'] +
'
endchgty:' + prefpgnlist[j]['endchgty'] +
'
geosrc:' + prefpgnlist[j]['geosrc'] +
'
compiler:' + prefpgnlist[j]['compiler'] +
'
gecomplr:' + prefpgnlist[j]['gecomplr'] +
'
checker:' + prefpgnlist[j]['checker']
);
polygonindex = polygonindex + 1;
polygonlist.push(dynastypolygon[polygonindex]);
}
};
}).catch((response) => {
console.log(response)
})
}
}
// console.log(prefpgnindexlist);
//prefpts
for (var i = 0; i < dynastylist.length; i++) {
if (dynastylist[i]['children'][1]['ischeck'] == true) {
var url = urlbase + 'category=prefpts&start=' + dynastylist[i]['timerange'][0] + '&end=' + dynastylist[i]['timerange'][1];
var dynastypoint = [];
axios.get(url).then((response) => {
var prefptslist = response.data['list']
for (var j = 0; j < prefptslist.length; j++) {
if(prefptsindexlist.indexOf(prefptslist[j]['gid'])>0){
continue;
}
prefptsindexlist.push(prefptslist[j]['gid']);
dynastypoint[j] = new L.marker([prefptslist[j]['geometry']['coordinates'][1], prefptslist[j]['geometry']['coordinates'][0]], { icon: redIcon, title: prefptslist[j]['namech'] });
dynastypoint[j].addTo(this.map);
redpointlist.push(dynastypoint[j]);
dynastypoint[j].bindPopup('namech:' + prefptslist[j]['namech'] +
'
nameft:' + prefptslist[j]['nameft'] +
'
namepy:' + prefptslist[j]['namepy'] +
'
typech:' + prefptslist[j]['typech'] +
'
presloc:' + prefptslist[j]['presloc'] +
'
begyr:' + prefptslist[j]['begyr'] +
'
begrule:' + prefptslist[j]['begrule'] +
'
begchgty:' + prefptslist[j]['begchgty'] +
'
endyr:' + prefptslist[j]['endyr'] +
'
endrule:' + prefptslist[j]['endrule'] +
'
endchgty:' + prefptslist[j]['endchgty'] +
'
geosrc:' + prefptslist[j]['geosrc'] +
'
compiler:' + prefptslist[j]['compiler'] +
'
gecomplr:' + prefptslist[j]['gecomplr'] +
'
checker:' + prefptslist[j]['checker']
);
};
}).catch((response) => {
console.log(response)
})
}
}
//cntypts
for (var i = 0; i < dynastylist.length; i++) {
if (dynastylist[i]['children'][0]['ischeck'] == true) {
var url = urlbase + 'category=cntypts&start=' + dynastylist[i]['timerange'][0] + '&end=' + dynastylist[i]['timerange'][1];
var dynastypoint = [];
axios.get(url).then((response) => {
var cntyptslist = response.data['list']
for (var j = 0; j < cntyptslist.length; j++) {
if(cntyindexlist.indexOf(cntyptslist[j]['gid'])>0){
continue;
}
cntyindexlist.push(cntyptslist[j]['gid']);
dynastypoint[j] = new L.marker([cntyptslist[j]['geometry']['coordinates'][1], cntyptslist[j]['geometry']['coordinates'][0]], { icon: blackIcon, title: cntyptslist[j]['namech'] });
dynastypoint[j].addTo(this.map);
blackpointlist.push(dynastypoint[j]);
dynastypoint[j].bindPopup('namech:' + cntyptslist[j]['namech'] +
'
nameft:' + cntyptslist[j]['nameft'] +
'
namepy:' + cntyptslist[j]['namepy'] +
'
typech:' + cntyptslist[j]['typech'] +
'
presloc:' + cntyptslist[j]['presloc'] +
'
begyr:' + cntyptslist[j]['begyr'] +
'
begrule:' + cntyptslist[j]['begrule'] +
'
begchgty:' + cntyptslist[j]['begchgty'] +
'
endyr:' + cntyptslist[j]['endyr'] +
'
endrule:' + cntyptslist[j]['endrule'] +
'
endchgty:' + cntyptslist[j]['endchgty'] +
'
geosrc:' + cntyptslist[j]['geosrc'] +
'
compiler:' + cntyptslist[j]['compiler'] +
'
gecomplr:' + cntyptslist[j]['gecomplr'] +
'
checker:' + cntyptslist[j]['checker']
);
};
}).catch((response) => {
console.log(response)
})
}
}
}
},
mounted() {
this.initLeaflet()
}
};
#map {
position: absolute;
left: 15%;
top: 60px;
width: 85%;
height: 100vh;
z-index: 80;
}
https://github.com/yimengyao13/vue-gismap.git
分支:leaflets
接下来该做的就是打包,整合到后端工程中了。