/
MapView.js
executable file
·365 lines (310 loc) · 7.66 KB
/
MapView.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// @flow
import React from 'react'
import PropTypes from 'prop-types'
import { processColor, requireNativeComponent, ViewPropTypes, Platform } from 'react-native'
import { LatLng, Region } from '../PropTypes'
import Component from '../Component'
export type MapStatus = {
zoomLevel?: number,
coordinate?: LatLng,
titl?: number,
rotation?: number,
}
export const LocationStyle = PropTypes.shape({
image: PropTypes.string,
fillColor: PropTypes.string,
strokeColor: PropTypes.string,
strokeWidth: PropTypes.number,
})
const isIos = Platform.OS === 'ios'
export default class MapView extends Component<any> {
static propTypes = {
...ViewPropTypes,
/**
* 地图类型
*
* - standard: 标准地图
* - satellite: 卫星地图
* - navigation: 导航地图
* - night: 夜间地图
* - bus: 公交地图
*/
mapType: PropTypes.oneOf(['standard', 'satellite', 'navigation', 'night', 'bus']),
/**
* 是否启用跟踪模式
* 0: 不跟踪
* 1: 跟踪用户定位
* 2: 跟踪用户定位和方向
*/
userTrackingMode: PropTypes.oneOf(0, 1, 2),
/**
* 是否跟踪用户位置
*/
showsUserLocation: PropTypes.bool,
/**
* 缩放尺寸,范围是:[3-19]
*/
zoomLevel: PropTypes.number,
/**
* 设置定位图标的样式
*/
locationStyle: LocationStyle,
/**
* 设置定位模式 默认 LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER
*
* @platform android
*/
locationType: PropTypes.oneOf(['show', 'locate', 'follow', 'map_rotate', 'location_rotate',
'location_rotate_no_center', 'follow_no_center', 'map_rotate_no_center']),
/**
* 是否启用定位
*/
locationEnabled: PropTypes.bool,
/**
* 定位间隔(ms),默认 2000
*
* @platform android
*/
locationInterval: PropTypes.number,
/**
* 定位的最小更新距离
*
* @platform ios
*/
distanceFilter: PropTypes.number,
/**
* 是否显示室内地图
*/
showsIndoorMap: PropTypes.bool,
/**
* 是否显示室内地图楼层切换控件
*
* TODO: 似乎并不能正常显示
*/
showsIndoorSwitch: PropTypes.bool,
/**
* 是否显示3D建筑
*/
showsBuildings: PropTypes.bool,
/**
* 是否显示文本标签
*/
showsLabels: PropTypes.bool,
/**
* 是否显示指南针
*/
showsCompass: PropTypes.bool,
/**
* 是否显示放大缩小按钮
*
* @platform android
*/
showsZoomControls: PropTypes.bool,
/**
* 是否显示比例尺
*/
showsScale: PropTypes.bool,
/**
* 是否显示定位按钮
*
* @platform android
*/
showsLocationButton: PropTypes.bool,
/**
* 是否显示路况
*/
showsTraffic: PropTypes.bool,
/**
* 最大缩放级别
*/
maxZoomLevel: PropTypes.number,
/**
* 最小缩放级别
*/
minZoomLevel: PropTypes.number,
/**
* 当前缩放级别,取值范围 [3, 20]
*/
zoomLevel: PropTypes.number,
/**
* 中心坐标
*/
coordinate: LatLng,
/**
* 显示区域
*/
region: Region,
/**
* 限制地图只能显示某个矩形区域
*/
limitRegion: Region,
/**
* 倾斜角度,取值范围 [0, 60]
*/
tilt: PropTypes.number,
/**
* 旋转角度
*/
rotation: PropTypes.number,
/**
* 是否启用缩放手势,用于放大缩小
*/
zoomEnabled: PropTypes.bool,
/**
* 是否启用滑动手势,用于平移
*/
scrollEnabled: PropTypes.bool,
/**
* 是否启用旋转手势,用于调整方向
*/
rotateEnabled: PropTypes.bool,
/**
* 是否启用倾斜手势,用于改变视角
*/
tiltEnabled: PropTypes.bool,
/**
* 是否可以触摸
*/
touchEnable: PropTypes.bool,
/**
* 是否启用定位
*/
showsUserLocation: PropTypes.bool,
/**
* 中心坐标
*/
centerLocation: LatLng,
/**
* 点击事件
*
* @param {{ nativeEvent: LatLng }}
*/
onMapPress: PropTypes.func,
/**
* 长按事件
*
* @param {{ nativeEvent: LatLng }}
*/
onLongPress: PropTypes.func,
/**
* 定位事件
*
* @param {{
* nativeEvent: {
* timestamp: number,
* speed: number,
* accuracy: number,
* altitude: number,
* longitude: number,
* latitude: number,
* }
* }}
*/
onLocation: PropTypes.func,
/**
* 动画完成事件
*/
onAnimateFinish: PropTypes.func,
/**
* 动画取消事件
*/
onAnimateCancel: PropTypes.func,
/**
* 地图状态变化事件
*
* @param {{
* nativeEvent: {
* longitude: number,
* latitude: number,
* rotation: number,
* zoomLevel: number,
* tilt: number,
* }
* }}
*/
onStatusChange: PropTypes.func,
/**
* 地图状态变化完成事件
*
* @param {{
* nativeEvent: {
* longitude: number,
* latitude: number,
* longitudeDelta: number,
* latitudeDelta: number,
* rotation: number,
* zoomLevel: number,
* tilt: number,
* }
* }}
*/
onStatusChangeComplete: PropTypes.func,
}
name = 'AMapView'
isMapFirstLoaded = false
_isMoveByUser = true
lastCompleteTime = 0
_onMoveComplete = (event) => {
const oldLastCompleteTime = this.lastCompleteTime
this.lastCompleteTime = new Date().getTime()
if ((this.lastCompleteTime - oldLastCompleteTime) < 700) {
this._isMoveByUser = true
return
}
if (isIos && !this.isMapFirstLoaded) {
this.isMapFirstLoaded = true
this._isMoveByUser = true
return
}
this.props.onStatusChangeComplete && this.props.onStatusChangeComplete(event, this._isMoveByUser)
this._isMoveByUser = true
}
componentWillReceiveProps({ coordinate }) {
const oldCoordinate = this.props.coordinate
if (coordinate && oldCoordinate) {
const { latitude, longitude } = coordinate
const oldLatitude = oldCoordinate.latitude
const oldLongitude = oldCoordinate.longitude
if ((latitude != oldLatitude) && (longitude != oldLongitude)) {
this._isMoveByUser = false
}
} else if (coordinate) {
this._isMoveByUser = false
}
}
_onMovingChange = (event) => {
if (isIos && !this.isMapFirstLoaded) {
return
}
this.props.onStatusChange && this.props.onStatusChange(event, this._isMoveByUser)
}
/**
* 动画过渡到某个状态(坐标、缩放级别、倾斜度、旋转角度)
*/
animateTo(target: MapStatus, duration?: number = 500) {
this.sendCommand('animateTo', [target, duration])
}
onMapPress = (event) => {
if (this.props.onMapPress) {
this.props.onMapPress(event)
}
}
render() {
const props = { ...this.props }
const { centerLocation } = this.props
if (centerLocation) {
delete props.centerLocation
props.coordinate = centerLocation
}
if (props.locationStyle) {
if (props.locationStyle.strokeColor) {
props.locationStyle.strokeColor = processColor(props.locationStyle.strokeColor)
}
if (props.locationStyle.fillColor) {
props.locationStyle.fillColor = processColor(props.locationStyle.fillColor)
}
}
return <AMapView {...props} onMapPress={this.onMapPress} onStatusChange={this._onMovingChange} onStatusChangeComplete={this._onMoveComplete} />
}
}
const AMapView = requireNativeComponent('AMapView', MapView)