openlayers 地图组件封装
<template>
<div class="mapBox">
<div ref="map" id="map" class="map">div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer">a>
<div id="popup-content">{{ popup.content }}div>
div>
<div v-if="false" class="optionsBox">
<el-dropdown @command="handleGisCommand">
<el-button type="primary">
GIS服务
<i class="el-icon-arrow-down el-icon--right">i>
el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="1">测距离el-dropdown-item>
<el-dropdown-item command="2">测面积el-dropdown-item>
<el-dropdown-item command="3">清空el-dropdown-item>
<el-dropdown-item command="4">打印地图el-dropdown-item>
el-dropdown-menu>
el-dropdown>
<el-radio-group v-model="checkUnderLayer" @change="handleUnderLayerChange">
<el-radio-button label="sldt">矢量底图el-radio-button>
<el-radio-button label="wxdt">卫星底图el-radio-button>
el-radio-group>
<el-checkbox-group v-model="checkLayers" @change="handleLayerChange">
<el-checkbox-button label="zjc">注记层el-checkbox-button>
<el-checkbox-button label="xzqh">行政区划el-checkbox-button>
el-checkbox-group>
<el-button type="primary" @click="handleFullScreen">{{ isFullScreen ? "退出全屏" : "全屏" }}el-button>
<el-button type="primary" @click="handleBackCenter">回到原点el-button>
<el-button type="primary" @click="handleTrackStart">{{ mapTrack.isPlay ? "暂停" : "开始" }}el-button>
<el-button type="primary" @click="handleTrackStop">停止el-button>
div>
div>
template>
<script>
import "ol/ol.css";
import request from "@/utils/request.js";
import domtoimage from "dom-to-image";
import Text from "ol/style/Text";
import { getWidth, getTopLeft } from "ol/extent";
import View from "ol/View";
import Map from "ol/Map";
import WMTSTileGrid from "ol/tilegrid/WMTS";
import Feature from "ol/Feature";
import { WMTS, Vector as VectorSource, XYZ } from "ol/source";
import { Tile as TileLayer, Vector as VectorLayer, VectorImage } from "ol/layer";
import { getArea, getLength } from "ol/sphere";
import { unByKey } from "ol/Observable";
import { LineString, Polygon, Point, MultiLineString } from "ol/geom";
import { MousePosition, ScaleLine, ZoomSlider } from "ol/control";
import { createStringXY } from "ol/coordinate";
import * as olProj from "ol/proj";
import { Draw, Select, Modify } from "ol/interaction";
import Overlay from "ol/Overlay";
import { Circle as CircleStyle, Fill, Stroke, Style, Icon } from "ol/style";
import { scale } from "ol/size";
import GeoJSON from "ol/format/GeoJSON";
import SourceVector from "ol/source/Vector";
import { getDistance } from "ol/sphere";
import { getVectorContext } from "ol/render";
import { EventBus } from "@/utils/eventBus.js";
import { transform, fromLonLat, toLonLat } from "ol/proj";
import LayerTile from "ol/layer/Tile";
import ImageLayer from "ol/layer/Image";
import { Raster as RasterSource } from "ol/source";
import { defaults as defaultControls } from "ol/control";
import { saveAs } from "file-saver";
export default {
data() {
return {
map: null,
mapCenter: [118.091838, 36.958653],
popup: {
popupOverlay: null,
content: "",
},
checkLayers: ["zjc", "xzqh"],
mapLayers: {},
underlayer: {
sldt: { layer: null, show: true },
wxdt: { layer: null, show: false },
},
checkUnderLayer: "sldt",
mapDraw: {
helpTooltipElement: null,
feature: null,
helpTooltip: null,
draw: null,
measureTooltipElement: null,
measureTooltip: null,
listener: null,
mapMouseMove: null,
drawElements: [],
drawLayers: [],
},
isFullScreen: false,
mapTrack: {
isPlay: false,
isStop: false,
route: null,
featureMove: {},
geometryMove: {},
carPoints: [],
routeIndex: 0,
timer: null,
routeLayer: {},
coordinates: [
[117.98804853292008, 36.924719974587475],
... ...
],
},
};
},
mounted() {
this.initDeepColorMap();
this.initEvent();
this.initPointPopup();
window.addEventListener("resize", () => {
this.isFullScreen =
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.msFullscreenElement ||
document.mozFullScreenElement ||
null;
});
},
methods: {
handleTrackStart() {
this.mapTrack.isPlay = !this.mapTrack.isPlay;
if (this.mapTrack.isPlay) {
this.trackMoveStart();
} else {
this.trackMovePause();
}
},
handleTrackStop() {
this.mapTrack.isPlay = false;
this.mapTrack.isStop = true;
this.resetTrack();
this.addTrack();
},
handleUnderLayerChange(type) {
for (let i in this.underlayer) {
this.underlayer[i].layer.setVisible(false);
this.underlayer[i].show = false;
}
this.underlayer[type].show = true;
this.underlayer[type].layer.setVisible(true);
},
handleLayerChange() {
for (let i in this.mapLayers) {
this.mapLayers[i].layer.setVisible(false);
}
for (let i in this.checkLayers) {
this.mapLayers[this.checkLayers[i]].layer.setVisible(true);
}
},
handleFullScreen() {
if (this.isFullScreen) {
this.exitfullscreen();
} else {
this.enterfullscreen();
}
},
handleBackCenter() {
this.map.getView().setCenter(this.mapCenter);
},
initMap() {
const tdLayer = new TileLayer({
source: new XYZ({
crossOrigin: "anonymous",
projection: "EPSG:4326",
url: "https://t0.tianditu.gov.cn/DataServer?x={x}&y={y}&l={z}&T=vec_c&tk=地图的key",
}),
className: "dt",
});
const tdTextLayer = new TileLayer({
source: new XYZ({
crossOrigin: "anonymous",
projection: "EPSG:4326",
url: "https://t0.tianditu.gov.cn/DataServer?x={x}&y={y}&l={z}&T=cva_c&tk=地图的key",
}),
className: "txt",
});
this.map = new Map({
target: "map",
view: new View({
center: this.mapCenter,
projection: olProj.get("EPSG:4326"),
zoom: 13,
maxZoom: 17,
minZoom: 1,
}),
});
this.map.addLayer(tdLayer);
this.map.addLayer(tdTextLayer);
this.mapLayers.zjc = { layer: tdTextLayer, show: true };
const projection = olProj.get("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = [];
const matrixIds = [];
for (let z = 0; z < 20; ++z) {
resolutions[z] = size / Math.pow(2, z + 1);
matrixIds[z] = z + 1;
}
const tdwxLayer = new TileLayer({
opacity: 1,
visible: false,
source: new WMTS({
crossOrigin: "anonymous",
url: "http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=地图的key",
layer: "wxt",
matrixSet: "c",
format: "tiles",
style: "default",
projection: this.projection,
tileGrid: new WMTSTileGrid({
origin: getTopLeft(projectionExtent),
resolutions: resolutions,
matrixIds: matrixIds,
}),
wrapX: true,
}),
});
this.map.addLayer(tdwxLayer);
this.underlayer.sldt = { layer: tdLayer, show: true };
this.underlayer.wxdt = { layer: tdwxLayer, show: false };
},
initDeepColorMap() {
const view = new View({
center: this.mapCenter,
zoom: 12,
projection: "EPSG:4326",
});
this.map = new Map({
target: "map",
view: view,
controls: defaultControls({
zoom: true,
attribution: false,
rotate: false,
}),
});
let url = "https://t0.tianditu.gov.cn/DataServer?x={x}&y={y}&l={z}";
url = `${url}&T=vec_c&tk=地图的key`;
const source = new XYZ({
crossOrigin: "anonymous",
url: url,
projection: "EPSG:4326",
});
const tdtLayer = new TileLayer({
className: "blueLayer",
source: source,
});
this.map.addLayer(tdtLayer);
url = "https://t0.tianditu.gov.cn/DataServer?x={x}&y={y}&l={z}";
url = `${url}&T=cva_c&tk=地图的key`;
const sourceCVA = new XYZ({
crossOrigin: "anonymous",
url: url,
projection: "EPSG:4326",
});
const tdtcvaLayer = new TileLayer({
className: "blueLayer",
source: sourceCVA,
});
this.map.addLayer(tdtcvaLayer);
this.mapLayers.zjc = { layer: tdtcvaLayer, show: true };
const projection = olProj.get("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = [];
const matrixIds = [];
for (let z = 0; z < 20; ++z) {
resolutions[z] = size / Math.pow(2, z + 1);
matrixIds[z] = z + 1;
}
const tdwxLayer = new TileLayer({
opacity: 1,
visible: false,
source: new WMTS({
crossOrigin: "anonymous",
url:
"http://t0.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=地图的key",
layer: "wxt",
matrixSet: "c",
format: "tiles",
style: "default",
projection: this.projection,
tileGrid: new WMTSTileGrid({
origin: getTopLeft(projectionExtent),
resolutions: resolutions,
matrixIds: matrixIds,
}),
wrapX: true,
}),
});
this.map.addLayer(tdwxLayer);
this.underlayer.sldt = { layer: tdtLayer, show: true };
this.underlayer.wxdt = { layer: tdwxLayer, show: false };
},
initBlueColorMap() {
let target = "map";
let tileLayer = [
new TileLayer({
source: new XYZ({
url: "http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
}),
}),
];
let view = new View({
center: this.mapCenter,
zoom: 12,
});
this.map = new Map({
target: target,
layers: tileLayer,
view: view,
});
const TiandiMap_cia = new TileLayer({
name: "天地图影像注记图层",
source: new XYZ({
url: "http://t0.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=地图的key",
wrapX: false,
}),
});
this.map.addLayer(TiandiMap_cia);
},
initEvent() {
this.map.once("rendercomplete", () => {
this.addProvinceBorder();
this.$emit("complete", this.map);
});
this.map.on("click", e => {
var pixel = this.map.getEventPixel(e.originalEvent);
console.log(e.coordinate);
let feature = this.map.forEachFeatureAtPixel(pixel, feature => {
return feature;
});
if (feature && feature.name && feature.name != "cityScope") {
var coodinate = e.coordinate;
this.popup.content = "你点击的坐标为:" + coodinate;
this.popup.popupOverlay.setPosition(coodinate);
this.map.addOverlay(this.popup.popupOverlay);
this.$emit("pointClick", true, feature.name, e.coordinate, e.pixel);
} else {
this.$emit("pointClick", false, feature.name, e.coordinate, e.pixel);
this.popup.popupOverlay.setPosition(undefined);
}
});
},
initPointPopup() {
var container = document.getElementById("popup");
var popupCloser = document.getElementById("popup-closer");
this.popup.popupOverlay = new Overlay({
element: container,
autoPan: true,
});
popupCloser.addEventListener("click", function() {
this.popup.popupOverlay.setPosition(undefined);
});
},
addProvinceBorder(geojson) {
const provinceLayer = new VectorLayer({
renderMode: "vector",
source: new VectorSource(),
zIndex: 0,
});
this.map.addLayer(provinceLayer);
this.mapLayers.xzqh = { layer: provinceLayer, show: true };
let qxn = geojson ? geojson : require("../mixins/370321.json");
qxn.features.forEach(featureJSON => {
let feature = new GeoJSON().readFeature(featureJSON);
feature.set("name", feature.values_.NAME);
feature.setStyle(
new Style({
stroke: new Stroke({
color: "rgba(3,15,83, 1)",
width: 1,
}),
fill: new Fill({
color: "rgba(3,15,83, 0.5)",
}),
})
);
provinceLayer.getSource().addFeature(feature);
});
},
addPoint(list, icon, angle, popupType = 0) {
const flagLayer = new VectorLayer({
source: new VectorSource(),
});
this.map.addLayer(flagLayer);
let featuresArr = [];
for (let i = 0; i < list.length; i++) {
let feature = new Feature({
geometry: new Point([list[i][0], list[i][1]]),
});
feature.name = "point";
feature.popupType = popupType;
feature.setStyle(
new Style({
image: new Icon({
src: icon,
anchor: [0.5, 0.9],
scale: 1,
rotation: angle || 0,
}),
zIndex: 6000,
})
);
featuresArr.push(feature);
}
flagLayer.getSource().addFeatures(featuresArr);
},
handleGisCommand(cmd) {
switch (cmd) {
case "1":
this.drawDistance();
break;
case "2":
this.drawArea();
break;
case "3":
this.drawClear();
break;
case "4":
this.printMap();
break;
default:
break;
}
},
drawDistance() {
let source = new VectorSource();
const layer = new VectorLayer({
source: source,
style: new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
stroke: new Stroke({
color: "#ffcc33",
width: 4,
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: "#ffcc33",
}),
}),
}),
zIndex: 4000,
});
this.mapDraw.mapMouseMove = this.map.on("pointermove", ev => {
let helpMsg = "点击开始测量";
if (this.mapDraw.feature) {
helpMsg = "双击结束测量";
}
this.mapDraw.helpTooltipElement.innerHTML = helpMsg;
this.mapDraw.helpTooltip.setPosition(ev.coordinate);
this.mapDraw.helpTooltipElement.classList.remove("hidden");
});
this.drawCreateHelpTooltip();
this.map.addLayer(layer);
this.mapDraw.drawLayers.push(layer);
this.mapDraw.draw = new Draw({
source: source,
type: "LineString",
style: new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
stroke: new Stroke({
color: "rgba(0, 0, 0, 0.5)",
lineDash: [10, 10],
width: 4,
}),
image: new CircleStyle({
radius: 5,
stroke: new Stroke({
color: "rgba(0, 0, 0, 0.7)",
}),
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
}),
}),
});
this.map.addInteraction(this.mapDraw.draw);
const formatLength = function(line) {
const length = getLength(line);
let output;
if (length > 100) {
output = Math.round((length / 1000) * 100) / 100 + " " + "km";
} else {
output = Math.round(length * 100) / 100 + " " + "m";
}
return output;
};
this.drawCreateMeasureTooltip();
const _this = this;
this.mapDraw.draw.on("drawstart", evt => {
_this.mapDraw.feature = evt.feature;
let tooltipCoord = evt.coordinate;
_this.mapDraw.listener = _this.mapDraw.feature.getGeometry().on("change", function(evt) {
const geom = evt.target;
let output = formatLength(geom);
tooltipCoord = geom.getLastCoordinate();
_this.mapDraw.measureTooltipElement.innerHTML = output;
_this.mapDraw.measureTooltip.setPosition(tooltipCoord);
});
});
this.mapDraw.draw.on("drawend", () => {
this.mapDraw.measureTooltipElement.className = "ol-tooltip ol-tooltip-static";
this.mapDraw.measureTooltip.setOffset([0, -7]);
this.mapDraw.feature = null;
this.mapDraw.measureTooltipElement = null;
this.drawCreateMeasureTooltip();
unByKey(this.mapDraw.listener);
unByKey(this.mapDraw.mapMouseMove);
setTimeout(() => {
this.mapDraw.helpTooltipElement.classList.add("hidden");
this.mapDraw.helpTooltipElement = null;
this.map.removeInteraction(this.mapDraw.draw);
}, 50);
});
},
drawCreateHelpTooltip() {
if (this.mapDraw.helpTooltipElement) {
this.mapDraw.helpTooltipElement.parentNode.removeChild(this.mapDraw.helpTooltipElement);
}
this.mapDraw.helpTooltipElement = document.createElement("div");
this.mapDraw.helpTooltipElement.className = "ol-tooltip hidden";
this.mapDraw.helpTooltip = new Overlay({
element: this.mapDraw.helpTooltipElement,
offset: [15, 0],
positioning: "center-left",
});
this.map.addOverlay(this.mapDraw.helpTooltip);
},
drawCreateMeasureTooltip() {
if (this.mapDraw.measureTooltipElement) {
this.mapDraw.measureTooltipElement.parentNode.removeChild(this.mapDraw.measureTooltipElement);
}
this.mapDraw.measureTooltipElement = document.createElement("div");
this.mapDraw.measureTooltipElement.className = "ol-tooltip ol-tooltip-measure";
this.mapDraw.measureTooltip = new Overlay({
element: this.mapDraw.measureTooltipElement,
offset: [0, -15],
positioning: "bottom-center",
stopEvent: false,
insertFirst: false,
});
this.mapDraw.drawElements.push(this.mapDraw.measureTooltip);
this.map.addOverlay(this.mapDraw.measureTooltip);
},
drawClear() {
for (let i = 0; i < this.mapDraw.drawLayers.length; i++) {
this.map.removeLayer(this.mapDraw.drawLayers[i]);
}
for (let i = 0; i < this.mapDraw.drawElements.length; i++) {
this.map.removeOverlay(this.mapDraw.drawElements[i]);
}
this.mapDraw.drawLayers = [];
this.mapDraw.drawElements = [];
this.map.removeInteraction(this.mapDraw.draw);
unByKey(this.mapDraw.mapMouseMove);
},
drawArea() {
let source = new VectorSource();
const layer = new VectorLayer({
source: source,
style: new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
stroke: new Stroke({
color: "#ffcc33",
width: 4,
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: "#ffcc33",
}),
}),
}),
zIndex: 4000,
});
this.mapDraw.mapMouseMove = this.map.on("pointermove", ev => {
let helpMsg = "点击开始测量";
if (this.mapDraw.feature) {
helpMsg = "双击结束测量";
}
this.mapDraw.helpTooltipElement.innerHTML = helpMsg;
this.mapDraw.helpTooltip.setPosition(ev.coordinate);
this.mapDraw.helpTooltipElement.classList.remove("hidden");
});
this.drawCreateHelpTooltip();
this.map.addLayer(layer);
this.mapDraw.drawLayers.push(layer);
this.mapDraw.draw = new Draw({
source: source,
type: "Polygon",
style: new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
stroke: new Stroke({
color: "rgba(0, 0, 0, 0.5)",
lineDash: [10, 10],
width: 4,
}),
image: new CircleStyle({
radius: 5,
stroke: new Stroke({
color: "rgba(0, 0, 0, 0.7)",
}),
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
}),
}),
});
this.map.addInteraction(this.mapDraw.draw);
const _this = this;
const formatArea = function(polygon) {
var sourceProj = _this.map.getView().getProjection();
var area = getArea(polygon, {
projection: sourceProj,
});
var output;
if (area > 10000) {
output = Math.round((area / 1000000) * 100) / 100 + " " + "km2";
} else {
output = Math.round(area * 100) / 100 + " " + "m2";
}
return output;
};
this.drawCreateMeasureTooltip();
this.mapDraw.draw.on("drawstart", evt => {
_this.mapDraw.feature = evt.feature;
let tooltipCoord = evt.coordinate;
_this.mapDraw.listener = _this.mapDraw.feature.getGeometry().on("change", function(evt) {
const geom = evt.target;
let output = formatArea(geom);
tooltipCoord = geom.getInteriorPoint().getCoordinates();
_this.mapDraw.measureTooltipElement.innerHTML = output;
_this.mapDraw.measureTooltip.setPosition(tooltipCoord);
});
});
this.mapDraw.draw.on("drawend", () => {
this.mapDraw.measureTooltipElement.className = "ol-tooltip ol-tooltip-static";
this.mapDraw.measureTooltip.setOffset([0, -7]);
this.mapDraw.feature = null;
this.mapDraw.measureTooltipElement = null;
this.drawCreateMeasureTooltip();
unByKey(this.mapDraw.listener);
unByKey(this.mapDraw.mapMouseMove);
setTimeout(() => {
this.mapDraw.helpTooltipElement.classList.add("hidden");
this.mapDraw.helpTooltipElement = null;
this.map.removeInteraction(this.mapDraw.draw);
}, 50);
});
},
printMap() {
const that = this;
let exportOptions = {
filter: function(element) {
var className = element.className || "";
return (
className.indexOf("ol-control") === -1 ||
className.indexOf("ol-scale") > -1 ||
(className.indexOf("ol-attribution") > -1 && className.indexOf("ol-uncollapsible"))
);
},
};
that.map.once("rendercomplete", function(e) {
domtoimage.toJpeg(that.map.getViewport(), exportOptions).then(function(dataUrl) {
saveAs(dataUrl, "map.jpg");
});
});
that.map.updateSize();
},
enterfullscreen() {
let docElm = document.documentElement;
if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
}
else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
}
else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
}
this.isFullScreen = true;
},
exitfullscreen() {
if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
this.isFullScreen = false;
},
addTrack() {
this.mapTrack.route = new LineString(this.mapTrack.coordinates);
this.mapTrack.geometryMove = new Point(this.mapTrack.route.getFirstCoordinate());
this.mapTrack.featureMove = new Feature({
type: "featureMove",
geometry: this.mapTrack.geometryMove,
});
this.mapTrack.featureMove.name = "track_point";
this.mapTrack.dotsData = this.mapTrack.coordinates.map(item => {
return transform(item, "EPSG:3857", "EPSG:4326");
});
this.mapTrack.carPoints = [...this.mapTrack.dotsData];
this.mapTrack.routeLayer = new VectorLayer({
source: new VectorSource({
features: [
new Feature({
type: "route",
geometry: this.mapTrack.route,
}),
this.mapTrack.featureMove,
new Feature({
type: "startIcon",
geometry: new Point(this.mapTrack.route.getFirstCoordinate()),
}),
new Feature({
type: "endIcon",
geometry: new Point(this.mapTrack.route.getLastCoordinate()),
}),
new Feature({
type: "featureMove",
geometry: new Point(this.mapTrack.carPoints[0]),
}),
],
}),
style: feature => {
if (feature.get("type") == "route") {
return new Style({
stroke: new Stroke({
width: 6,
color: [25, 59, 128, 1],
}),
});
} else if (feature.get("type") == "startIcon") {
return new Style({
});
} else if (feature.get("type") == "endIcon") {
return new Style({
});
} else if (feature.get("type") == "featureMove") {
return new Style({
image: new Icon({
src: require("@/assets/image/homecar2.png"),
scale: 1,
rotation: this.trackCountRotate(),
}),
});
}
},
});
this.map.addLayer(this.mapTrack.routeLayer);
},
trackMoveStart() {
this.mapTrack.timer = setInterval(() => {
if (this.mapTrack.routeIndex + 1 >= this.mapTrack.carPoints.length) {
this.mapTrack.routeIndex = 0;
clearInterval(this.mapTrack.timer);
return;
}
if (this.trackNextPoint() === this.mapTrack.carPoints[this.mapTrack.routeIndex + 1]) {
this.mapTrack.routeIndex++;
if (this.mapTrack.featureMove.getStyle()) {
this.mapTrack.featureMove
.getStyle()
.getImage()
.setRotation(this.trackCountRotate());
}
}
this.mapTrack.featureMove.getGeometry().setCoordinates(fromLonLat(this.mapTrack.carPoints[this.mapTrack.routeIndex]));
console.log(this.mapTrack.coordinates[this.mapTrack.routeIndex]);
}, 10);
},
trackMovePause() {
clearInterval(this.mapTrack.timer);
},
trackNextPoint() {
let routeIndex = this.mapTrack.routeIndex;
let p1 = this.map.getPixelFromCoordinate(fromLonLat(this.mapTrack.carPoints[routeIndex]));
let p2 = this.map.getPixelFromCoordinate(fromLonLat(this.mapTrack.carPoints[routeIndex + 1]));
let dx = p2[0] - p1[0];
let dy = p2[1] - p1[1];
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance <= 1) {
return this.mapTrack.carPoints[routeIndex + 1];
} else {
let x = p1[0] + dx / distance;
let y = p1[1] + dy / distance;
let coor = transform(this.map.getCoordinateFromPixel([x, y]), "EPSG:3857", "EPSG:4326");
this.mapTrack.carPoints[routeIndex] = coor;
return this.mapTrack.carPoints[routeIndex];
}
},
trackCountRotate() {
let i = this.mapTrack.routeIndex,
j = i + 1;
if (j === this.mapTrack.carPoints.length) {
i--;
j--;
}
let p1 = this.mapTrack.carPoints[i];
let p2 = this.mapTrack.carPoints[j];
return Math.atan2(p2[0] - p1[0], p2[1] - p1[1]);
},
resetTrack() {
clearInterval(this.mapTrack.timer);
this.mapTrack.routeLayer.getSource().clear();
this.map.removeLayer(this.mapTrack.routeLayer);
this.mapTrack.featureMove = {};
this.mapTrack.geometryMove = {};
this.mapTrack.timer = null;
this.mapTrack.carPoints = [];
this.mapTrack.routeIndex = 0;
this.mapTrack.routeLayer = {};
this.mapTrack.route = null;
},
},
};
script>
<style lang="scss" scoped>
.mapBox {
width: 100%;
height: 100%;
position: relative;
}
.map {
width: 100%;
height: 100%;
}
/deep/.ol-control {
display: none;
}
/deep/.blueLayer {
// filter: grayscale(100%) sepia(21%) invert(100%) saturate(150%) brightness(100%);
filter: grayscale(98%) invert(100%) sepia(50%) hue-rotate(180deg) saturate(2000%) brightness(50%) contrast(90%) !important;
}
.optionsBox {
position: absolute;
top: 20px;
left: 20px;
z-index: 1000;
display: flex;
align-items: center;
}
style>
<style lang="scss" scoped>
/deep/.ol-tooltip {
position: relative;
background: rgba(0, 0, 0, 0.5);
border-radius: 4px;
color: white;
padding: 4px 8px;
opacity: 0.7;
white-space: nowrap;
font-size: 12px;
cursor: default;
user-select: none;
}
/deep/ .hidden {
display: none;
}
/deep/.ol-tooltip-measure {
opacity: 1;
font-weight: bold;
}
/deep/.ol-tooltip-static {
background-color: #ffcc33;
color: black;
border: 1px solid white;
}
/deep/.ol-tooltip-measure:before,
/deep/.ol-tooltip-static:before {
border-top: 6px solid rgba(0, 0, 0, 0.5);
border-right: 6px solid transparent;
border-left: 6px solid transparent;
content: "";
position: absolute;
bottom: -6px;
margin-left: -7px;
left: 50%;
}
/deep/.ol-tooltip-static:before {
border-top-color: #ffcc33;
}
style>

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645
- 646
- 647
- 648
- 649
- 650
- 651
- 652
- 653
- 654
- 655
- 656
- 657
- 658
- 659
- 660
- 661
- 662
- 663
- 664
- 665
- 666
- 667
- 668
- 669
- 670
- 671
- 672
- 673
- 674
- 675
- 676
- 677
- 678
- 679
- 680
- 681
- 682
- 683
- 684
- 685
- 686
- 687
- 688
- 689
- 690
- 691
- 692
- 693
- 694
- 695
- 696
- 697
- 698
- 699
- 700
- 701
- 702
- 703
- 704
- 705
- 706
- 707
- 708
- 709
- 710
- 711
- 712
- 713
- 714
- 715
- 716
- 717
- 718
- 719
- 720
- 721
- 722
- 723
- 724
- 725
- 726
- 727
- 728
- 729
- 730
- 731
- 732
- 733
- 734
- 735
- 736
- 737
- 738
- 739
- 740
- 741
- 742
- 743
- 744
- 745
- 746
- 747
- 748
- 749
- 750
- 751
- 752
- 753
- 754
- 755
- 756
- 757
- 758
- 759
- 760
- 761
- 762
- 763
- 764
- 765
- 766
- 767
- 768
- 769
- 770
- 771
- 772
- 773
- 774
- 775
- 776
- 777
- 778
- 779
- 780
- 781
- 782
- 783
- 784
- 785
- 786
- 787
- 788
- 789
- 790
- 791
- 792
- 793
- 794
- 795
- 796
- 797
- 798
- 799
- 800
- 801
- 802
- 803
- 804
- 805
- 806
- 807
- 808
- 809
- 810
- 811
- 812
- 813
- 814
- 815
- 816
- 817
- 818
- 819
- 820
- 821
- 822
- 823
- 824
- 825
- 826
- 827
- 828
- 829
- 830
- 831
- 832
- 833
- 834
- 835
- 836
- 837
- 838
- 839
- 840
- 841
- 842
- 843
- 844
- 845
- 846
- 847
- 848
- 849
- 850
- 851
- 852
- 853
- 854
- 855
- 856
- 857
- 858
- 859
- 860
- 861
- 862
- 863
- 864
- 865
- 866
- 867
- 868
- 869
- 870
- 871
- 872
- 873
- 874
- 875
- 876
- 877
- 878
- 879
- 880
- 881
- 882
- 883
- 884
- 885
- 886
- 887
- 888
- 889
- 890
- 891
- 892
- 893
- 894
- 895
- 896
- 897
- 898
- 899
- 900
- 901
- 902
- 903
- 904
- 905
- 906
- 907
- 908
- 909
- 910
- 911
- 912
- 913
- 914
- 915
- 916
- 917
- 918
- 919
- 920
- 921
- 922
- 923
- 924
- 925
- 926
- 927
- 928
- 929
- 930
- 931
- 932
- 933
- 934
- 935
- 936
- 937
- 938
- 939
- 940
- 941
- 942
- 943
- 944
- 945
- 946
- 947
- 948
- 949
- 950
- 951
- 952
- 953
- 954
- 955
- 956
- 957
- 958
- 959
- 960
- 961
- 962
- 963
- 964
- 965
- 966
- 967
- 968
- 969
- 970
- 971
- 972
- 973
- 974
- 975
- 976
- 977
- 978
- 979
- 980
- 981
- 982
- 983
- 984
- 985
- 986
- 987
- 988
- 989
- 990
- 991
- 992
- 993
- 994
- 995
- 996
- 997
- 998
- 999
- 1000
- 1001
- 1002
- 1003
- 1004
- 1005
- 1006
- 1007
- 1008
- 1009
- 1010
- 1011
- 1012
- 1013
- 1014
- 1015
- 1016
- 1017
- 1018
- 1019
- 1020
- 1021
- 1022
- 1023
- 1024
- 1025
- 1026
- 1027
- 1028
- 1029
- 1030
- 1031
- 1032
- 1033
- 1034
- 1035
- 1036
- 1037
- 1038
- 1039
- 1040
- 1041
- 1042
- 1043
- 1044
- 1045
- 1046
- 1047
- 1048
- 1049
- 1050
- 1051
- 1052
- 1053
- 1054
- 1055
- 1056
- 1057
- 1058
- 1059
- 1060
- 1061
- 1062
- 1063
- 1064
- 1065
- 1066
- 1067
- 1068
- 1069
- 1070
- 1071
- 1072
- 1073
- 1074
- 1075
- 1076
- 1077
- 1078
- 1079
- 1080
- 1081
- 1082
- 1083
- 1084
- 1085
- 1086
- 1087
- 1088
- 1089
- 1090
- 1091
- 1092
- 1093
- 1094
- 1095
- 1096
- 1097
- 1098
- 1099
- 1100
- 1101
- 1102
- 1103
- 1104
- 1105
- 1106
- 1107
- 1108
- 1109
- 1110
- 1111
- 1112
- 1113
- 1114
- 1115
- 1116
- 1117
- 1118
- 1119
- 1120
- 1121
- 1122
- 1123
- 1124
- 1125
- 1126
- 1127
- 1128
- 1129
- 1130
- 1131
- 1132
- 1133
- 1134
- 1135
- 1136
- 1137
- 1138
- 1139
- 1140
- 1141
- 1142
- 1143
- 1144
- 1145
- 1146
- 1147
- 1148
- 1149
- 1150
- 1151
- 1152
- 1153
- 1154
- 1155
- 1156
- 1157
- 1158
- 1159
- 1160
- 1161
- 1162
- 1163
- 1164
- 1165
- 1166
- 1167
- 1168
- 1169
- 1170
- 1171
- 1172
- 1173