| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- 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 marker = new AMap.Marker({
- position: new AMap.LngLat(coordinates[0], coordinates[1]),
- title: name,
- icon: getPinIcon(color),
- extData: data
- })
- const text = new AMap.Text({
- position: new AMap.LngLat(coordinates[0], coordinates[1]),
- offset: new AMap.Pixel(-30, -30),
- text: name,
- style: {
- fontSize: 12,
- padding: 0,
- backgroundColor: 'transparent',
- borderColor: 'transparent',
- },
- extData: {
- ...data,
- id: data.id + '_text',
- },
- })
- 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 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: {
- fontSize: 12,
- padding: 0,
- backgroundColor: 'transparent',
- borderColor: 'transparent',
- },
- extData: {
- ...data,
- id: data.id + '_text',
- },
- })
- AddCoverToMap(text)
- 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 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: {
- fontSize: 12,
- padding: 0,
- backgroundColor: 'transparent',
- borderColor: 'transparent',
- },
- extData: {
- ...data,
- id: data.id + '_text',
- },
- })
- AddCoverToMap(text)
- 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 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,
- removeCoverFromMap,
- getElementFromMap,
- updatePhotoElement,
- updatePinElement,
- updatePolylineElement,
- updatePolygonElement,
- initFlightAreaCircle,
- initFlightAreaPolygon,
- updateFlightAreaPolygon,
- updateFlightAreaCircle,
- calcPolygonPosition,
- }
- }
|