AMapViewManager.m
7.4 KB
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
#import <React/RCTUIManager.h>
#import "AMapView.h"
#import "AMapMarker.h"
#import "AMapOverlay.h"
#pragma ide diagnostic ignored "OCUnusedClassInspection"
#pragma ide diagnostic ignored "-Woverriding-method-mismatch"
@interface AMapViewManager : RCTViewManager <MAMapViewDelegate>
@end
@implementation AMapViewManager
RCT_EXPORT_MODULE()
- (UIView *)view {
AMapView *mapView = [AMapView new];
mapView.centerCoordinate = CLLocationCoordinate2DMake(39.9242, 116.3979);
mapView.zoomLevel = 10;
mapView.delegate = self;
return mapView;
}
RCT_EXPORT_VIEW_PROPERTY(locationEnabled, BOOL)
RCT_REMAP_VIEW_PROPERTY(showsCompass, showCompass, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsScale, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsIndoorMap, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsLabels, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsTraffic, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsBuildings, BOOL)
RCT_EXPORT_VIEW_PROPERTY(zoomLevel, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(maxZoomLevel, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(minZoomLevel, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(zoomEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(rotateEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(tiltEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(mapType, MAMapType)
RCT_EXPORT_VIEW_PROPERTY(coordinate, CLLocationCoordinate2D)
RCT_EXPORT_VIEW_PROPERTY(limitRegion, MACoordinateRegion)
RCT_EXPORT_VIEW_PROPERTY(region, MACoordinateRegion)
RCT_EXPORT_VIEW_PROPERTY(tilt, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(rotation, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(distanceFilter, CLLocationDistance)
RCT_EXPORT_VIEW_PROPERTY(locationStyle, LocationStyle)
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLongPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLocation, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onStatusChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onStatusChangeComplete, RCTBubblingEventBlock)
RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *)params duration:(NSInteger)duration) {
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
AMapView *mapView = (AMapView *) viewRegistry[reactTag];
MAMapStatus *mapStatus = mapView.getMapStatus;
if (params[@"zoomLevel"]) {
mapStatus.zoomLevel = [params[@"zoomLevel"] floatValue];
}
if (params[@"coordinate"]) {
NSDictionary *coordinate = params[@"coordinate"];
mapStatus.centerCoordinate = CLLocationCoordinate2DMake(
[coordinate[@"latitude"] doubleValue],
[coordinate[@"longitude"] doubleValue]);
}
if (params[@"tilt"]) {
mapStatus.cameraDegree = [params[@"tilt"] floatValue];
}
if (params[@"rotation"]) {
mapStatus.rotationDegree = [params[@"rotation"] floatValue];
}
[mapView setMapStatus:mapStatus animated:YES duration:duration / 1000.0];
}];
}
- (void)mapView:(AMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (mapView.onPress) {
mapView.onPress(@{
@"latitude": @(coordinate.latitude),
@"longitude": @(coordinate.longitude),
});
}
}
- (void)mapView:(AMapView *)mapView didLongPressedAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (mapView.onLongPress) {
mapView.onLongPress(@{
@"latitude": @(coordinate.latitude),
@"longitude": @(coordinate.longitude),
});
}
}
- (void)mapView:(AMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
if (mapView.onLocation) {
mapView.onLocation(@{
@"latitude": @(userLocation.coordinate.latitude),
@"longitude": @(userLocation.coordinate.longitude),
@"accuracy": @((userLocation.location.horizontalAccuracy + userLocation.location.verticalAccuracy) / 2),
@"altitude": @(userLocation.location.altitude),
@"speed": @(userLocation.location.speed),
@"timestamp": @(userLocation.location.timestamp.timeIntervalSince1970),
});
}
}
- (MAAnnotationView *)mapView:(AMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
AMapMarker *marker = [mapView getMarker:annotation];
return marker.annotationView;
}
return nil;
}
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
if ([overlay isKindOfClass:[AMapOverlay class]]) {
return ((AMapOverlay *) overlay).renderer;
}
return nil;
}
- (void)mapView:(AMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
AMapMarker *marker = [mapView getMarker:view.annotation];
if (marker.onPress) {
marker.onPress(nil);
}
}
- (void)mapView:(AMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view {
AMapMarker *marker = [mapView getMarker:view.annotation];
if (marker.onInfoWindowPress) {
marker.onInfoWindowPress(nil);
}
}
- (void)mapView:(AMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState
fromOldState:(MAAnnotationViewDragState)oldState {
AMapMarker *marker = [mapView getMarker:view.annotation];
if (newState == MAAnnotationViewDragStateStarting && marker.onDragStart) {
marker.onDragStart(nil);
}
if (newState == MAAnnotationViewDragStateDragging) {
if (marker.onDrag) {
marker.onDrag(nil);
}
}
if (newState == MAAnnotationViewDragStateEnding && marker.onDragEnd) {
marker.onDragEnd(@{
@"latitude": @(marker.annotation.coordinate.latitude),
@"longitude": @(marker.annotation.coordinate.longitude),
});
}
}
- (void)mapViewRegionChanged:(AMapView *)mapView {
if (mapView.onStatusChange) {
MAMapStatus *status = mapView.getMapStatus;
mapView.onStatusChange(@{
@"zoomLevel": @(status.zoomLevel),
@"tilt": @(status.cameraDegree),
@"rotation": @(status.rotationDegree),
@"latitude": @(status.centerCoordinate.latitude),
@"longitude": @(status.centerCoordinate.longitude),
});
}
}
- (void)mapView:(AMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
if (mapView.onStatusChangeComplete) {
MAMapStatus *status = mapView.getMapStatus;
mapView.onStatusChangeComplete(@{
@"zoomLevel": @(status.zoomLevel),
@"tilt": @(status.cameraDegree),
@"rotation": @(status.rotationDegree),
@"latitude": @(status.centerCoordinate.latitude),
@"longitude": @(status.centerCoordinate.longitude),
@"latitudeDelta": @(mapView.region.span.latitudeDelta),
@"longitudeDelta": @(mapView.region.span.longitudeDelta),
});
}
}
- (void)mapInitComplete:(AMapView *)mapView {
mapView.loaded = YES;
// struct 里的值会被初始化为 0,这里以此作为条件,判断 initialRegion 是否被设置过
// 但实际上经度为 0 是一个合法的坐标,只是考虑到高德地图只在中国使用,就这样吧
if (mapView.initialRegion.center.latitude != 0) {
mapView.region = mapView.initialRegion;
}
}
@end