Skip to content

Commit d862b2f

Browse files
KerumenMasu Lin
authored and
Masu Lin
committed
Add onMapReady callback (react-native-maps#1369)
* Add onMapReady callback Fix react-native-maps#246 * Call onMapReady when state has changed * Add onMapReady callback on iOS
1 parent 999c969 commit d862b2f

File tree

7 files changed

+26
-2
lines changed

7 files changed

+26
-2
lines changed

docs/mapview.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ To access event data, you will need to use `e.nativeEvent`. For example, `onPres
4040

4141
| Event Name | Returns | Notes
4242
|---|---|---|
43+
| `onMapReady` | | Callback that is called once the map is fully loaded.
4344
| `onRegionChange` | `Region` | Callback that is called continuously when the region changes, such as when a user is dragging the map.
4445
| `onRegionChangeComplete` | `Region` | Callback that is called once when the region changes, such as when the user is done moving the map.
4546
| `onPress` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user taps on the map.

lib/components/MapView.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ const propTypes = {
317317
*/
318318
legalLabelInsets: EdgeInsetsPropType,
319319

320+
/**
321+
* Callback that is called once the map is fully loaded.
322+
*/
323+
onMapReady: PropTypes.func,
324+
320325
/**
321326
* Callback that is called continuously when the user is dragging the map.
322327
*/
@@ -440,14 +445,16 @@ class MapView extends React.Component {
440445
}
441446

442447
_onMapReady() {
443-
const { region, initialRegion } = this.props;
448+
const { region, initialRegion, onMapReady } = this.props;
444449
if (region) {
445450
this.map.setNativeProps({ region });
446451
} else if (initialRegion) {
447452
this.map.setNativeProps({ region: initialRegion });
448453
}
449454
this._updateStyle();
450-
this.setState({ isReady: true });
455+
this.setState({ isReady: true }, () => {
456+
if (onMapReady) onMapReady();
457+
});
451458
}
452459

453460
_onLayout(e) {

lib/ios/AirGoogleMaps/AIRGoogleMap.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
@property (nonatomic, assign) MKCoordinateRegion initialRegion;
1919
@property (nonatomic, assign) MKCoordinateRegion region;
2020
@property (nonatomic, assign) NSString *customMapStyleString;
21+
@property (nonatomic, copy) RCTBubblingEventBlock onMapReady;
2122
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
2223
@property (nonatomic, copy) RCTBubblingEventBlock onLongPress;
2324
@property (nonatomic, copy) RCTBubblingEventBlock onMarkerPress;
@@ -40,6 +41,7 @@
4041
@property (nonatomic, assign) BOOL showsUserLocation;
4142
@property (nonatomic, assign) BOOL showsMyLocationButton;
4243

44+
- (void)didFinishTileRendering;
4345
- (BOOL)didTapMarker:(GMSMarker *)marker;
4446
- (void)didTapPolyline:(GMSPolyline *)polyline;
4547
- (void)didTapPolygon:(GMSPolygon *)polygon;

lib/ios/AirGoogleMaps/AIRGoogleMap.m

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ - (void)setRegion:(MKCoordinateRegion)region {
155155
self.camera = [AIRGoogleMap makeGMSCameraPositionFromMap:self andMKCoordinateRegion:region];
156156
}
157157

158+
- (void)didFinishTileRendering {
159+
if (self.onMapReady) self.onMapReady(@{});
160+
}
161+
158162
- (BOOL)didTapMarker:(GMSMarker *)marker {
159163
AIRGMSMarker *airMarker = (AIRGMSMarker *)marker;
160164

lib/ios/AirGoogleMaps/AIRGoogleMapManager.m

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ - (UIView *)view
5959
RCT_EXPORT_VIEW_PROPERTY(showsUserLocation, BOOL)
6060
RCT_EXPORT_VIEW_PROPERTY(showsMyLocationButton, BOOL)
6161
RCT_EXPORT_VIEW_PROPERTY(customMapStyleString, NSString)
62+
RCT_EXPORT_VIEW_PROPERTY(onMapReady, RCTBubblingEventBlock)
6263
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
6364
RCT_EXPORT_VIEW_PROPERTY(onLongPress, RCTBubblingEventBlock)
6465
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
@@ -222,6 +223,11 @@ - (UIView *)view
222223
}
223224

224225

226+
- (void)mapViewDidFinishTileRendering:(GMSMapView *)mapView {
227+
AIRGoogleMap *googleMapView = (AIRGoogleMap *)mapView;
228+
[googleMapView didFinishTileRendering];
229+
}
230+
225231
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
226232
AIRGoogleMap *googleMapView = (AIRGoogleMap *)mapView;
227233
return [googleMapView didTapMarker:marker];

lib/ios/AirMaps/AIRMap.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern const CGFloat AIRMapZoomBoundBuffer;
4646

4747
@property (nonatomic, assign) BOOL ignoreRegionChanges;
4848

49+
@property (nonatomic, copy) RCTBubblingEventBlock onMapReady;
4950
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
5051
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
5152
@property (nonatomic, copy) RCTBubblingEventBlock onPanDrag;

lib/ios/AirMaps/AIRMapManager.m

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ - (UIView *)view
8585
RCT_EXPORT_VIEW_PROPERTY(minDelta, CGFloat)
8686
RCT_EXPORT_VIEW_PROPERTY(legalLabelInsets, UIEdgeInsets)
8787
RCT_EXPORT_VIEW_PROPERTY(mapType, MKMapType)
88+
RCT_EXPORT_VIEW_PROPERTY(onMapReady, RCTBubblingEventBlock)
8889
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
8990
RCT_EXPORT_VIEW_PROPERTY(onPanDrag, RCTBubblingEventBlock)
9091
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
@@ -662,6 +663,8 @@ - (void)mapViewDidFinishRenderingMap:(AIRMap *)mapView fullyRendered:(BOOL)fully
662663
{
663664
[mapView finishLoading];
664665
[mapView cacheViewIfNeeded];
666+
667+
mapView.onMapReady(@{});
665668
}
666669

667670
#pragma mark Private

0 commit comments

Comments
 (0)