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

(cherry picked from commit 8a86e98)
  • Loading branch information
Kerumen authored and nidongara committed Sep 27, 2017
1 parent d6087ba commit f2b8eba
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,13 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
// Timer Implementation

public void startMonitoringRegion() {
if (isMonitoringRegion) return;
if (this.map == null || isMonitoringRegion) return;
timerHandler.postDelayed(timerRunnable, 100);
isMonitoringRegion = true;
}

public void stopMonitoringRegion() {
if (!isMonitoringRegion) return;
if (this.map == null || !isMonitoringRegion) return;
timerHandler.removeCallbacks(timerRunnable);
isMonitoringRegion = false;
}
Expand Down
11 changes: 9 additions & 2 deletions components/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,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 @@ -394,13 +399,15 @@ 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.setState({ isReady: true });
this.setState({ isReady: true }, () => {
if (onMapReady) onMapReady();
});
}

_onLayout(e) {
Expand Down
5 changes: 3 additions & 2 deletions docs/mapview.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@

| Event Name | Returns | Notes
|---|---|---|
| `onRegionChange` | `Region` | Callback that is called continuously when the user is dragging the map.
| `onRegionChangeComplete` | `Region` | Callback that is called once, when the user is done moving the map.
| `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.
| `onPanDrag` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user presses and drags the map. **NOTE**: for iOS `scrollEnabled` should be set to false to trigger the event
| `onLongPress` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user makes a "long press" somewhere on the map.
Expand Down
1 change: 1 addition & 0 deletions ios/AirGoogleMaps/AIRGoogleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@property (nonatomic, assign) BOOL pitchEnabled;
@property (nonatomic, assign) BOOL showsUserLocation;

- (void)didFinishTileRendering;
- (BOOL)didTapMarker:(GMSMarker *)marker;
- (void)didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;
- (void)didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate;
Expand Down
6 changes: 6 additions & 0 deletions ios/AirGoogleMaps/AIRGoogleMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(pitchEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsUserLocation, BOOL)
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 @@ -177,6 +178,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

0 comments on commit f2b8eba

Please sign in to comment.