| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- import { EFlightAreaType } from '../types/flight-area'
- import pin19be6b from '/@/assets/icons/pin-19be6b.svg'
- import pin212121 from '/@/assets/icons/pin-212121.svg'
- import pin2d8cf0 from '/@/assets/icons/pin-2d8cf0.svg'
- import pinb620e0 from '/@/assets/icons/pin-b620e0.svg'
- import pine23c39 from '/@/assets/icons/pin-e23c39.svg'
- import pineffbb00 from '/@/assets/icons/pin-ffbb00.svg'
- import { getRoot } from '/@/root'
- import rootStore from '/@/store'
- import { GeojsonCoordinate } from '/@/types/map'
- export function useGMapCover() {
- const root = getRoot()
- const AMap = root.$aMap
- const normalColor = '#2D8CF0'
- const store = rootStore
- const coverMap = store.state.coverMap
- const flightAreaColorMap = {
- [EFlightAreaType.DFENCE]: '#19be6b',
- [EFlightAreaType.NFZ]: '#ff0000',
- }
- const disableColor = '#b3b3b3'
- function AddCoverToMap(cover: any) {
- root.$map.add(cover)
- coverMap[cover.getExtData().id] = [cover]
- }
- function getPinIcon(color?: string) {
- const colorObj: {
- [key: number | string]: any
- } = {
- '2d8cf0': pin2d8cf0,
- '19be6b': pin19be6b,
- 212121: pin212121,
- b620e0: pinb620e0,
- e23c39: pine23c39,
- ffbb00: pineffbb00,
- }
- const iconName = (color?.replaceAll('#', '') || '').toLocaleLowerCase()
- return new AMap.Icon({
- image: colorObj[iconName],
- })
- }
- function init2DPin(name: string, coordinates: GeojsonCoordinate, color?: string, data?: {}) {
- const title = coordinates.map((item: any, index) => {
- if (index < 2) {
- return item.toFixed(4)
- }
- });
- const marker = new AMap.Marker({
- position: new AMap.LngLat(coordinates[0], coordinates[1]),
- title: title,
- icon: getPinIcon(color),
- extData: data
- })
- const text = new AMap.Text({
- position: new AMap.LngLat(coordinates[0], coordinates[1]),
- offset: new AMap.Pixel(20, -2),
- text: name,
- style: {
- backgroundColor: 'transparent',
- borderColor: 'transparent',
- },
- extData: {
- ...data,
- id: data.id + '_other',
- },
- })
- AddCoverToMap(text)
- marker.on('click', function (e: any) {
- const options = marker.getOptions()
- const id = options.extData.id;
- store.commit('SET_MAP_CLICK_ELEMENT', {
- id: id,
- type: 'DEFAULT',
- });
- })
- AddCoverToMap(marker)
- }
- function initPolyline(name: string, coordinates: GeojsonCoordinate[], color?: string, data?: {}) {
- const path = [] as GeojsonCoordinate[]
- coordinates.forEach(coordinate => {
- path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
- })
- const polyline = new AMap.Polyline({
- path: path,
- strokeColor: color || normalColor,
- strokeOpacity: 1,
- strokeWeight: 2,
- strokeStyle: 'solid',
- extData: data
- })
- const circles = path.map((item, index) => {
- return new AMap.Circle({
- center: item,
- radius: 0.5, // 半径
- strokeColor: color,
- fillColor: color,
- fillOpacity: 1,
- strokeWeight: 6,
- });
- })
- const coordinatesList = coordinates[0];
- const text = new AMap.Text({
- position: new AMap.LngLat(coordinatesList[0], coordinatesList[1]),
- offset: new AMap.Pixel(-30, -30),
- text: name,
- style: {
- backgroundColor: 'transparent',
- borderColor: 'transparent',
- },
- extData: {
- ...data,
- id: data.id + '_other',
- },
- })
- const distances = [];
- // 确保首尾相连
- const paths = [...coordinates, coordinates[0]]
- // 计算并显示每段线的距离
- for (let i = 0; i < paths.length - 1; i++) {
- const distance = AMap.GeometryUtil.distance(new AMap.LngLat(paths[i][0], paths[i][1]), new AMap.LngLat(paths[i + 1][0], paths[i + 1][1]));
- // 计算两个点之间的中点坐标
- const midLng = (paths[i][0] + paths[i + 1][0]) / 2;
- const midLat = (paths[i][1] + paths[i + 1][1]) / 2;
- const midPoint = new AMap.LngLat(midLng, midLat);
- // 在中点位置放置文本以显示距离
- const distanceText = new AMap.Text({
- position: midPoint,
- offset: new AMap.Pixel(-16, 10),
- text: `${distance.toFixed(1)} m`,// 距离
- style: {
- fontSize: '10px',
- color: '#FFFFFF',
- backgroundColor: 'rgba(0, 0, 0, 0.75)',
- borderColor: 'transparent',
- },
- });
- distances.push(distanceText);
- }
- const other = [...circles, text, ...distances]
- root.$map.add(other)
- coverMap[data.id + '_other'] = [other]
- polyline.on('click', function () {
- const options = polyline.getOptions()
- const id = options.extData.id;
- store.commit('SET_MAP_CLICK_ELEMENT', {
- id: id,
- type: 'DEFAULT',
- });
- })
- AddOverlayGroup(polyline)
- }
- function initPolygon(name: string, coordinates: GeojsonCoordinate[][], color?: string, data?: {}) {
- const path = [] as GeojsonCoordinate[]
- coordinates[0].forEach(coordinate => {
- path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
- })
- const polygon = new AMap.Polygon({
- path: path,
- strokeOpacity: 1,
- strokeWeight: 2,
- fillColor: color || normalColor,
- fillOpacity: 0.4,
- strokeColor: color || normalColor,
- extData: data
- })
- const circles = path.map((item, index) => {
- return new AMap.Circle({
- center: item,
- radius: 0.5, // 半径
- strokeColor: color,
- fillColor: color,
- fillOpacity: 1,
- strokeWeight: 6,
- });
- })
- const coordinatesList = coordinates[0][0];
- const text = new AMap.Text({
- position: new AMap.LngLat(coordinatesList[0], coordinatesList[1]),
- offset: new AMap.Pixel(-30, -30),
- text: name,
- style: {
- backgroundColor: 'transparent',
- borderColor: 'transparent',
- },
- extData: {
- ...data,
- id: data.id + '_other',
- },
- })
- const distances = [];
- // 确保首尾相连
- const paths = [...coordinates[0], coordinates[0][0]]
- // 计算并显示每段线的距离
- for (let i = 0; i < paths.length - 1; i++) {
- const distance = AMap.GeometryUtil.distance(new AMap.LngLat(paths[i][0], paths[i][1]), new AMap.LngLat(paths[i + 1][0], paths[i + 1][1]));
- // 计算两个点之间的中点坐标
- const midLng = (paths[i][0] + paths[i + 1][0]) / 2;
- const midLat = (paths[i][1] + paths[i + 1][1]) / 2;
- const midPoint = new AMap.LngLat(midLng, midLat);
- // 在中点位置放置文本以显示距离
- const distanceText = new AMap.Text({
- position: midPoint,
- offset: new AMap.Pixel(-16, 10),
- text: `${distance.toFixed(1)} m`,// 距离
- style: {
- fontSize: '10px',
- color: '#FFFFFF',
- backgroundColor: 'rgba(0, 0, 0, 0.75)',
- borderColor: 'transparent',
- },
- });
- distances.push(distanceText);
- }
- const other = [...circles, text, ...distances]
- root.$map.add(other)
- coverMap[data.id + '_other'] = [other]
- polygon.on('click', function () {
- const options = polygon.getOptions()
- const id = options.extData.id;
- store.commit('SET_MAP_CLICK_ELEMENT', {
- id: id,
- type: 'DEFAULT',
- });
- })
- AddOverlayGroup(polygon)
- }
- function getPolygonArea(coordinates: GeojsonCoordinate[][]) {
- const path = [] as GeojsonCoordinate[]
- coordinates[0].forEach(coordinate => {
- path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
- })
- const polygon = new AMap.Polygon({
- path: path,
- })
- return polygon.getArea();
- }
- function AddOverlayGroup(overlayGroup: any) {
- root.$map.add(overlayGroup)
- const id = overlayGroup.getExtData().id
- coverMap[id] = [...(coverMap[id] || []), overlayGroup]
- }
- function removeCoverFromMap(id: string) {
- coverMap[id].forEach(cover => root.$map.remove(cover))
- coverMap[id] = []
- }
- function getElementFromMap(id: string): any[] {
- return coverMap[id]
- }
- function updatePhotoElement(id: string, name: string, url: string, coordinates: [number, number]) {
- const icon = new AMap.Icon({
- size: new AMap.Size(30, 30),
- image: url,
- imageSize: new AMap.Size(30, 30)
- })
- const marker = new AMap.Marker({
- position: new AMap.LngLat(coordinates[0], coordinates[1]),
- icon: icon,
- title: name,
- extData: {
- id: id
- }
- })
- marker.on('click', function (e: any) {
- const options = marker.getOptions()
- const id = options.extData.id;
- store.commit('SET_MAP_CLICK_ELEMENT', {
- id: id,
- type: 'PHOTO',
- });
- })
- AddCoverToMap(marker)
- }
- function updatePinElement(id: string, name: string, coordinates: GeojsonCoordinate, color?: string) {
- init2DPin(name, coordinates, color, {
- id: id,
- name: name
- })
- }
- function updatePolylineElement(id: string, name: string, coordinates: GeojsonCoordinate[], color?: string) {
- initPolyline(name, coordinates, color, {
- id: id,
- name: name
- })
- }
- function updatePolygonElement(id: string, name: string, coordinates: GeojsonCoordinate[][], color?: string) {
- initPolygon(name, coordinates, color, {
- id: id,
- name: name
- })
- }
- function initTextInfo(content: string, coordinates: GeojsonCoordinate, id: string) {
- const info = new AMap.Text({
- text: content,
- position: new AMap.LngLat(coordinates[0], coordinates[1]),
- extData: { id: id, type: 'text' },
- anchor: 'top-center',
- style: {
- background: 'none',
- borderStyle: 'none',
- fontSize: '16px',
- },
- })
- AddOverlayGroup(info)
- }
- function initFlightAreaCircle(name: string, radius: number, position: GeojsonCoordinate, data: { id: string, type: EFlightAreaType, enable: boolean }) {
- const circle = new AMap.Circle({
- strokeColor: data.enable ? flightAreaColorMap[data.type] : disableColor,
- strokeOpacity: 1,
- strokeWeight: 6,
- extData: data,
- strokeStyle: 'dashed',
- strokeDasharray: EFlightAreaType.NFZ === data.type ? [10, 2] : [10, 1, 2],
- fillColor: flightAreaColorMap[data.type],
- fillOpacity: EFlightAreaType.NFZ === data.type && data.enable ? 0.3 : 0,
- radius: radius,
- center: new AMap.LngLat(position[0], position[1]),
- })
- AddOverlayGroup(circle)
- initTextInfo(name, position, data.id)
- }
- function updateFlightAreaCircle(id: string, name: string, radius: number, position: GeojsonCoordinate, enable: boolean, type: EFlightAreaType) {
- const elements = getElementFromMap(id)
- if (elements && elements.length > 0) {
- let textIndex = elements.findIndex(ele => ele.getExtData()?.type === 'text')
- if (textIndex === -1) {
- textIndex = 1
- initTextInfo(name, position, id)
- } else {
- const text = elements[textIndex]
- text.setText(name)
- text.setPosition(position)
- }
- const element = elements[textIndex ^ 1]
- const options = element.getOptions()
- options.fillOpacity = EFlightAreaType.NFZ === type && enable ? 0.3 : 0
- options.strokeColor = enable ? flightAreaColorMap[type] : disableColor
- options.radius = radius
- options.center = new AMap.LngLat(position[0], position[1])
- element.setOptions(options)
- } else {
- initFlightAreaCircle(name, radius, position, { id, type, enable })
- }
- }
- function calcPolygonPosition(coordinate: GeojsonCoordinate[]): GeojsonCoordinate {
- const index = coordinate.length - 1
- return [(coordinate[0][0] + coordinate[index][0]) / 2.0, (coordinate[0][1] + coordinate[index][1]) / 2]
- }
- function initFlightAreaPolygon(name: string, coordinates: GeojsonCoordinate[], data: { id: string, type: EFlightAreaType, enable: boolean }) {
- const path = [] as GeojsonCoordinate[]
- coordinates.forEach(coordinate => {
- path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
- })
- const polygon = new AMap.Polygon({
- path: path,
- strokeColor: data.enable ? flightAreaColorMap[data.type] : disableColor,
- strokeOpacity: 1,
- strokeWeight: 4,
- draggable: true,
- extData: data,
- strokeStyle: 'dashed',
- strokeDasharray: EFlightAreaType.NFZ === data.type ? [10, 2] : [10, 1, 2],
- fillColor: flightAreaColorMap[data.type],
- fillOpacity: EFlightAreaType.NFZ === data.type && data.enable ? 0.3 : 0,
- })
- AddOverlayGroup(polygon)
- initTextInfo(name, calcPolygonPosition(coordinates), data.id)
- }
- function updateFlightAreaPolygon(id: string, name: string, coordinates: GeojsonCoordinate[], enable: boolean, type: EFlightAreaType) {
- const elements = getElementFromMap(id)
- if (elements && elements.length > 0) {
- let textIndex = elements.findIndex(ele => ele.getExtData()?.type === 'text')
- if (textIndex === -1) {
- textIndex = 1
- initTextInfo(name, calcPolygonPosition(coordinates), id)
- } else {
- const text = elements[textIndex]
- text.setText(name)
- text.setPosition(calcPolygonPosition(coordinates))
- }
- const element = elements[textIndex ^ 1]
- const options = element.getOptions()
- const path = [] as GeojsonCoordinate[]
- coordinates.forEach(coordinate => {
- path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
- })
- options.path = path
- options.fillOpacity = EFlightAreaType.NFZ === type && enable ? 0.3 : 0
- options.strokeColor = enable ? flightAreaColorMap[type] : disableColor
- element.setOptions(options)
- } else {
- initFlightAreaPolygon(name, coordinates, { id, type, enable })
- }
- }
- return {
- init2DPin,
- initPolyline,
- initPolygon,
- getPolygonArea,
- removeCoverFromMap,
- getElementFromMap,
- updatePhotoElement,
- updatePinElement,
- updatePolylineElement,
- updatePolygonElement,
- initFlightAreaCircle,
- initFlightAreaPolygon,
- updateFlightAreaPolygon,
- updateFlightAreaCircle,
- calcPolygonPosition,
- }
- }
|