Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
basic-uniapp-v3
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-uniapp-v3
Commits
1fa947a8
提交
1fa947a8
authored
8月 22, 2023
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 实现地图组件的相关常用方法
上级
80e4188f
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
175 行增加
和
2 行删除
+175
-2
hook.ts
src/components/Map/Mapbox/hook.ts
+36
-0
index.ts
src/components/Map/Mapbox/index.ts
+0
-0
index.vue
src/components/Map/Mapbox/index.vue
+0
-0
mapbox.module.js
src/components/Map/Mapbox/mapbox.module.js
+139
-2
没有找到文件。
src/components/Map/Mapbox/hook.ts
浏览文件 @
1fa947a8
import
type
mapboxgl
from
'mapbox-gl'
import
type
{
MapboxConfig
,
MapboxInstance
}
from
'./index'
import
{
isProdMode
}
from
'@/utils/env'
...
...
@@ -41,6 +42,41 @@ export function useMapbox<T extends MapboxInstance, P extends MapboxConfig>(
{
setConfig
:
(
config
:
Partial
<
P
>
)
=>
getInstance
()?.
setConfig
(
config
),
isReady
:
computed
(()
=>
instanceRef
?.
value
?.
isReady
)
as
unknown
as
ComputedRef
<
boolean
>
,
on
:
(
type
:
string
,
layerId
:
string
,
listener
:
(...
args
:
any
[])
=>
void
)
=>
getInstance
()?.
on
(
type
,
layerId
,
listener
),
addSource
:
(
id
:
string
,
source
:
mapboxgl
.
AnySourceData
)
=>
getInstance
()?.
addSource
(
id
,
source
),
removeSource
:
(
id
:
string
)
=>
getInstance
()?.
removeSource
(
id
),
setGeoJSONSourceForRequest
:
(
id
:
string
,
url
:
string
,
map
:
(
data
:
Recordable
,
)
=>
GeoJSON
.
Feature
<
GeoJSON
.
Geometry
>
|
GeoJSON
.
FeatureCollection
<
GeoJSON
.
Geometry
>
,
)
=>
getInstance
()?.
setGeoJSONSourceForRequest
(
id
,
url
,
map
),
setGeoJSONSourceData
:
(
id
:
string
,
data
:
mapboxgl
.
GeoJSONSource
[
'setData'
],
filter
?:
string
)
=>
getInstance
()?.
setGeoJSONSourceData
(
id
,
data
,
filter
),
setVectorTileSourceTiles
:
(
id
:
string
,
tiles
:
string
[])
=>
getInstance
()?.
setVectorTileSourceTiles
(
id
,
tiles
),
addLayer
:
(
layer
:
mapboxgl
.
Layer
)
=>
getInstance
()?.
addLayer
(
layer
),
removeLayer
:
(
id
:
string
)
=>
getInstance
()?.
removeLayer
(
id
),
setPaintProperty
:
(
layerId
:
string
,
name
:
string
,
value
:
any
)
=>
getInstance
()?.
setPaintProperty
(
layerId
,
name
,
value
),
setLayoutProperty
:
(
layerId
:
string
,
name
:
string
,
value
:
any
)
=>
getInstance
()?.
setLayoutProperty
(
layerId
,
name
,
value
),
setFilter
:
(
layerId
:
string
,
filter
:
any
[])
=>
getInstance
()?.
setFilter
(
layerId
,
filter
),
flyTo
:
(
options
:
mapboxgl
.
FlyToOptions
)
=>
getInstance
()?.
flyTo
(
options
),
},
]
}
export
function
buildGeoJSONSourceDataUrl
(
config
:
{
url
:
string
;
params
:
Recordable
}):
string
{
const
{
url
,
params
}
=
config
const
keys
=
Object
.
keys
(
params
)
if
(
keys
.
length
===
0
)
{
return
url
}
const
query
=
keys
.
map
((
key
)
=>
`
${
key
}
=
${
params
[
key
]}
`
).
join
(
'&'
)
return
`
${
url
}
?
${
query
}
`
}
src/components/Map/Mapbox/index.ts
浏览文件 @
1fa947a8
差异被折叠。
点击展开。
src/components/Map/Mapbox/index.vue
浏览文件 @
1fa947a8
差异被折叠。
点击展开。
src/components/Map/Mapbox/mapbox.module.js
浏览文件 @
1fa947a8
import
axios
from
'axios'
import
{
merge
}
from
'lodash-es'
import
{
defaultStyle
,
loadMapControl
,
loadMapboxLibs
}
from
'/@/components/Map/Mapbox'
import
{
HandlerUtil
,
defaultStyle
,
loadMapControl
,
loadMapboxLibs
}
from
'/@/components/Map/Mapbox'
// renderjs 官方文档
// https://uniapp.dcloud.io/tutorial/renderjs.html
// renderjs 的一些细节问题
// https://juejin.cn/post/7049185827582115870
async
function
request
(
url
,
handler
)
{
const
res
=
await
axios
.
get
(
url
)
if
(
handler
)
{
// TODO: 待优化
// 1. 考虑是否为函数
// 2. 考虑箭头函数的情况,可能意外的兼容性问题
// HandlerUtil 为 renderjs 提供的工具类,用于处理一些常用的数据转换,需要保证逻辑层同样使用此工具类进行处理
console
.
debug
(
'handler'
,
handler
,
HandlerUtil
)
// eslint-disable-next-line no-eval
const
executableFunction
=
eval
(
`(
${
handler
}
)`
)
return
executableFunction
(
res
.
data
)
}
return
res
.
data
}
const
defaultEmptyGeoJSON
=
{
type
:
'FeatureCollection'
,
features
:
[],
}
export
default
{
mounted
()
{},
methods
:
{
...
...
@@ -102,12 +127,19 @@ export default {
// TODO: 加载地图图层
return
Promise
.
resolve
()
},
// 检测触发变更时候的有效性
checkOnChangeValidity
(
options
)
{
return
options
&&
Object
.
keys
(
options
).
length
>
0
&&
this
.
map
},
emitChangeEvent
(
type
)
{
// 通知逻辑层,本次变更事件已经送达
this
.
$ownerInstance
.
callMethod
(
'onMapOptionsChangeEvent'
,
{
type
})
},
changeOptions
(
options
)
{
if
(
!
options
.
container
)
{
return
}
console
.
log
(
options
)
if
(
typeof
window
.
mapboxgl
===
'object'
)
{
this
.
initMap
(
options
)
}
else
{
...
...
@@ -116,5 +148,110 @@ export default {
})
}
},
changeFlyToOptions
(
options
)
{
this
.
emitChangeEvent
(
'flyTo'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
flyTo
(
options
)
}
},
changeAddSourceOptions
(
options
)
{
this
.
emitChangeEvent
(
'addSource'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
addSource
(
options
.
id
,
options
.
source
)
}
},
changeSetGeoJSONSourceForRequestOptions
(
options
)
{
this
.
emitChangeEvent
(
'setGeoJSONSourceForRequest'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
if
(
this
.
map
.
getSource
(
options
.
id
))
{
// 清空数据
this
.
map
.
getSource
(
options
.
id
).
setData
(
defaultEmptyGeoJSON
)
}
else
{
// 添加空的 source
this
.
map
.
addSource
(
options
.
id
,
{
type
:
'geojson'
,
data
:
defaultEmptyGeoJSON
,
})
}
// 请求数据
request
(
options
.
url
,
options
.
handler
).
then
((
data
)
=>
{
this
.
map
.
getSource
(
options
.
id
).
setData
(
data
)
})
}
},
changeSetGeoJSONSourceDataOptions
(
options
)
{
this
.
emitChangeEvent
(
'setGeoJSONSourceData'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
getSource
(
options
.
id
).
setData
(
options
.
data
)
}
},
changeSetVectorTileSourceTilesOptions
(
options
)
{
this
.
emitChangeEvent
(
'setVectorTileSourceTiles'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
getSource
(
options
.
id
).
setTiles
(
options
.
tiles
)
}
},
changeAddLayerOptions
(
options
)
{
this
.
emitChangeEvent
(
'addLayer'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
addLayer
(
options
.
layer
,
options
.
beforeId
)
}
},
changeRemoveOptions
(
options
)
{
this
.
emitChangeEvent
(
'remove'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
if
(
options
.
type
===
'layer'
)
{
this
.
map
.
removeLayer
(
options
.
id
)
}
else
if
(
options
.
type
===
'source'
)
{
this
.
map
.
removeSource
(
options
.
id
)
}
}
},
changeSetFilterOptions
(
options
)
{
this
.
emitChangeEvent
(
'filter'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
setFilter
(
options
.
layer
,
options
.
filter
)
}
},
changeSetLayoutPropertyOptions
(
options
)
{
this
.
emitChangeEvent
(
'setLayoutProperty'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
setLayoutProperty
(
options
.
layer
,
options
.
name
,
options
.
value
)
}
},
changeSetPaintPropertyOptions
(
options
)
{
this
.
emitChangeEvent
(
'setPaintProperty'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
this
.
map
.
setPaintProperty
(
options
.
layer
,
options
.
name
,
options
.
value
)
}
},
changeEventOptions
(
options
)
{
this
.
emitChangeEvent
(
'event'
)
if
(
this
.
checkOnChangeValidity
(
options
))
{
if
(
options
.
event
===
'on'
)
{
if
(
options
.
type
===
'click'
)
{
this
.
map
.
on
(
options
.
type
,
options
.
layer
,
(
e
)
=>
{
console
.
log
(
'e'
,
e
)
const
feature
=
e
.
features
[
0
]
new
window
.
mapboxgl
.
Popup
()
.
setLngLat
(
feature
.
geometry
.
coordinates
)
.
setHTML
(
feature
.
properties
.
popup
.
replace
(
'{key}'
,
feature
.
properties
.
key
)
.
replace
(
'{value}'
,
feature
.
properties
.
value
)
.
replace
(
'{suffix}'
,
feature
.
properties
.
suffix
||
''
),
)
.
addTo
(
this
.
map
)
this
.
$ownerInstance
.
callMethod
(
'onEvent'
,
options
)
})
}
else
{
this
.
map
.
on
(
options
.
type
,
options
.
layer
,
()
=>
{})
}
}
else
if
(
options
.
event
===
'off'
)
{
this
.
map
.
off
(
options
.
type
,
options
.
layer
,
()
=>
{})
}
}
},
},
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论