|
@@ -1,7 +1,7 @@
|
|
|
import AMapLoader from '@amap/amap-jsapi-loader';
|
|
|
import { nanoid } from 'nanoid';
|
|
|
import { deepClone } from '@/utils';
|
|
|
-import { wgs_gcj_encrypts } from '@/utils/gisUtils';
|
|
|
+import { mergeGeoJsonPolygons, wgs_gcj_encrypts } from '@/utils/gisUtils';
|
|
|
|
|
|
export function useAMap(options) {
|
|
|
let AMap, map, nowLayer, labelsLayer, scale, cluster;
|
|
@@ -267,49 +267,85 @@ export function useAMap(options) {
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
+ const removeMask = () => {
|
|
|
+ if (!!maskPolygon) {
|
|
|
+ map.remove(maskPolygon);
|
|
|
+ maskPolygon = null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ let maskPolygon2 = [];
|
|
|
const creatMask2 = (data, option) => {
|
|
|
- // 外多边形坐标数组和内多边形坐标数组
|
|
|
+ data = convertCoordinates(data);
|
|
|
+ // 边界部分
|
|
|
+ data.features.forEach((feature) => {
|
|
|
+ if (feature.geometry.type === 'Polygon') {
|
|
|
+ const polygonPath = feature.geometry.coordinates[0].map(function(coord) {
|
|
|
+ return [coord[0], coord[1]];
|
|
|
+ });
|
|
|
+ const polygon = new AMap.Polygon({
|
|
|
+ path: polygonPath,
|
|
|
+ strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
|
|
|
+ strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
|
|
|
+ strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
|
|
|
+ fillOpacity: 0
|
|
|
+ });
|
|
|
+ maskPolygon2.push(polygon);
|
|
|
+ map.add(polygon);
|
|
|
+ } else if (feature.geometry.type === 'MultiPolygon') {
|
|
|
+ feature.geometry.coordinates.forEach((polygonCoords) => {
|
|
|
+ const polygonPath = polygonCoords.map((ring) => {
|
|
|
+ return ring.map((coord) => {
|
|
|
+ return [coord[0], coord[1]];
|
|
|
+ });
|
|
|
+ });
|
|
|
+ const outerPath = polygonPath[0];
|
|
|
+ const innerPaths = polygonPath.slice(1);
|
|
|
+ const polygon = new AMap.Polygon({
|
|
|
+ path: outerPath,
|
|
|
+ holes: innerPaths,
|
|
|
+ strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
|
|
|
+ strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
|
|
|
+ strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
|
|
|
+ fillOpacity: 0
|
|
|
+ });
|
|
|
+ maskPolygon2.push(polygon);
|
|
|
+ map.add(polygon);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 遮罩部分
|
|
|
const outer = [
|
|
|
- new AMap.LngLat(-360, 90, true),
|
|
|
- new AMap.LngLat(-360, -90, true),
|
|
|
- new AMap.LngLat(360, -90, true),
|
|
|
- new AMap.LngLat(360, 90, true)
|
|
|
+ new AMap.LngLat(-180, 90, true),
|
|
|
+ new AMap.LngLat(-180, -90, true),
|
|
|
+ new AMap.LngLat(180, -90, true),
|
|
|
+ new AMap.LngLat(180, 90, true)
|
|
|
];
|
|
|
- //outer
|
|
|
const pathArray = [outer];
|
|
|
- data = convertCoordinates(data);
|
|
|
- data.features.forEach((item, index) => {
|
|
|
- let arr = [];
|
|
|
- const geometry = item.geometry;
|
|
|
- geometry.coordinates.forEach((coordinate) => {
|
|
|
- if (geometry.type === 'MultiPolygon') {
|
|
|
- coordinate[0].forEach((coordinate2) => {
|
|
|
- arr.push(coordinate2);
|
|
|
- });
|
|
|
- pathArray.push(arr);
|
|
|
- arr = [];
|
|
|
- } else {
|
|
|
- arr.push(coordinate);
|
|
|
- }
|
|
|
- });
|
|
|
- if (arr.length == 0) return;
|
|
|
- pathArray.push(arr);
|
|
|
+ // 合并区边界
|
|
|
+ const data2 = mergeGeoJsonPolygons(data);
|
|
|
+ data2.geometry.coordinates.forEach((coords) => {
|
|
|
+ pathArray.push(coords[0]);
|
|
|
});
|
|
|
maskPolygon = new AMap.Polygon({
|
|
|
path: pathArray,
|
|
|
- strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
|
|
|
- strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
|
|
|
+ strokeOpacity: 0,
|
|
|
strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
|
|
|
fillColor: option.fillColor ? option.fillColor : '#10243b',
|
|
|
fillOpacity: option.fillOpacity ? option.fillOpacity : 0.65
|
|
|
});
|
|
|
map.add(maskPolygon);
|
|
|
};
|
|
|
- const removeMask = () => {
|
|
|
+ const removeMask2 = () => {
|
|
|
if (!!maskPolygon) {
|
|
|
map.remove(maskPolygon);
|
|
|
maskPolygon = null;
|
|
|
}
|
|
|
+ if (maskPolygon2 && maskPolygon2.length > 0) {
|
|
|
+ maskPolygon2.forEach((polygon) => {
|
|
|
+ map.remove(polygon);
|
|
|
+ });
|
|
|
+ maskPolygon2 = [];
|
|
|
+ }
|
|
|
};
|
|
|
let moveMarker, movePolyline, movePassedPolyline, timerId;
|
|
|
const trackPlayback = (lineArr) => {
|
|
@@ -529,10 +565,13 @@ export function useAMap(options) {
|
|
|
);
|
|
|
newGeometry = { ...geometry, coordinates: newCoordinates };
|
|
|
} else if (geometry.type === 'Polygon') {
|
|
|
- const newCoordinates = geometry.coordinates.map((ring) => {
|
|
|
- const obj = wgs_gcj_encrypts(ring);
|
|
|
- return obj;
|
|
|
- });
|
|
|
+ const newCoordinates = geometry.coordinates.map((polygon) =>
|
|
|
+ polygon.map((coords) => {
|
|
|
+ const [x, y] = coords;
|
|
|
+ const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
|
|
|
+ return [obj.lng, obj.lat];
|
|
|
+ })
|
|
|
+ );
|
|
|
newGeometry = { ...geometry, coordinates: newCoordinates };
|
|
|
} else if (geometry.type === 'MultiPolygon') {
|
|
|
const newCoordinates = geometry.coordinates.map((polygon) =>
|
|
@@ -570,6 +609,7 @@ export function useAMap(options) {
|
|
|
creatMask,
|
|
|
removeMask,
|
|
|
creatMask2,
|
|
|
+ removeMask2,
|
|
|
trackPlayback,
|
|
|
drawData
|
|
|
};
|