Skip to content

Commit

Permalink
Add onMapReady callback (react-native-maps#1369)
Browse files Browse the repository at this point in the history
* Add onMapReady callback

Fix react-native-maps#246

* Call onMapReady when state has changed

* Add onMapReady callback on iOS
  • Loading branch information
Kerumen authored and patricio committed Sep 27, 2017
1 parent 681e7d2 commit d761efd
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/mapview.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ To access event data, you will need to use `e.nativeEvent`. For example, `onPres

| Event Name | Returns | Notes
|---|---|---|
| `onMapReady` | | Callback that is called once the map is fully loaded.
| `onRegionChange` | `Region` | Callback that is called continuously when the region changes, such as when a user is dragging the map.
| `onRegionChangeComplete` | `Region` | Callback that is called once when the region changes, such as when the user is done moving the map.
| `onPress` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user taps on the map.
Expand Down
11 changes: 9 additions & 2 deletions lib/components/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ const propTypes = {
*/
legalLabelInsets: EdgeInsetsPropType,

/**
* Callback that is called once the map is fully loaded.
*/
onMapReady: PropTypes.func,

/**
* Callback that is called continuously when the user is dragging the map.
*/
Expand Down Expand Up @@ -441,14 +446,16 @@ class MapView extends React.Component {
}

_onMapReady() {
const { region, initialRegion } = this.props;
const { region, initialRegion, onMapReady } = this.props;
if (region) {
this.map.setNativeProps({ region });
} else if (initialRegion) {
this.map.setNativeProps({ region: initialRegion });
}
this._updateStyle();
this.setState({ isReady: true });
this.setState({ isReady: true }, () => {
if (onMapReady) onMapReady();
});
}

_onLayout(e) {
Expand Down
2 changes: 2 additions & 0 deletions lib/ios/AirGoogleMaps/AIRGoogleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@property (nonatomic, assign) MKCoordinateRegion initialRegion;
@property (nonatomic, assign) MKCoordinateRegion region;
@property (nonatomic, assign) NSString *customMapStyleString;
@property (nonatomic, copy) RCTBubblingEventBlock onMapReady;
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onLongPress;
@property (nonatomic, copy) RCTBubblingEventBlock onMarkerPress;
Expand All @@ -40,6 +41,7 @@
@property (nonatomic, assign) BOOL showsUserLocation;
@property (nonatomic, assign) BOOL showsMyLocationButton;

- (void)didFinishTileRendering;
- (BOOL)didTapMarker:(GMSMarker *)marker;
- (void)didTapPolyline:(GMSPolyline *)polyline;
- (void)didTapPolygon:(GMSPolygon *)polygon;
Expand Down
4 changes: 4 additions & 0 deletions lib/ios/AirGoogleMaps/AIRGoogleMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ - (void)setRegion:(MKCoordinateRegion)region {
self.camera = [AIRGoogleMap makeGMSCameraPositionFromMap:self andMKCoordinateRegion:region];
}

- (void)didFinishTileRendering {
if (self.onMapReady) self.onMapReady(@{});
}

- (BOOL)didTapMarker:(GMSMarker *)marker {
AIRGMSMarker *airMarker = (AIRGMSMarker *)marker;

Expand Down
6 changes: 6 additions & 0 deletions lib/ios/AirGoogleMaps/AIRGoogleMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(showsUserLocation, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsMyLocationButton, BOOL)
RCT_EXPORT_VIEW_PROPERTY(customMapStyleString, NSString)
RCT_EXPORT_VIEW_PROPERTY(onMapReady, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLongPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
Expand Down Expand Up @@ -222,6 +223,11 @@ - (UIView *)view
}


- (void)mapViewDidFinishTileRendering:(GMSMapView *)mapView {
AIRGoogleMap *googleMapView = (AIRGoogleMap *)mapView;
[googleMapView didFinishTileRendering];
}

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
AIRGoogleMap *googleMapView = (AIRGoogleMap *)mapView;
return [googleMapView didTapMarker:marker];
Expand Down
1 change: 1 addition & 0 deletions lib/ios/AirMaps/AIRMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern const CGFloat AIRMapZoomBoundBuffer;

@property (nonatomic, assign) BOOL ignoreRegionChanges;

@property (nonatomic, copy) RCTBubblingEventBlock onMapReady;
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onPanDrag;
Expand Down
3 changes: 3 additions & 0 deletions lib/ios/AirMaps/AIRMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(minDelta, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(legalLabelInsets, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(mapType, MKMapType)
RCT_EXPORT_VIEW_PROPERTY(onMapReady, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPanDrag, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
Expand Down Expand Up @@ -662,6 +663,8 @@ - (void)mapViewDidFinishRenderingMap:(AIRMap *)mapView fullyRendered:(BOOL)fully
{
[mapView finishLoading];
[mapView cacheViewIfNeeded];

mapView.onMapReady(@{});
}

#pragma mark Private
Expand Down

0 comments on commit d761efd

Please sign in to comment.