initMap() {
let _this = this;
map = new mapboxgl.Map({
container: "collection-map",
style: {
version: 8,
glyphs: "/font/{fontstack}/{range}.pbf",
sources: {},
layers: [
{
id: "background",
type: "background",
layout: { visibility: "visible" },
paint: { "background-color": "#002B42" },
},
]
},
center: [106.29318, 30.29],
zoom: 4.2,
minZoom: 3.6,
pitch: 40,
bearing: -8,
});
map.on("load", function () {
_this.addLayer_country();
_this.addLayer_countryLine();
_this.addLabel_provience();
_this.addLabel_provienceLine();
_this.addBase();
_this.addLine();
_this.addPowerPlant();
});
},
addLayer_country() {
map.addSource("country_Area", {
type: "vector",
tiles: ['http://117.175.169.58:47781/gisserver/rest/services/featureserver/vector/country_Area/{z}/{x}/{y}'],
});
map.addLayer({
id: "country_Area",
source: "country_Area",
"source-layer": "country_Area",
type: "fill",
layout: {},
paint: {
"fill-opacity": 0.75,
"fill-color": "#082958",
},
});
},
addLayer_countryLine() {
map.addSource("country_guojiexian", {
type: "vector",
tiles: ['http://117.175.169.58:47781/gisserver/rest/services/featureserver/vector/country_guojiexian/{z}/{x}/{y}'],
});
map.addLayer({
id: "country_guojiexian",
source: "country_guojiexian",
"source-layer": "country_guojiexian",
type: "line",
layout: {},
paint: {
"line-color": "#97C8F7",
"line-width": 3,
"line-opacity": 0.8,
},
});
},
addLabel_provienceLine() {
map.addSource("country_Border", {
type: "vector",
tiles: ['http://117.175.169.58:47781/gisserver/rest/services/featureserver/vector/country_Border/{z}/{x}/{y}'],
});
map.addLayer({
id: "country_Border",
source: "country_Border",
"source-layer": "country_Border",
type: "line",
layout: {},
paint: {
"line-color": "#396FB5",
"line-width": 2,
"line-opacity": 0.9,
},
});
},
addLabel_provience() {
axios.get("/json/省份标注.json").then(res=>{
let provinceNameArr = res.data.features.slice();
provinceNameArr.forEach((v) => {
let xy = new mapboxgl.LngLat(v.properties.x, v.properties.y);
const el = document.createElement("div");
el.style.color = "#fff";
el.style.fontSize = "14px";
el.style.fontWeight = "bold";
el.style.opacity = "0.6";
const t = document.createTextNode(v.properties.name);
el.appendChild(t);
new mapboxgl.Marker({
element: el,
})
.setLngLat(xy)
.addTo(map);
});
})
},
addBase() {
let size = 100;
let pulsingDot1 = {
width: size,
height: size,
data: new Uint8Array(size * size * 4),
onAdd: function () {
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
this.context = canvas.getContext("2d");
},
render: function () {
var duration = 1000;
var t = (performance.now() % duration) / duration;
var radius = (size / 2) * 0.3;
var outerRadius = (size / 2) * 0.7 * t + radius;
var context = this.context;
context.clearRect(0, 0, this.width, this.height);
context.beginPath();
context.arc(
this.width / 2,
this.height / 2,
outerRadius,
0,
Math.PI * 2
);
context.fillStyle = "rgba(46,129,192," + (1 - t) + ")";
context.fill();
context.beginPath();
context.arc(this.width / 2, this.height / 2, radius, 0, Math.PI * 2);
context.fillStyle = "rgba(46,129,192,1)";
context.strokeStyle = "white";
context.lineWidth = 2 + 4 * (1 - t);
context.fill();
context.stroke();
this.data = context.getImageData(0, 0, this.width, this.height).data;
map.triggerRepaint();
return true;
},
};
let pulsingDot2 = {
width: size,
height: size,
data: new Uint8Array(size * size * 4),
onAdd: function () {
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
this.context = canvas.getContext("2d");
},
render: function () {
var duration = 1000;
var t = (performance.now() % duration) / duration;
var radius = (size / 2) * 0.3;
var outerRadius = (size / 2) * 0.7 * t + radius;
var context = this.context;
context.clearRect(0, 0, this.width, this.height);
context.beginPath();
context.arc(
this.width / 2,
this.height / 2,
outerRadius,
0,
Math.PI * 2
);
context.fillStyle = "rgba(102,184,124," + (1 - t) + ")";
context.fill();
context.beginPath();
context.arc(this.width / 2, this.height / 2, radius, 0, Math.PI * 2);
context.fillStyle = "rgba(102,184,124,1)";
context.strokeStyle = "white";
context.lineWidth = 2 + 4 * (1 - t);
context.fill();
context.stroke();
this.data = context.getImageData(0, 0, this.width, this.height).data;
map.triggerRepaint();
return true;
},
};
axios.get("/json/基地点.json").then(res=>{
let newBase = res.data.slice();
let newArr = newBase.map((v) => {
return {
type: "Feature",
geometry: {
type: "Point",
coordinates: v.coordinates,
},
properties: {
Name: v.name,
edition: v.edition,
offset: v.offset,
},
};
});
map.addImage("pulsing-dot1", pulsingDot1, { pixelRatio: 2 });
map.addImage("pulsing-dot2", pulsingDot2, { pixelRatio: 2 });
map.addSource("base", {
type: "geojson",
data: {
type: "FeatureCollection",
features: newArr,
},
});
map.addLayer({
id: "points",
type: "symbol",
source: "base",
layout: {
"icon-image": [
"match",
["get", "edition"],
1,
"pulsing-dot1",
2,
"pulsing-dot2",
"pulsing-dot2",
],
"text-field": ["get", "Name"],
"text-font": ["Open Sans Semibold,Arial Unicode MS Bold"],
"text-size": 18,
"text-ignore-placement": true,
"text-offset": ["get", "offset"],
"text-anchor": "top",
"icon-size": 1,
"icon-ignore-placement": true,
},
paint: {
"text-color": "#fff",
"text-halo-color": "#000",
"text-halo-width": 2,
},
});
})
},
addLine() {
let echartslayer;
axios.get("/json/基地点.json").then(res=>{
let lineList = res.data.slice();
let convertData = function (data) {
let res = [];
for (let i of data) {
res.push({
coords: [i.coordinates, [116.4, 39.9]],
});
}
return res;
};
let series = [
{
coordinateSystem: "GLMap",
type: "lines",
zlevel: 1,
effect: {
show: true,
period: 5,
trailLength: 0.7,
color: "#dff0f7",
symbolSize: 5,
},
lineStyle: {
normal: {
color: "#5fccfa",
width: 1,
opacity: 0.4,
curveness: 0.3,
},
},
data: convertData(lineList),
},
];
let option = {
GLMap: {
roam: true,
},
series: series,
};
echartslayer = new EchartsLayer(map);
echartslayer.chart.setOption(option);
})
},
addPowerPlant(){
function arrayGetText(defaultArray, transformArray) {
let arr = [];
defaultArray.forEach((e) => {
transformArray.find((i) => {
if (e.properties.name == i.NAME) {
e.properties.capacity = i.capacity + "万KW";
e.properties.capacityNum = i.capacity * 20000;
arr.push(e);
}
});
});
return arr;
}
function arrayGetCircle(defaultArray, transformArray) {
let arr = [];
defaultArray.forEach((e) => {
transformArray.find((i) => {
if (e.properties.name == i.NAME) {
arr.push(turf.circle(e.geometry.coordinates, 30, {
steps: 128,
units: "kilometers",
properties: e.properties,
}));
}
});
});
return arr;
}
axios.get("/json/省份.json").then(res=>{
let provincePoint = res.data;
axios.get("/json/发电厂.json").then(res2=>{
let newArray = res2.data;
provincePoint.features = arrayGetText(provincePoint.features, newArray)
provincePoint.features = arrayGetText(provincePoint.features, newArray);
let bars = arrayGetCircle(provincePoint.features, newArray)
map.addSource("building", {
type: "geojson",
data: {
type: "FeatureCollection",
features: bars,
},
});
map.addLayer({
id: "3d-buildings",
source: "building",
type: "fill-extrusion",
paint: {
"fill-extrusion-color": [
"interpolate",
["exponential", 1],
["get", "capacityNum"],
5000000,
"rgb(0, 210, 145)",
10000000,
"rgb(171, 255, 80)",
15000000,
"rgb(255, 73, 73)",
25000000,
"rgb(0, 255, 252)",
],
"fill-extrusion-height": [
"interpolate",
["linear"],
["zoom"],
4,
0,
14.05,
["get", "capacityNum"],
],
"fill-extrusion-opacity": 1,
},
});
})
})
}
```c
在这里插入代码片