Mapbox绘图工具、图斑复制

hc_zhufu
徐景良 1 year ago
parent ccd0308958
commit 9fef8986d2

@ -0,0 +1,7 @@
{
"i18n-ally.localesPaths": [
"src/locales",
"src/locales/lang",
"public/resource/tinymce/langs"
]
}

@ -100,6 +100,8 @@
"lodash-es": "^4.17.21",
"mapbox-gl": "^3.3.0",
"mapbox-gl-utils": "^0.44.0",
"@mapbox/mapbox-gl-draw": "^1.4.1",
"mapbox-gl-draw-snap-mode": "^0.2.0",
"mars3d": "^3.7.0",
"mars3d-cesium": "^1.113.0",
"min-dash": "^4.2.1",

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

@ -0,0 +1,274 @@
<template>
<div class="map-container">
<div id="mapContainer" class="map-box"></div>
<!-- <div class="map-control">
<img
v-for="(item, index) in nextMapControl"
:key="index"
:src="item.icon"
:title="item.title"
@click="handlerMapControlClick(item.handler)"
/>
<img v-show="nextMapControl.length > 0" @click="handlerUnDraw" src="/del.png" title="清除" />
</div> -->
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, defineProps, reactive } from 'vue';
import { useMessage } from '@/hooks/web/useMessage';
import mapboxgl, { Map,Popup } from 'mapbox-gl';
//
import MapboxDraw from "@mapbox/mapbox-gl-draw";
import U from 'mapbox-gl-utils';
import 'mapbox-gl/dist/mapbox-gl.css';
import './src/index.less';
import { MapboxConfig, MapboxDefaultStyle, MapControlConfig } from './src/config';
import { MP } from './src/MP';
import { DrawingType } from '@/enums/mapEnum';
import {
SnapPolygonMode,
SnapPointMode,
SnapLineMode,
SnapModeDrawStyles,
SnapDirectSelect,
} from "mapbox-gl-draw-snap-mode";
// map
interface MapboxOptionsInterface {
mapOptions: mapboxgl.MapboxOptions;
control: DrawingType[];
}
const props = defineProps<MapboxOptionsInterface>();
let nextMapControl: Array<any> = reactive([]);
nextMapControl = props.control
? props.control.map((item) => {
console.log('item::: ', item);
return MapControlConfig[item];
})
: [];
console.log('nextMapControl::: ', nextMapControl);
//
let map: Map;
let popup:Popup;
let clickPoisition:Array<number> = [];
let selectFeature:Object = {};
let mp: any = null;
const { createConfirm, createMessage } = useMessage();
// emit
//
const emit = defineEmits(['mapOnLoad', 'mapDraw']);
onMounted(() => {
mapboxgl.accessToken = MapboxConfig.ACCESS_TOKEN;
map = initMap();
map.on('load', () => {
//mapbox-gl-utils
// U.init(map);
// mp = new MP(map);
// emit('mapOnLoad', map);
//
handlerInitDrawTool();
map.on("click",(e)=>{
clickPoisition = e.lngLat
})
map.on("draw.selectionchange",(e)=>{
handlerCopyToTargetLayer(e);
})
window.handlerCopyFeature = handlerCopyFeature;
});
});
//
//
onUnmounted(() => {
map ? map.remove() : null;
});
//
//
const initMap = () => {
return new mapboxgl.Map({
container: 'mapContainer',
language: 'zh-cmn',
projection: 'equirectangular', // wgs84
style: MapboxDefaultStyle,
maxZoom: 22,
minZoom: 6,
...props.mapOptions,
});
};
const handlerMapControlClick = (handler: string) => {
handler === 'handlerDrawPoint' && handlerDrawPoint();
handler === 'handlerDrawLineString' && handlerDrawLineString();
handler === 'handlerDrawPolygon' && handlerDrawPolygon();
};
//
const handlerDrawPoint = () => {
mp.draw('Point');
mp.on('Point', function (e) {
emit('mapDraw', 'Point', e);
});
};
//线
const handlerDrawLineString = () => {
mp.draw('LineString');
mp.on('LineString', function (e) {
emit('mapDraw', 'LineString', e);
});
};
//
const handlerDrawPolygon = () => {
mp.draw('Polygon');
mp.on('Polygon', function (e) {
emit('mapDraw', 'Polygon', e);
});
};
//
const handlerUnDraw = () => {
mp.deleteDraw();
emit('mapDraw', 'cancel');
};
//
const handlerInitDrawTool = () => {
let drawTool = new MapboxDraw({
modes: {
...MapboxDraw.modes,
draw_point: SnapPointMode,
draw_polygon: SnapPolygonMode,
draw_line_string: SnapLineMode,
direct_select: SnapDirectSelect,
},
// Styling guides
styles: SnapModeDrawStyles,
userProperties: true,
// Config snapping features
snap: true,
snapOptions: {
snapPx: 15, // defaults to 15
snapToMidPoints: true, // defaults to false
snapVertexPriorityDistance: 0.0025, // defaults to 1.25
},
guides: true,
});
map.addControl(drawTool, "top-right");
}
//
const handlerCopyToTargetLayer = (e) => {
if(e.features.length>0){
if(popup){
popup.remove();
popup = null;
}
selectFeature = e.features[0];
popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
});
// popup
popup
.setLngLat(clickPoisition)
.setHTML(`
<div style="color:#333;padding:3px 12px;cursor:pointer;" type="primary" icon="el-icon-search" onclick="handlerCopyFeature();">复制当前图斑</div>`
).addTo(map);
}else{
popup.remove();
}
}
const handlerCopyFeature = () => {
console.log(selectFeature)
popup.remove();
createMessage.success("复制成功!")
}
</script>
<style>
.mapboxgl-ctrl-group:not(:empty){
box-shadow: none;
}
.mapboxgl-ctrl-group{
background:none!important;
}
.mapbox-gl-draw_ctrl-draw-btn{
width:40px!important;
height:40px!important;
float:left;
border-radius: 50%;
}
.mapboxgl-ctrl-top-right{
}
.mapboxgl-ctrl-group button+button{
border:0px;
margin:0px 3px;
}
.mapbox-gl-draw_ctrl-draw-btn:hover{
transform: scale(1.2);
}
.mapbox-gl-draw_polygon{
background-image:url(/polygon.png);
background-size:100% 100%;
width:100px;
height:100px;
}
.mapbox-gl-draw_point{
background-image:url(/point.png);
background-size:100% 100%;
width:100px;
height:100px;
}
.mapbox-gl-draw_line{
background-image:url(/line.png);
background-size:100% 100%;
width:100px;
height:100px;
}
.mapbox-gl-draw_trash{
background-image:url(/del.png);
background-size:100% 100%;
width:100px;
height:100px;
}
.mapbox-gl-draw_combine{
background-image:url(/combine.png);
background-size:100% 100%;
width:100px;
height:100px;
}
.mapbox-gl-draw_uncombine{
background-image:url(/uncombine.png);
background-size:100% 100%;
width:100px;
height:100px;
}
</style>

@ -1,15 +1,21 @@
<template>
<div class="w-full" style="height: 100%">
<MapboxMaps
:mapOptions="mapOptions"
:control="mapDrawControl"
@map-on-load="mapOnLoad"
@map-draw="handlerMapDraw"
/>
<MapboxMap></MapboxMap>
</div>
</template>
<script lang="ts" setup>
import MapboxMaps from '@/components/MapboxMaps/index.vue';
import MapboxMaps from '@/components/MapboxMaps/MapboxMap.vue';
import { DrawingType } from '@/enums/mapEnum';
const mapOptions = {

Loading…
Cancel
Save