|
@@ -2,6 +2,7 @@ import AMapLoader from '@amap/amap-jsapi-loader';
|
|
import { nanoid } from 'nanoid';
|
|
import { nanoid } from 'nanoid';
|
|
import { deepClone } from '@/utils';
|
|
import { deepClone } from '@/utils';
|
|
import {onMounted} from "vue";
|
|
import {onMounted} from "vue";
|
|
|
|
+import { mergeGeoJsonPolygons, wgs_gcj_encrypts } from '@/utils/gisUtils';
|
|
|
|
|
|
export function useAMap(options) {
|
|
export function useAMap(options) {
|
|
let AMap, map, nowLayer, labelsLayer, scale, cluster;
|
|
let AMap, map, nowLayer, labelsLayer, scale, cluster;
|
|
@@ -20,7 +21,7 @@ export function useAMap(options) {
|
|
version: !!options.version ? options.version : '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
|
|
version: !!options.version ? options.version : '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
|
|
plugins: options.plugins
|
|
plugins: options.plugins
|
|
? options.plugins
|
|
? options.plugins
|
|
- : ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool', 'AMap.PolygonEditor', 'AMap.MarkerCluster', 'AMap.DistrictSearch', 'AMap.MoveAnimation', 'AMap.Driving', 'AMap.Geocoder', 'AMap.PlaceSearch']
|
|
|
|
|
|
+ : ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool', 'AMap.PolygonEditor', 'AMap.MarkerCluster', 'AMap.DistrictSearch', 'AMap.MoveAnimation', 'AMap.Driving', 'AMap.Geocoder', 'AMap.PlaceSearch', 'AMap.GeoJSON']
|
|
}).then((res) => {
|
|
}).then((res) => {
|
|
AMap = res;
|
|
AMap = res;
|
|
map = new AMap.Map(options.el ? options.el : 'aMap', {
|
|
map = new AMap.Map(options.el ? options.el : 'aMap', {
|
|
@@ -83,7 +84,7 @@ export function useAMap(options) {
|
|
}
|
|
}
|
|
addPoints = points;
|
|
addPoints = points;
|
|
const count = points.length;
|
|
const count = points.length;
|
|
- const _renderClusterMarker = function (context) {
|
|
|
|
|
|
+ const _renderClusterMarker = function(context) {
|
|
// 聚合中点个数
|
|
// 聚合中点个数
|
|
const clusterCount = context.count;
|
|
const clusterCount = context.count;
|
|
const div = document.createElement('div');
|
|
const div = document.createElement('div');
|
|
@@ -104,7 +105,7 @@ export function useAMap(options) {
|
|
map.setZoomAndCenter(map.getZoom() + 1, bounds.getCenter());
|
|
map.setZoomAndCenter(map.getZoom() + 1, bounds.getCenter());
|
|
});
|
|
});
|
|
};
|
|
};
|
|
- const _renderMarker = function (context) {
|
|
|
|
|
|
+ const _renderMarker = function(context) {
|
|
const content =
|
|
const content =
|
|
'<div style="display: flex;flex-direction: column;align-items: center;justify-content: center">' +
|
|
'<div style="display: flex;flex-direction: column;align-items: center;justify-content: center">' +
|
|
'<div style="background: url(' +
|
|
'<div style="background: url(' +
|
|
@@ -120,7 +121,7 @@ export function useAMap(options) {
|
|
context.marker.setContent(content);
|
|
context.marker.setContent(content);
|
|
context.marker.setOffset(offset);
|
|
context.marker.setOffset(offset);
|
|
context.marker.setExtData(context.data[0]);
|
|
context.marker.setExtData(context.data[0]);
|
|
- context.marker.on('click', function (e) {
|
|
|
|
|
|
+ context.marker.on('click', function(e) {
|
|
const extData = e.target.getExtData();
|
|
const extData = e.target.getExtData();
|
|
let index = 0;
|
|
let index = 0;
|
|
let index2 = 0;
|
|
let index2 = 0;
|
|
@@ -242,7 +243,7 @@ export function useAMap(options) {
|
|
new AMap.DistrictSearch({
|
|
new AMap.DistrictSearch({
|
|
extensions: 'all',
|
|
extensions: 'all',
|
|
subdistrict: 0
|
|
subdistrict: 0
|
|
- }).search(name, function (status, result) {
|
|
|
|
|
|
+ }).search(name, function(status, result) {
|
|
// 外多边形坐标数组和内多边形坐标数组
|
|
// 外多边形坐标数组和内多边形坐标数组
|
|
const outer = [
|
|
const outer = [
|
|
new AMap.LngLat(-360, 90, true),
|
|
new AMap.LngLat(-360, 90, true),
|
|
@@ -252,12 +253,6 @@ export function useAMap(options) {
|
|
];
|
|
];
|
|
options.forEach((option) => {
|
|
options.forEach((option) => {
|
|
const holes = result.districtList[0].boundaries;
|
|
const holes = result.districtList[0].boundaries;
|
|
- // if (option.offset) {
|
|
|
|
- //
|
|
|
|
- // holes.forEach((items) => {
|
|
|
|
- //
|
|
|
|
- // })
|
|
|
|
- // }
|
|
|
|
let pathArray = [outer];
|
|
let pathArray = [outer];
|
|
pathArray.push.apply(pathArray, holes);
|
|
pathArray.push.apply(pathArray, holes);
|
|
maskPolygon = new AMap.Polygon({
|
|
maskPolygon = new AMap.Polygon({
|
|
@@ -279,6 +274,86 @@ export function useAMap(options) {
|
|
maskPolygon = null;
|
|
maskPolygon = null;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
+ let maskPolygon2 = [];
|
|
|
|
+ const creatMask2 = (data, option) => {
|
|
|
|
+ if (maskPolygon2 && maskPolygon2.length > 0) {
|
|
|
|
+ maskPolygon2.forEach((polygon) => {
|
|
|
|
+ polygon.show();
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ data = convertCoordinates(data);
|
|
|
|
+ // 遮罩部分
|
|
|
|
+ // const outer = [
|
|
|
|
+ // new AMap.LngLat(-180, 90, true),
|
|
|
|
+ // new AMap.LngLat(-180, -90, true),
|
|
|
|
+ // new AMap.LngLat(180, -90, true),
|
|
|
|
+ // new AMap.LngLat(180, 90, true)
|
|
|
|
+ // ];
|
|
|
|
+ // const pathArray = [outer];
|
|
|
|
+ // 合并区边界
|
|
|
|
+ // const data2 = mergeGeoJsonPolygons(data);
|
|
|
|
+ // data2.geometry.coordinates.forEach((coords) => {
|
|
|
|
+ // pathArray.push(coords[0]);
|
|
|
|
+ // });
|
|
|
|
+ // const maskPolygon = new AMap.Polygon({
|
|
|
|
+ // path: pathArray,
|
|
|
|
+ // strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
|
|
|
|
+ // strokeOpacity: 1,
|
|
|
|
+ // strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
|
|
|
|
+ // fillColor: option.fillColor ? option.fillColor : '#10243b',
|
|
|
|
+ // fillOpacity: option.fillOpacity ? option.fillOpacity : 0.65
|
|
|
|
+ // });
|
|
|
|
+ // maskPolygon2.push(maskPolygon);
|
|
|
|
+ // map.add(maskPolygon);
|
|
|
|
+ // 边界部分
|
|
|
|
+ data.features.forEach((feature) => {
|
|
|
|
+ if (feature.geometry.type === 'Polygon') {
|
|
|
|
+ const polygonPath = feature.geometry.coordinates[0].map((coord) => {
|
|
|
|
+ return [coord[0], coord[1]];
|
|
|
|
+ });
|
|
|
|
+ const polygon = new AMap.Polyline({
|
|
|
|
+ path: polygonPath,
|
|
|
|
+ strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
|
|
|
|
+ strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
|
|
|
|
+ strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
|
|
|
|
+ clickable: false
|
|
|
|
+ });
|
|
|
|
+ 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.Polyline({
|
|
|
|
+ path: outerPath,
|
|
|
|
+ holes: innerPaths,
|
|
|
|
+ strokeColor: option.strokeColor ? option.strokeColor : '#268ab9',
|
|
|
|
+ strokeOpacity: option.strokeOpacity ? option.strokeOpacity : 1,
|
|
|
|
+ strokeWeight: option.strokeWeight ? option.strokeWeight : 1,
|
|
|
|
+ clickable: false
|
|
|
|
+ });
|
|
|
|
+ maskPolygon2.push(polygon);
|
|
|
|
+ map.add(polygon);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ const removeMask2 = (isHidden) => {
|
|
|
|
+ if (maskPolygon2 && maskPolygon2.length > 0) {
|
|
|
|
+ maskPolygon2.forEach((polygon) => {
|
|
|
|
+ polygon.hide();
|
|
|
|
+ });
|
|
|
|
+ if (!isHidden) {
|
|
|
|
+ maskPolygon2 = [];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
let moveMarker, movePolyline, movePassedPolyline, timerId;
|
|
let moveMarker, movePolyline, movePassedPolyline, timerId;
|
|
const trackPlayback = (lineArr) => {
|
|
const trackPlayback = (lineArr) => {
|
|
if (timerId) {
|
|
if (timerId) {
|
|
@@ -315,12 +390,12 @@ export function useAMap(options) {
|
|
strokeWeight: 6 //线宽
|
|
strokeWeight: 6 //线宽
|
|
});
|
|
});
|
|
|
|
|
|
- moveMarker.on('moving', function (e) {
|
|
|
|
|
|
+ moveMarker.on('moving', function(e) {
|
|
movePassedPolyline.setPath(e.passedPath);
|
|
movePassedPolyline.setPath(e.passedPath);
|
|
map.setCenter(e.target.getPosition(), true);
|
|
map.setCenter(e.target.getPosition(), true);
|
|
});
|
|
});
|
|
|
|
|
|
- moveMarker.on('moveend', function (e) {
|
|
|
|
|
|
+ moveMarker.on('moveend', function(e) {
|
|
index++;
|
|
index++;
|
|
if (index === lineArr.length - 1) {
|
|
if (index === lineArr.length - 1) {
|
|
timerId = setTimeout(() => {
|
|
timerId = setTimeout(() => {
|
|
@@ -465,6 +540,64 @@ export function useAMap(options) {
|
|
data.id = id;
|
|
data.id = id;
|
|
return { text, data };
|
|
return { text, data };
|
|
};
|
|
};
|
|
|
|
+ const convertCoordinates = (geoJson) => {
|
|
|
|
+ const features = geoJson.features.map((feature) => {
|
|
|
|
+ const geometry = feature.geometry;
|
|
|
|
+ let newGeometry;
|
|
|
|
+ if (geometry.type === 'Point') {
|
|
|
|
+ const [x, y] = geometry.coordinates;
|
|
|
|
+ const obj = wgs_gcj_encrypts([{ lng: x, lat: y }])[0];
|
|
|
|
+ newGeometry = { ...geometry, coordinates: [obj.lng, obj.lat] };
|
|
|
|
+ } else if (geometry.type === 'MultiPoint') {
|
|
|
|
+ const newCoordinates = geometry.coordinates.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 === 'LineString') {
|
|
|
|
+ const newCoordinates = geometry.coordinates.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 === 'MultiLineString') {
|
|
|
|
+ const newCoordinates = geometry.coordinates.map((line) =>
|
|
|
|
+ line.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 === 'Polygon') {
|
|
|
|
+ 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) =>
|
|
|
|
+ polygon.map((ring) =>
|
|
|
|
+ ring.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 };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return { ...feature, geometry: newGeometry };
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return { ...geoJson, features };
|
|
|
|
+ };
|
|
onMounted(() => {
|
|
onMounted(() => {
|
|
initMap(options);
|
|
initMap(options);
|
|
});
|
|
});
|
|
@@ -483,6 +616,8 @@ export function useAMap(options) {
|
|
handleHover,
|
|
handleHover,
|
|
creatMask,
|
|
creatMask,
|
|
removeMask,
|
|
removeMask,
|
|
|
|
+ creatMask2,
|
|
|
|
+ removeMask2,
|
|
trackPlayback,
|
|
trackPlayback,
|
|
drawData
|
|
drawData
|
|
};
|
|
};
|