Commit 02f564cd authored by 郭铭瑶's avatar 郭铭瑶 🤘

添加面状覆盖物

parent cdc77c41
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<div @click="move">Move</div> <div @click="move">Move</div>
<div @click="addPoints">Point</div> <div @click="addPoints">Point</div>
<div @click="addLines">Line</div> <div @click="addLines">Line</div>
<div @click="addPolygon">Polygon</div>
<div @click="removeLayers">Remove</div> <div @click="removeLayers">Remove</div>
</div> </div>
</div> </div>
...@@ -20,28 +21,29 @@ import { nextTick, onMounted } from 'vue' ...@@ -20,28 +21,29 @@ import { nextTick, onMounted } from 'vue'
let map: MyMap let map: MyMap
let pointsLayer: any let pointsLayer: any
let linesLayer: any let linesLayer: any
let polygonLayer: any
onMounted(async () => { onMounted(async () => {
await nextTick() await nextTick()
// map = useMap('SMap').with({ map = useMap('SMap').with({
// el: 'container',
// center: [0, 0],
// zoom: 5,
// style: 'smap://styles/dark',
// appKey: 'ACF69EDK17LON63GHPF081',
// netType: 'internet',
// })
map = useMap('AIMap').with({
el: 'container', el: 'container',
center: [121.612846, 31.205494], center: [0, 0],
zoom: 13, zoom: 5,
style: 'aimap://styles/aimap/darkblue-v4', style: 'smap://styles/dark',
appKey: 'gt8XidVe5upHf7cirkJwwXTCWj20zfu3', appKey: 'ACF69EDK17LON63GHPF081',
baseApiUrl: 'https://location-dev.newayz.com', netType: 'internet',
}) })
// map = useMap('AIMap').with({
// el: 'container',
// center: [121.612846, 31.205494],
// zoom: 13,
// style: 'aimap://styles/aimap/darkblue-v4',
// appKey: 'gt8XidVe5upHf7cirkJwwXTCWj20zfu3',
// baseApiUrl: 'https://location-dev.newayz.com',
// })
map map
.on('load', addControls) .on('load', addControls)
.on('click', (a, b) => console.log(a, b)) .on('click', (a, b) => console.log(a, b))
...@@ -112,11 +114,26 @@ function addLines() { ...@@ -112,11 +114,26 @@ function addLines() {
], ],
text: 'test message', text: 'test message',
}, },
labelKey: 'text',
})
}
function addPolygon() {
polygonLayer = map.add('polygon', {
data: {
path: [
[0, 0],
[121.59751247938203, 29.835174764721145],
[21.59751247938203, 0.835174764721145],
[0, 0],
],
text: 'test message',
},
}) })
} }
function removeLayers() { function removeLayers() {
map.remove(pointsLayer) map.remove(pointsLayer)
map.remove(linesLayer) map.remove(linesLayer)
map.remove(polygonLayer)
} }
</script> </script>
...@@ -136,7 +153,7 @@ function removeLayers() { ...@@ -136,7 +153,7 @@ function removeLayers() {
margin: 10px; margin: 10px;
background: #fff; background: #fff;
color: #000; color: #000;
min-width: 40px; min-width: 50px;
text-align: center; text-align: center;
cursor: pointer; cursor: pointer;
} }
......
...@@ -13,7 +13,7 @@ import { ...@@ -13,7 +13,7 @@ import {
Layers, Layers,
LayerOption, LayerOption,
} from '../types' } from '../types'
import { toLines, toPoints } from './util' import { toLines, toPoints, toPolygons } from './util'
export default class AI_Map extends MyMap { export default class AI_Map extends MyMap {
private _clickCb: ClickCallBack | null = null private _clickCb: ClickCallBack | null = null
...@@ -119,43 +119,30 @@ export default class AI_Map extends MyMap { ...@@ -119,43 +119,30 @@ export default class AI_Map extends MyMap {
/** 设置覆盖物 */ /** 设置覆盖物 */
private _setLayers() { private _setLayers() {
const _markers: Partial<Layers> = { const makeLayer = (layer: any) => {
point: (data: LayerOption) => { layer.on('click', (e: any) => {
const layer = new aimap.MassMarkerLayer(toPoints(this.map, data)) const arg = {
// 维智的覆盖物点击事件需特别定义 type: e.type,
layer.on('click', (e: any) => { center: [e.lngLat.lng, e.lngLat.lat],
const arg = { event: e.originalEvent,
type: e.type, clientX: e.point.x,
center: [e.lngLat.lng, e.lngLat.lat], clientY: e.point.y,
event: e.originalEvent, originData: e,
clientX: e.point.x, }
clientY: e.point.y, // 点击覆盖物获取自定义数据
originData: e, this._clickCb && this._clickCb(arg, e?.features?.[0]?.properties)
} })
// 点击覆盖物获取自定义数据 return layer
this._clickCb && this._clickCb(arg, e?.features?.[0]?.properties) }
}) const _layers: Partial<Layers> = {
return layer point: (data: LayerOption) =>
}, makeLayer(new aimap.MassMarkerLayer(toPoints(this.map, data))),
line: (data: LayerOption) => { line: (data: LayerOption) =>
const layer = new aimap.MassMarkerLayer(toLines(this.map, data)) makeLayer(new aimap.LineString(toLines(this.map, data))),
// 维智的覆盖物点击事件需特别定义 polygon: (data: LayerOption) =>
layer.on('click', (e: any) => { makeLayer(new aimap.Polygon(toPolygons(this.map, data))),
const arg = {
type: e.type,
center: [e.lngLat.lng, e.lngLat.lat],
event: e.originalEvent,
clientX: e.point.x,
clientY: e.point.y,
originData: e,
}
// 点击覆盖物获取自定义数据
this._clickCb && this._clickCb(arg, e?.features?.[0]?.properties)
})
return layer
},
} }
this._markers = Object.assign(this._markers, _markers) this._layers = Object.assign(this._layers, _layers)
} }
zoomIn(options: ZoomOptions) { zoomIn(options: ZoomOptions) {
......
...@@ -92,3 +92,41 @@ export function toLines(map: unknown, options: LayerOption) { ...@@ -92,3 +92,41 @@ export function toLines(map: unknown, options: LayerOption) {
}, },
} }
} }
/** 转化面状覆盖物 */
export function toPolygons(map: unknown, options: LayerOption) {
const {
data,
lineWidth = 3,
color = 'blue',
fillColor = 'rgba(135,206,235,0.5)',
labelKey,
spatialReference = 'gcj02',
labelOptions,
lineJoin = 'round',
} = options
const [horizontal, vertical] = labelOptions?.offset || []
return {
map,
spatialReference: spatialReference,
data: {
coordinates: Array.isArray(data)
? data.map(({ path }) => path?.map((e) => e))
: data.path,
...data,
},
style: {
'line-width': lineWidth,
'line-color': color,
'fill-color': fillColor,
'line-join': lineJoin,
'text-field': `{${labelKey}}`,
'text-color': labelOptions?.color,
// 偏移相比测绘院地图大概小了10倍,且竖直偏移相反
'text-offset': [
horizontal ? horizontal / 10 : 0,
vertical ? vertical / -10 : 0,
],
},
}
}
...@@ -6,6 +6,7 @@ declare const SMap: { ...@@ -6,6 +6,7 @@ declare const SMap: {
Label: any Label: any
Marker: any Marker: any
Polyline: any Polyline: any
Polygon: any
OverlayGroup: any OverlayGroup: any
Network: any Network: any
Home: any Home: any
...@@ -32,4 +33,6 @@ declare const aimap: { ...@@ -32,4 +33,6 @@ declare const aimap: {
CompassControl: any CompassControl: any
ScaleControl: any ScaleControl: any
MassMarkerLayer: any MassMarkerLayer: any
LineString: any
Polygon: any
} }
...@@ -14,7 +14,7 @@ export default abstract class MyMap { ...@@ -14,7 +14,7 @@ export default abstract class MyMap {
protected map protected map
protected _listeners: Listeners protected _listeners: Listeners
protected _controls: Controls protected _controls: Controls
protected _markers: Layers protected _layers: Layers
constructor(instance: any) { constructor(instance: any) {
this.map = instance this.map = instance
...@@ -88,7 +88,7 @@ export default abstract class MyMap { ...@@ -88,7 +88,7 @@ export default abstract class MyMap {
console.error('set:此地图不存在 bMapGalleryexpand 控件!') console.error('set:此地图不存在 bMapGalleryexpand 控件!')
}, },
} }
this._markers = { this._layers = {
point: () => console.error('add:此地图不存在 point 覆盖物!'), point: () => console.error('add:此地图不存在 point 覆盖物!'),
line: () => console.error('add:此地图不存在 line 覆盖物!'), line: () => console.error('add:此地图不存在 line 覆盖物!'),
} }
...@@ -130,7 +130,7 @@ export default abstract class MyMap { ...@@ -130,7 +130,7 @@ export default abstract class MyMap {
* @param data 覆盖物数据 * @param data 覆盖物数据
*/ */
add<K extends keyof Layers>(type: K, data: LayerOption) { add<K extends keyof Layers>(type: K, data: LayerOption) {
return this._markers[type](data) return this._layers[type](data)
} }
/** /**
......
...@@ -12,7 +12,7 @@ import { ...@@ -12,7 +12,7 @@ import {
LayerOption, LayerOption,
ClickCallBack, ClickCallBack,
} from '../types' } from '../types'
import { toLines, toPoints } from './util' import { toLines, toPoints, toPolygons } from './util'
export default class S_Map extends MyMap { export default class S_Map extends MyMap {
constructor(config: MapConfig<SMapConfig>) { constructor(config: MapConfig<SMapConfig>) {
...@@ -104,94 +104,47 @@ export default class S_Map extends MyMap { ...@@ -104,94 +104,47 @@ export default class S_Map extends MyMap {
/** 设置控制器 */ /** 设置控制器 */
private _setControls() { private _setControls() {
const makeControl = (constructorFn: any, options?: ControlOptions) => {
this.map.addControl(
new constructorFn({
visible: options?.show,
position: options?.position,
}),
)
}
const _controls: Partial<Controls> = { const _controls: Partial<Controls> = {
home: (options?: ControlOptions) => { home: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.Home, options)
new SMap.Home({
visible: options?.show,
position: options?.position,
}),
)
}, },
compass: (options?: ControlOptions) => { compass: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.Compass, options)
new SMap.Compass({
visible: options?.show,
position: options?.position,
}),
)
}, },
zoom: (options?: ControlOptions) => { zoom: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.Zoom, options)
new SMap.Zoom({
visible: options?.show,
position: options?.position,
}),
)
}, },
fullScreen: (options?: ControlOptions) => { fullScreen: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.Fullscreen, options)
new SMap.Fullscreen({
visible: options?.show,
position: options?.position,
}),
)
}, },
layerList: (options?: ControlOptions) => { layerList: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.LayerListControl, options)
new SMap.LayerListControl({
visible: options?.show,
position: options?.position,
}),
)
}, },
measureLine: (options?: ControlOptions) => { measureLine: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.MeasureLine, options)
new SMap.MeasureLine({
visible: options?.show,
position: options?.position,
}),
)
}, },
measureArea: (options?: ControlOptions) => { measureArea: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.MeasureArea, options)
new SMap.MeasureArea({
visible: options?.show,
position: options?.position,
}),
)
}, },
basemapToggle: (options?: ControlOptions) => { basemapToggle: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.BasemapToggle, options)
new SMap.BasemapToggle({
visible: options?.show,
position: options?.position,
}),
)
}, },
underguroundSwitch: (options?: ControlOptions) => { underguroundSwitch: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.UndergroundSwitch, options)
new SMap.UndergroundSwitch({
visible: options?.show,
position: options?.position,
}),
)
}, },
bMapGallery: (options?: ControlOptions) => { bMapGallery: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.BMapGallery, options)
new SMap.BMapGallery({
visible: options?.show,
position: options?.position,
}),
)
}, },
bMapGalleryexpand: (options?: ControlOptions) => { bMapGalleryexpand: (options?: ControlOptions) => {
this.map.addControl( makeControl(SMap.BMapGalleryExpand, options)
new SMap.BMapGalleryExpand({
visible: options?.show,
position: options?.position,
}),
)
}, },
} }
this._controls = Object.assign(this._controls, _controls) this._controls = Object.assign(this._controls, _controls)
...@@ -199,19 +152,17 @@ export default class S_Map extends MyMap { ...@@ -199,19 +152,17 @@ export default class S_Map extends MyMap {
/** 设置覆盖物 */ /** 设置覆盖物 */
private _setLayers() { private _setLayers() {
const _markers: Partial<Layers> = { const makeLayer = (group: unknown[]) => {
point: (data: LayerOption) => { const layer = new SMap.OverlayGroup(group, {})
const layer = new SMap.OverlayGroup(toPoints(data), {}) this.map.add(layer)
this.map.add(layer) return layer
return layer }
}, const _layers: Partial<Layers> = {
line: (data: LayerOption) => { point: (data: LayerOption) => makeLayer(toPoints(data)),
const layer = new SMap.OverlayGroup(toLines(data), {}) line: (data: LayerOption) => makeLayer(toLines(data)),
this.map.add(layer) polygon: (data: LayerOption) => makeLayer(toPolygons(data)),
return layer
},
} }
this._markers = Object.assign(this._markers, _markers) this._layers = Object.assign(this._layers, _layers)
} }
zoomIn() { zoomIn() {
......
...@@ -83,3 +83,48 @@ export function toLines(options: LayerOption) { ...@@ -83,3 +83,48 @@ export function toLines(options: LayerOption) {
return [transData(data)] return [transData(data)]
} }
} }
/** 转化面状覆盖物 */
export function toPolygons(options: LayerOption) {
const {
data,
color = 'blue',
fillColor = 'rgba(135,206,235,0.5)',
labelKey,
labelOptions,
lineCap = 'square',
lineStyle = 'solid',
lineJoin = 'round',
lineWidth = 3,
} = options
const transData = (item: PointsData) => {
const result: any = {
paths: item.path?.map(([x, y]) => new SMap.LngLat(x, y)),
attributes: { ...item },
cap: lineCap,
strokeColor: color,
fillColor,
strokeWeight: lineWidth,
style: lineStyle,
lineJoin: lineJoin,
}
if (labelKey) {
result.label = new SMap.Label({
text: item[labelKey] + '',
size: labelOptions?.size,
color: labelOptions?.color,
xoffset: labelOptions?.offset?.[0],
yoffset: labelOptions?.offset?.[1],
verticalAlignment: 'middle',
horizontalAlignment: 'center',
})
}
return new SMap.Polygon(result)
}
if (Array.isArray(data)) {
return data.map(transData)
} else {
return [transData(data)]
}
}
...@@ -130,21 +130,19 @@ export interface ControlOptions { ...@@ -130,21 +130,19 @@ export interface ControlOptions {
* 控制器列表 * 控制器列表
*/ */
export interface Controls { export interface Controls {
home: (options?: ControlOptions) => unknown home: (options?: ControlOptions) => void
compass: (options?: ControlOptions) => unknown compass: (options?: ControlOptions) => void
zoom: (options?: ControlOptions) => unknown zoom: (options?: ControlOptions) => void
scale: (options?: ControlOptions) => unknown scale: (options?: ControlOptions) => void
fullScreen: (options?: ControlOptions) => unknown fullScreen: (options?: ControlOptions) => void
layerList: (options?: ControlOptions) => unknown layerList: (options?: ControlOptions) => void
measureLine: (options?: ControlOptions) => unknown measureLine: (options?: ControlOptions) => void
measureArea: (options?: ControlOptions) => unknown measureArea: (options?: ControlOptions) => void
basemapToggle: (options?: ControlOptions) => unknown basemapToggle: (options?: ControlOptions) => void
underguroundSwitch: (options?: ControlOptions) => unknown underguroundSwitch: (options?: ControlOptions) => void
bMapGallery: (options?: ControlOptions) => unknown bMapGallery: (options?: ControlOptions) => void
bMapGalleryexpand: (options?: ControlOptions) => unknown bMapGalleryexpand: (options?: ControlOptions) => void
} }
export interface ZoomOptions { export interface ZoomOptions {
/** 如果 false ,则没有动画效果(默认true) */ /** 如果 false ,则没有动画效果(默认true) */
animate?: boolean animate?: boolean
...@@ -179,6 +177,7 @@ export interface FocusOptions { ...@@ -179,6 +177,7 @@ export interface FocusOptions {
export interface Layers { export interface Layers {
point: (data: LayerOption) => unknown point: (data: LayerOption) => unknown
line: (data: LayerOption) => unknown line: (data: LayerOption) => unknown
polygon: (data: LayerOption) => unknown
} }
/** 覆盖物点位数据 */ /** 覆盖物点位数据 */
...@@ -215,6 +214,7 @@ export interface LayerOption { ...@@ -215,6 +214,7 @@ export interface LayerOption {
color?: string color?: string
} }
color?: string color?: string
fillColor?: string
lineCap?: string lineCap?: string
lineStyle?: string lineStyle?: string
lineJoin?: 'bevel' | 'round' | 'miter' lineJoin?: 'bevel' | 'round' | 'miter'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment