-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
571 lines (507 loc) · 16.7 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
'use strict';
import React from 'react';
import ReactNative, {
EdgeInsetsPropType,
Image,
NativeMethodsMixin,
Platform,
requireNativeComponent,
StyleSheet,
View,
UIManager,
processColor,
ColorPropType,
} from 'react-native';
import deprecatedPropType from 'react-native/Libraries/Utilities/deprecatedPropType';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
const RCTBaiduMapConstants = UIManager.RCTBaiduMap.Constants;
type Event = Object;
export type MAAnnotationDragState = $Enum<{
idle: string;
starting: string;
dragging: string;
canceling: string;
ending: string;
}>;
// class Fuck extends React.Component {
// render() {
// return <View></View>;
// }
// }
const BaiduMapView= React.createClass({
mixins: [NativeMethodsMixin],
propTypes: {
...View.propTypes,
/**
* Used to style and layout the `MapView`. See `StyleSheet.js` and
* `ViewStylePropTypes.js` for more info.
*/
style: View.propTypes.style,
/**
* If `true` the app will ask for the user's location and display it on
* the map. Default value is `false`.
*
* **NOTE**: on iOS, you need to add the `NSLocationWhenInUseUsageDescription`
* key in Info.plist to enable geolocation, otherwise it will fail silently.
*/
showsUserLocation: React.PropTypes.bool,
userLocationViewParams: React.PropTypes.shape({
/**
* 定位图标XY轴偏移量(屏幕坐标)
*/
offsetX: React.PropTypes.number,
offsetY: React.PropTypes.number,
/**
* 精度圈是否显示
*/
showAccuracyCircle: React.PropTypes.bool,
/**
* 精度圈 填充颜色
*/
accuracyCircleFillColor: ColorPropType,
/**
* 精度圈 边框颜色
*/
accuracyCircleStrokeColor: ColorPropType,
/**
* 跟随态旋转角度是否生效
*/
rotateAngleValid: React.PropTypes.bool,
/**
* 定位图标名称,需要将该图片放到 mapapi.bundle/images 目录下
*/
image: Image.propTypes.source
}),
autoZoomToSpan: React.PropTypes.bool,
/**
* If `true` the map will follow the user's location whenever it changes.
* Note that this has no effect unless `showsUserLocation` is enabled.
* Default value is `true`.
* @platform ios
*/
followUserLocation: React.PropTypes.bool,
/**
* If `false` points of interest won't be displayed on the map.
* Default value is `true`.
* @platform ios
*/
showsPointsOfInterest: React.PropTypes.bool,
/**
* If `false` compass won't be displayed on the map.
* Default value is `true`.
* @platform ios
*/
showsCompass: React.PropTypes.bool,
/**
* If `false` the user won't be able to pinch/zoom the map.
* Default value is `true`.
*/
zoomEnabled: React.PropTypes.bool,
/**
* When this property is set to `true` and a valid camera is associated with
* the map, the camera’s heading angle is used to rotate the plane of the
* map around its center point. When this property is set to `false`, the
* camera’s heading angle is ignored and the map is always oriented so
* that true north is situated at the top of the map view
*/
rotateEnabled: React.PropTypes.bool,
/**
* When this property is set to `true` and a valid camera is associated
* with the map, the camera’s pitch angle is used to tilt the plane
* of the map. When this property is set to `false`, the camera’s pitch
* angle is ignored and the map is always displayed as if the user
* is looking straight down onto it.
*/
pitchEnabled: React.PropTypes.bool,
/**
* If `false` the user won't be able to change the map region being displayed.
* Default value is `true`.
*/
scrollEnabled: React.PropTypes.bool,
/**
* The map type to be displayed.
*
* - standard: standard road map (default)
* - satellite: satellite view
* - hybrid: satellite view with roads and points of interest overlaid
*
* @platform ios
*/
mapType: React.PropTypes.oneOf([
'standard',
'satellite',
'hybrid',
]),
/**
* The region to be displayed by the map.
*
* The region is defined by the center coordinates and the span of
* coordinates to display.
*/
region: React.PropTypes.shape({
/**
* Coordinates for the center of the map.
*/
latitude: React.PropTypes.number.isRequired,
longitude: React.PropTypes.number.isRequired,
/**
* Distance between the minimum and the maximum latitude/longitude
* to be displayed.
*/
latitudeDelta: React.PropTypes.number,
longitudeDelta: React.PropTypes.number,
}),
/**
* Map annotations with title/subtitle.
* @platform ios
*/
annotations: React.PropTypes.arrayOf(React.PropTypes.shape({
/**
* The location of the annotation.
*/
latitude: React.PropTypes.number.isRequired,
longitude: React.PropTypes.number.isRequired,
/**
* Whether the pin drop should be animated or not
*/
animateDrop: React.PropTypes.bool,
/**
* Whether the pin should be draggable or not
*/
draggable: React.PropTypes.bool,
/**
* Event that fires when the annotation drag state changes.
*/
onDragStateChange: React.PropTypes.func,
/**
* Event that fires when the annotation gets was tapped by the user
* and the callout view was displayed.
*/
onFocus: React.PropTypes.func,
/**
* Event that fires when another annotation or the mapview itself
* was tapped and a previously shown annotation will be closed.
*/
onBlur: React.PropTypes.func,
/**
* Annotation title/subtile.
*/
title: React.PropTypes.string,
subtitle: React.PropTypes.string,
/**
* Callout views.
*/
leftCalloutView: React.PropTypes.element,
rightCalloutView: React.PropTypes.element,
/**
* The pin color. This can be any valid color string, or you can use one
* of the predefined PinColors constants. Applies to both standard pins
* and custom pin images.
*
* Note that on iOS 8 and earlier, only the standard PinColor constants
* are supported for regular pins. For custom pin images, any tintColor
* value is supported on all iOS versions.
*/
tintColor: React.PropTypes.number,
/**
* Custom pin image. This must be a static image resource inside the app.
*/
image: Image.propTypes.source,
/**
* Custom pin view. If set, this replaces the pin or custom pin image.
*/
view: React.PropTypes.element,
/**
* annotation id
*/
id: React.PropTypes.string,
/**
* Deprecated. Use the left/right/detailsCalloutView props instead.
*/
hasLeftCallout: deprecatedPropType(
React.PropTypes.bool,
'Use `leftCalloutView` instead.'
),
hasRightCallout: deprecatedPropType(
React.PropTypes.bool,
'Use `rightCalloutView` instead.'
),
onLeftCalloutPress: deprecatedPropType(
React.PropTypes.func,
'Use `leftCalloutView` instead.'
),
onRightCalloutPress: deprecatedPropType(
React.PropTypes.func,
'Use `rightCalloutView` instead.'
),
})),
/**
* Map overlays
* @platform ios
*/
overlays: React.PropTypes.arrayOf(React.PropTypes.shape({
/**
* Polyline coordinates
*/
coordinates: React.PropTypes.arrayOf(React.PropTypes.shape({
latitude: React.PropTypes.number.isRequired,
longitude: React.PropTypes.number.isRequired
})),
/**
* Line attributes
*/
lineWidth: React.PropTypes.number,
strokeColor: ColorPropType,
fillColor: ColorPropType,
/**
* Overlay id
*/
id: React.PropTypes.string
})),
/**
* Maximum size of area that can be displayed.
* @platform ios
*/
maxDelta: React.PropTypes.number,
/**
* Minimum size of area that can be displayed.
* @platform ios
*/
minDelta: React.PropTypes.number,
/**
* Insets for the map's legal label, originally at bottom left of the map.
* See `EdgeInsetsPropType.js` for more information.
* @platform ios
*/
legalLabelInsets: EdgeInsetsPropType,
/**
* Callback that is called continuously when the user is dragging the map.
*/
onRegionChange: React.PropTypes.func,
/**
* Callback that is called once, when the user is done moving the map.
*/
onRegionChangeComplete: React.PropTypes.func,
/**
* Deprecated. Use annotation onFocus and onBlur instead.
*/
onAnnotationPress: React.PropTypes.func,
/**
* @platform android
*/
active: React.PropTypes.bool,
},
render: function() {
let children = [], {annotations, overlays, followUserLocation, userLocationViewParams, showsZoomControl} = this.props;
annotations = annotations && annotations.map((annotation: Object) => {
let {
id,
image,
tintColor,
view,
leftCalloutView,
rightCalloutView,
} = annotation;
if (!view && image && tintColor) {
view = <Image
style={{
tintColor: processColor(tintColor),
}}
source={image}
/>;
image = undefined;
}
if (view) {
if (image) {
console.warn('`image` and `view` both set on annotation. Image will be ignored.');
}
var viewIndex = children.length;
children.push(React.cloneElement(view, {
style: [styles.annotationView, view.props.style || {}]
}));
}
if (leftCalloutView) {
var leftCalloutViewIndex = children.length;
children.push(React.cloneElement(leftCalloutView, {
style: [styles.calloutView, leftCalloutView.props.style || {}]
}));
}
if (rightCalloutView) {
var rightCalloutViewIndex = children.length;
children.push(React.cloneElement(rightCalloutView, {
style: [styles.calloutView, rightCalloutView.props.style || {}]
}));
}
let result = {
...annotation,
tintColor: tintColor && processColor(tintColor),
image,
viewIndex,
leftCalloutViewIndex,
rightCalloutViewIndex,
view: undefined,
leftCalloutView: undefined,
rightCalloutView: undefined,
};
result.id = id || encodeURIComponent(JSON.stringify(result));
result.image = image && resolveAssetSource(image);
return result;
});
overlays = overlays && overlays.map((overlay: Object) => {
let {id, fillColor, strokeColor} = overlay;
let result = {
...overlay,
strokeColor: strokeColor && processColor(strokeColor),
fillColor: fillColor && processColor(fillColor),
};
result.id = id || encodeURIComponent(JSON.stringify(result));
return result;
});
const findByAnnotationId = (annotationId: string) => {
if (!annotations) {
return null;
}
for (let i = 0, l = annotations.length; i < l; i++) {
if (annotations[i].id === annotationId) {
return annotations[i];
}
}
return null;
};
// TODO: these should be separate events, to reduce bridge traffic
let onPress, onAnnotationDragStateChange, onAnnotationFocus, onAnnotationBlur;
if (annotations) {
onPress = (event: Event) => {
if (event.nativeEvent.action === 'annotation-click') {
// TODO: Remove deprecated onAnnotationPress API call later.
this.props.onAnnotationPress &&
this.props.onAnnotationPress(event.nativeEvent.annotation);
} else if (event.nativeEvent.action === 'callout-click') {
const annotation = findByAnnotationId(event.nativeEvent.annotationId);
if (annotation) {
// Pass the right function
if (event.nativeEvent.side === 'left' && annotation.onLeftCalloutPress) {
annotation.onLeftCalloutPress(event.nativeEvent);
} else if (event.nativeEvent.side === 'right' && annotation.onRightCalloutPress) {
annotation.onRightCalloutPress(event.nativeEvent);
}
}
}
};
onAnnotationDragStateChange = (event: Event) => {
const annotation = findByAnnotationId(event.nativeEvent.annotationId);
if (annotation) {
// Update location
annotation.latitude = event.nativeEvent.latitude;
annotation.longitude = event.nativeEvent.longitude;
// Call callback
annotation.onDragStateChange &&
annotation.onDragStateChange(event.nativeEvent);
}
};
onAnnotationFocus = (event: Event) => {
const annotation = findByAnnotationId(event.nativeEvent.annotationId);
if (annotation && annotation.onFocus) {
annotation.onFocus(event.nativeEvent);
}
};
onAnnotationBlur = (event: Event) => {
const annotation = findByAnnotationId(event.nativeEvent.annotationId);
if (annotation && annotation.onBlur) {
annotation.onBlur(event.nativeEvent);
}
};
}
// TODO: these should be separate events, to reduce bridge traffic
if (this.props.onRegionChange || this.props.onRegionChangeComplete) {
var onChange = (event: Event) => {
if (event.nativeEvent.continuous) {
this.props.onRegionChange &&
this.props.onRegionChange(event.nativeEvent.region);
} else {
this.props.onRegionChangeComplete &&
this.props.onRegionChangeComplete(event.nativeEvent.region);
}
};
}
// followUserLocation defaults to true if showUserLocation is set
if (followUserLocation === undefined) {
followUserLocation = this.props.showUserLocation;
}
if (!!userLocationViewParams) {
let {accuracyCircleFillColor, accuracyCircleStrokeColor, image} = userLocationViewParams;
accuracyCircleFillColor = accuracyCircleFillColor && processColor(accuracyCircleFillColor);
accuracyCircleStrokeColor = accuracyCircleStrokeColor && processColor(accuracyCircleStrokeColor);
userLocationViewParams.accuracyCircleFillColor = accuracyCircleFillColor;
userLocationViewParams.accuracyCircleStrokeColor = accuracyCircleStrokeColor;
image = image && resolveAssetSource(image);
userLocationViewParams.image = image;
}
if (showsZoomControl === undefined) {
showsZoomControl = false;
}
return (
<RCTBaiduMap
{...this.props}
ref="baiduMap"
annotations={annotations}
children={children}
followUserLocation={followUserLocation}
showsZoomControl={showsZoomControl}
overlays={overlays}
onPress={onPress}
onChange={onChange}
onAnnotationDragStateChange={onAnnotationDragStateChange}
onAnnotationFocus={onAnnotationFocus}
onAnnotationBlur={onAnnotationBlur}
userLocationViewParams={userLocationViewParams}
/>
);
},
zoomToLocs(locs) {
if (!Array.isArray(locs) && Object.prototype.toString.call(locs) === '[object Object]') {
locs = [locs];
}
if (!Array.isArray(locs)) {
return;
}
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this.refs["baiduMap"]),
UIManager.RCTBaiduMap.Commands.zoomToLocs,
[locs]
);
},
});
const styles = StyleSheet.create({
annotationView: {
position: 'absolute',
backgroundColor: 'transparent',
},
calloutView: {
position: 'absolute',
backgroundColor: 'white',
},
});
/**
* Standard iOS MapView pin color constants, to be used with the
* `annotation.tintColor` property. On iOS 8 and earlier these are the
* only supported values when using regular pins. On iOS 9 and later
* you are not obliged to use these, but they are useful for matching
* the standard iOS look and feel.
*/
let PinColors = RCTBaiduMapConstants && RCTBaiduMapConstants.PinColors;
BaiduMapView.PinColors = PinColors && {
RED: PinColors.RED,
GREEN: PinColors.GREEN,
PURPLE: PinColors.PURPLE,
};
const RCTBaiduMap = requireNativeComponent('RCTBaiduMap', BaiduMapView, {
nativeOnly: {
onAnnotationDragStateChange: true,
onAnnotationFocus: true,
onAnnotationBlur: true,
onChange: true,
onPress: true
}
});
module.exports = BaiduMapView;