Commit 1e290eb3 authored by 郭铭瑶's avatar 郭铭瑶 🤘

约束初始化参数

parent 57e6f3fa
...@@ -17,7 +17,6 @@ let map: MyMap ...@@ -17,7 +17,6 @@ let map: MyMap
onMounted(async () => { onMounted(async () => {
await nextTick() await nextTick()
map = useMap('SMap').with({ map = useMap('SMap').with({
el: 'container', el: 'container',
center: [0, 0], center: [0, 0],
......
...@@ -8,6 +8,8 @@ import { ...@@ -8,6 +8,8 @@ import {
FocusOptions, FocusOptions,
Location, Location,
ClickCallBack, ClickCallBack,
Controls,
AIMapConfig,
} from './types' } from './types'
declare const aimap: { declare const aimap: {
...@@ -20,7 +22,7 @@ declare const aimap: { ...@@ -20,7 +22,7 @@ declare const aimap: {
} }
export default class AI_Map extends MyMap { export default class AI_Map extends MyMap {
constructor(config: MapConfig) { constructor(config: MapConfig<AIMapConfig>) {
if (!config.appKey) { if (!config.appKey) {
throw new Error('AIMap需要设置appKey参数!') throw new Error('AIMap需要设置appKey参数!')
} }
...@@ -89,27 +91,27 @@ export default class AI_Map extends MyMap { ...@@ -89,27 +91,27 @@ export default class AI_Map extends MyMap {
} }
private _setControls() { private _setControls() {
const _controls = { const _controls: Partial<Controls> = {
compass: (options: ControlOptions) => { compass: (options?: ControlOptions) => {
this.map.addControl(new aimap.CompassControl(), options.position) this.map.addControl(new aimap.CompassControl(), options?.position)
}, },
zoom: (options: ControlOptions) => { zoom: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new aimap.NavigationControl({ new aimap.NavigationControl({
showZoom: options.show, showZoom: options?.show,
showCompass: false, showCompass: false,
visualizePitch: false, visualizePitch: false,
}), }),
options.position, options?.position,
) )
}, },
scale: (options: ControlOptions) => { scale: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new aimap.ScaleControl({ new aimap.ScaleControl({
maxWidth: options.maxWidth || 80, maxWidth: options?.maxWidth || 80,
unit: options.unit || 'metric', unit: options?.unit || 'metric',
}), }),
options.position, options?.position,
) )
}, },
} }
......
import { MapConfig } from './types' import { AIMapConfig, MapConfig, SMapConfig } from './types'
import S_Map from './s-map' import S_Map from './s-map'
import AI_Map from './ai-map' import AI_Map from './ai-map'
const whichMap = { const whichMap = {
SMap: { SMap: {
with: (config: MapConfig) => new S_Map(config), /**
* 使用配置初始化地图
* @param config 地图配置
*/
with: (config: MapConfig<SMapConfig>) => new S_Map(config),
}, },
AIMap: { AIMap: {
with: (config: MapConfig) => new AI_Map(config), /**
* 使用配置初始化地图
* @param config 地图配置
*/
with: (config: MapConfig<AIMapConfig>) => new AI_Map(config),
}, },
} }
/**
* 创建地图
* @param key 地图关键字:
*
* 'SMap': 测绘院地图
*
* 'AIMap': 维智地图
*
*/
export default function useMap<K extends keyof typeof whichMap>(key: K) { export default function useMap<K extends keyof typeof whichMap>(key: K) {
return whichMap[key] return whichMap[key]
} }
...@@ -4,9 +4,10 @@ import { ...@@ -4,9 +4,10 @@ import {
MapConfig, MapConfig,
Listeners, Listeners,
ControlOptions, ControlOptions,
ZoomOptions,
Location, Location,
FocusOptions, FocusOptions,
Controls,
SMapConfig,
} from './types' } from './types'
declare const SMap: { declare const SMap: {
...@@ -34,7 +35,7 @@ declare const Plugins: { ...@@ -34,7 +35,7 @@ declare const Plugins: {
} }
export default class S_Map extends MyMap { export default class S_Map extends MyMap {
constructor(config: MapConfig) { constructor(config: MapConfig<SMapConfig>) {
if (!config.netType) { if (!config.netType) {
throw new Error('SMap需要设置netType参数!') throw new Error('SMap需要设置netType参数!')
} }
...@@ -116,92 +117,92 @@ export default class S_Map extends MyMap { ...@@ -116,92 +117,92 @@ export default class S_Map extends MyMap {
} }
private _setControls() { private _setControls() {
const _controls = { const _controls: Partial<Controls> = {
home: (options: ControlOptions) => { home: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.Home({ new SMap.Home({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
compass: (options: ControlOptions) => { compass: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.Compass({ new SMap.Compass({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
zoom: (options: ControlOptions) => { zoom: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.Zoom({ new SMap.Zoom({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
fullScreen: (options: ControlOptions) => { fullScreen: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.Fullscreen({ new SMap.Fullscreen({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
layerList: (options: ControlOptions) => { layerList: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.LayerListControl({ new SMap.LayerListControl({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
measureLine: (options: ControlOptions) => { measureLine: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.MeasureLine({ new SMap.MeasureLine({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
measureArea: (options: ControlOptions) => { measureArea: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.MeasureArea({ new SMap.MeasureArea({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
basemapToggle: (options: ControlOptions) => { basemapToggle: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.BasemapToggle({ new SMap.BasemapToggle({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
underguroundSwitch: (options: ControlOptions) => { underguroundSwitch: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.UndergroundSwitch({ new SMap.UndergroundSwitch({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
bMapGallery: (options: ControlOptions) => { bMapGallery: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.BMapGallery({ new SMap.BMapGallery({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
bMapGalleryexpand: (options: ControlOptions) => { bMapGalleryexpand: (options?: ControlOptions) => {
this.map.addControl( this.map.addControl(
new SMap.BMapGalleryExpand({ new SMap.BMapGalleryExpand({
visible: options.show, visible: options?.show,
position: options.position, position: options?.position,
}), }),
) )
}, },
......
/** /**
* 地图配置 * 地图配置
*/ */
export type MapConfig = Partial<{ export type MapConfig<T> = T & {
/** 地图容器(需要有宽高) */ /** 地图容器(需要有宽高) */
el: string el: string
/** /**
* SMap 地图专用 * 在SMap中作为appKey使用
* @param internet 表示互联网2D * 在AIMap中作为accessToken使用
* @param affairs 表示政务网2D
* @param local3D 表示局域网3D
* @param affairs3D 表示政务网3D
* @param njdl 表示南京东路政务网3D
*/ */
netType: 'internet' | 'affairs' | 'local3D' | 'affairs3D' | 'njdl' appKey: string
} & Partial<{
/** 地图模式 */ /** 地图模式 */
mode: '2D' | '3D' mode: '2D' | '3D'
/** 地图初始中心点位 */ /** 地图初始中心点位 */
...@@ -29,16 +26,25 @@ export type MapConfig = Partial<{ ...@@ -29,16 +26,25 @@ export type MapConfig = Partial<{
pitchs: [number, number] pitchs: [number, number]
/** 地图样式 */ /** 地图样式 */
style: string style: string
/**
* 在SMap中作为appKey使用
* 在AIMap中作为accessToken使用
*/
appKey: string
showBuildingBlock: boolean showBuildingBlock: boolean
rotateEnable: boolean rotateEnable: boolean
baseApiUrl: string
family: string family: string
}> }>
export type SMapConfig = {
/**
* SMap 地图专用
* @param internet 表示互联网2D
* @param affairs 表示政务网2D
* @param local3D 表示局域网3D
* @param affairs3D 表示政务网3D
* @param njdl 表示南京东路政务网3D
*/
netType: 'internet' | 'affairs' | 'local3D' | 'affairs3D' | 'njdl'
}
export type AIMapConfig = {
baseApiUrl: string
}
export type Location = [number, number] | [number, number, number] export type Location = [number, number] | [number, number, number]
......
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