leaflet.js实现在线绘制/编辑多边形

master
许宏杰 6 months ago
parent c00beddacb
commit 92ab617a9f

@ -51,6 +51,7 @@
"js-cookie": "3.0.1",
"jsencrypt": "3.0.0-rc.1",
"leaflet": "^1.9.4",
"leaflet-draw": "^1.0.4",
"moment": "^2.30.1",
"nprogress": "0.2.0",
"postcss-px-to-viewport": "^1.1.1",

@ -27,6 +27,7 @@ L.Icon.Default.mergeOptions({
iconUrl: require("leaflet/dist/images/marker-icon.png"),
shadowUrl: require("leaflet/dist/images/marker-shadow.png"),
});
import "leaflet-draw/dist/leaflet.draw.css";
import "./assets/icons"; // icon
import "./permission"; // permission control

@ -87,6 +87,13 @@ export const constantRoutes = [
meta: { title: "商铺", icon: "dashboard", affix: false },
hidden: true,
},
{
path: "/drawMap",
component: () => import("@/views/drawMap"),
name: "drawMap",
meta: { title: "商铺", icon: "dashboard", affix: false },
hidden: true,
},
{
path: "/xiaoqu",
component: () => import("@/views/xiaoqu"),

@ -0,0 +1,137 @@
<template>
<div id="map"></div>
</template>
<script>
import L from "leaflet";
import "leaflet-draw";
export default {
data() {
return {
map: null,
polyline: null,
polygon: null,
markers: [],
points: [],
isPolygonClosed: false, //
mapLayer: {},
};
},
mounted() {
this.initMap();
},
methods: {
initMap() {
this.map = L.map("map", {
center: [31.478296, 121.089856], // []
zoom: 15, //
zoomControl: false, //
attributionControl: false, // leaflet
});
this.mapLayer.picture = L.tileLayer(
"https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",
{
maxZoom: 19,
minZoom: 9,
subdomains: ["0", "1", "2", "3", "4", "5", "6", "7"],
}
).addTo(this.map);
//
this.map.on("click", this.onMapClick);
},
onMapClick(e) {
if (this.isPolygonClosed) {
//
this.toggleEditing();
} else {
//
this.addPoint(e);
}
},
addPoint(e) {
const latlng = e.latlng;
const marker = L.circleMarker(latlng, {
radius: 5,
color: "red",
fillColor: "#f03",
fillOpacity: 0.5,
}).addTo(this.map);
this.markers.push(marker);
this.points.push(latlng);
this.updateShape();
},
updateShape() {
if (this.polyline) {
this.map.removeLayer(this.polyline);
}
if (this.points.length > 2 && this.isShapeClosed()) {
this.isPolygonClosed = true;
this.markers.forEach((marker) => this.map.removeLayer(marker));
this.polygon = L.polygon(this.points, { color: "blue" }).addTo(
this.map
);
//
this.initEditing();
} else {
this.polyline = L.polyline(this.points, { color: "blue" }).addTo(
this.map
);
}
},
isShapeClosed() {
const start = this.points[0];
const end = this.points[this.points.length - 1];
return start.distanceTo(end) < 10;
},
initEditing() {
//
this.drawControl = new L.Control.Draw({
edit: {
featureGroup: L.featureGroup([this.polygon]),
remove: false,
marker: false,
},
draw: false,
});
this.map.addControl(this.drawControl);
},
toggleEditing() {
if (this.polygon.editing && this.polygon.editing.enabled()) {
//
this.polygon.editing.disable();
const polygonData = this.saveEditedPoints();
console.log("Polygon data:", polygonData); //
//
} else {
//
this.polygon.editing.enable();
}
},
saveEditedPoints() {
//
this.points = this.polygon.getLatLngs()[0];
return {
type: "Polygon",
coordinates: this.points.map((latlng) => [latlng.lng, latlng.lat]), // [lng, lat]
};
},
},
};
</script>
<style>
#map {
height: 100%;
width: 100%;
}
.leaflet-draw {
display: none;
}
</style>
Loading…
Cancel
Save