-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathindex.js
345 lines (319 loc) · 8.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// @flow
import React from 'react'
import APILoader from '../utils/APILoader'
import isFun from '../utils/isFun'
import log from '../utils/log'
import { toLnglat } from '../utils/common'
import withPropsReactive from '../utils/withPropsReactive'
const Component = React.Component
const Children = React.Children
const containerStyle = {
width: '100%',
height: '100%'
}
const wrapperStyle = {
width: '100%',
height: '100%',
position: 'relative'
}
// Native supported dynamic props by Amap
const NativeDynamicProps: Array<string> = [
'layers',
'zoom',
'center',
'labelzIndex',
// 'lang', native error in JSSDK when 3D viewMode
'mapStyle',
'features',
'cursor',
'pitch'
]
/*
* Props below can set by 'setStatus' altogether
*/
const StatusDynamicProps: Array<string> = [
'animateEnable',
'doubleClickZoom',
'dragEnable',
'isHotspot',
'jogEnable',
'keyboardEnable',
'resizeEnable',
'rotateEnable',
'scrollWheel',
'touchZoom',
'zoomEnable',
'touchZoomCenter'
]
const StaticProps: Array<string> = [
'view',
'zooms',
'showIndoorMap',
'indoorMap',
'expandZoomRange',
'showBuildingBlock',
'viewMode',
'pitchEnable',
'buildingAnimation',
'skyColor'
]
const CreateProps = NativeDynamicProps.concat(StatusDynamicProps, StaticProps)
// const reservedPropName = [
// 'amapkey',
// 'version',
// 'useAMapUI',
// 'onInstanceCreated',
// 'events',
// 'loading',
// 'plugins'
// ]
const defaultOpts = {
MapType: {
showRoad: false,
showTraffic: false,
defaultType: 0
},
ToolBar: {
position: 'RB',
noIpLocate: true,
locate: true,
liteStyle: true,
autoPosition: false
},
OverView: {},
ControlBar: {}
}
class BaseMap extends Component<MapProps, {mapLoaded: boolean}> {
pluginMap: Object
loader: Object
map: Object
mapWrapper: ?HTMLDivElement
setterMap: Object
converterMap: Object
constructor(props: MapProps) {
super(props)
this.state = {
mapLoaded: false
}
const self = this
this.setterMap = {
zoom(val) {
self.map.setZoom(val)
},
cursor(val) {
self.map.setDefaultCursor(val)
},
labelzIndex(val) {
self.map.setlabelzIndex(val)
}
}
this.converterMap = {
center: toLnglat,
mapStyle: styleStr => {
if (styleStr.indexOf('amap://styles') === 0) {
return styleStr
}
return `amap://styles/${styleStr}`
}
}
if (typeof window !== 'undefined') {
this.pluginMap = {}
new APILoader({
key: props.amapkey,
useAMapUI: props.useAMapUI,
version: props.version,
protocol: props.protocol
}).load().then(() => {
this.createInstance()
if (!this.state.mapLoaded) {
this.setState({
mapLoaded: true
})
}
})
}
}
get instance() {
return this.map
}
componentWillReceiveProps(nextProps: MapProps) {
if (this.state.mapLoaded) {
this.updateMapProps(this.props, nextProps)
}
}
renderChildren() {
return Children.map(this.props.children, (child) => {
if (child) {
const cType = child.type
/* 针对下面两种组件不注入地图相关属性
* 1. 明确声明不需要注入的
* 2. DOM 元素
*/
if (cType.preventAmap || (typeof cType === 'string')) {
return child
}
return React.cloneElement(child, {
__map__: this.map
})
}
return child
})
}
createInstance() {
if (!this.map) {
const options = this.buildCreateOptions()
this.map = new window.AMap.Map(this.mapWrapper, options)
// install map plugins
this.setPlugins(this.props)
this.props.onInstanceCreated && this.props.onInstanceCreated()
}
}
buildCreateOptions() {
const props = this.props
const options = {}
CreateProps.forEach((key) => {
if (key in props) {
options[key] = this.getSetterValue(key, props)
}
})
return options
}
updateMapProps(prevProps: MapProps, nextProps: MapProps) {
const nextMapStatus = {}
let statusChangeFlag = false
let statusPropExist = false
StatusDynamicProps.forEach((key) => {
if (key in nextProps) {
statusPropExist = true
if (this.detectPropChanged(key, prevProps, nextProps)) {
statusChangeFlag = true
nextMapStatus[key] = nextProps[key]
}
}
})
statusChangeFlag && this.map.setStatus(nextMapStatus)
if (statusPropExist && 'status' in nextProps) {
log.warning(`以下这些属性可以单独提供进行配置,也可以统一作为‘status’属性配置;但是请不要同时使用这两种方式。\n(${StatusDynamicProps.join(', ')})`)
}
StaticProps.forEach((key) => {
if (key in nextProps) {
if (this.detectPropChanged(key, prevProps, nextProps)) {
log.warning(`'${key}' 是一个静态属性,地图实例创建成功后无法修改`)
}
}
})
this.setPlugins(nextProps)
}
getSetterValue(key: string, props: MapProps) {
if (key in this.converterMap) {
return this.converterMap[key](props[key])
}
return props[key]
}
detectPropChanged(key: string, prevProps: MapProps, nextProps: MapProps) {
return prevProps[key] !== nextProps[key]
}
setPlugins(props: MapProps) {
const pluginList = ['Scale', 'ToolBar', 'MapType', 'OverView', 'ControlBar']
if ('plugins' in props) {
const plugins = props.plugins
if (plugins && plugins.length) {
plugins.forEach((p) => {
let name, config, visible
if (typeof p === 'string') {
name = p
config = null
visible = true
} else {
name = p.name
config = p.options || {}
visible = (('visible' in config) && (typeof config.visible === 'boolean')) ? config.visible : true
delete config.visible
}
const idx = pluginList.indexOf(name)
if (idx === -1) {
log.warning(`没有 ‘${name}’ 这个插件,请检查是否拼写错误`)
} else {
if (visible) {
pluginList.splice(idx, 1)
this.installPlugin(name, config)
}
}
})
}
}
this.removeOrDisablePlugins(pluginList)
}
removeOrDisablePlugins(plugins: any[]) {
if (plugins && plugins.length) {
plugins.forEach((p) => {
if (p in this.pluginMap) {
// ControlBar has no 'hide' method
if (p === 'ControlBar') {
this.map.removeControl(this.pluginMap[p])
delete this.pluginMap[p]
} else {
this.pluginMap[p].hide()
}
}
})
}
}
installPlugin(name: string, opts: ?Object) {
opts = opts || {}
switch (name) {
case 'Scale':
case 'ToolBar':
case 'OverView':
case 'MapType':
this.setMapPlugin(name, opts)
break
case 'ControlBar':
this.setControlBar(opts)
break
default:
// do nothing
}
}
setMapPlugin(name: string, opts: Object) {
if (this.pluginMap[name]) {
this.pluginMap[name].show()
} else {
const { onCreated, ...restOpts } = opts
const initOpts = {...defaultOpts[name], ...restOpts}
this.map.plugin([`AMap.${name}`], () => {
this.pluginMap[name] = new window.AMap[name](initOpts)
this.map.addControl(this.pluginMap[name])
if (isFun(onCreated)) {
onCreated(this.pluginMap[name])
}
})
}
}
setControlBar(opts: Object) {
if (this.pluginMap.ControlBar) {
// do nothing
} else {
const { onCreated, ...restOpts } = opts
const initOpts = {...defaultOpts.ControlBar, ...restOpts}
this.map.plugin(['AMap.ControlBar'], () => {
this.pluginMap.ControlBar = new window.AMap.ControlBar(initOpts)
this.map.addControl(this.pluginMap.ControlBar)
if (isFun(onCreated)) {
onCreated(this.pluginMap.ControlBar)
}
})
}
}
render() {
return (<div style={wrapperStyle}>
<div ref={(div)=>{ this.mapWrapper = div }} style={containerStyle}>
{
this.state.mapLoaded ? null : this.props.loading || null
}
</div>
<div>{ this.state.mapLoaded ? this.renderChildren() : null }</div>
</div>)
}
}
export default withPropsReactive(BaseMap)