From e895ca420c16d41b1f763a49b5eaa72423bd3898 Mon Sep 17 00:00:00 2001 From: Ryan Kaskel Date: Thu, 15 Jun 2017 17:34:23 +0100 Subject: [PATCH] [iOS - Google Maps] Fix animateToCoordinate and animateToRegion (#1115) * Add animateToCoordinate to Google Maps on iOS * Add animate to random coordinate button in example app * Fix animateToRegion duration for Google Maps on iOS --- example/examples/DisplayLatLng.js | 36 ++++++++++++++++----- lib/ios/AirGoogleMaps/AIRGoogleMapManager.m | 30 ++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/example/examples/DisplayLatLng.js b/example/examples/DisplayLatLng.js index 573617d41..aa2dde2ac 100644 --- a/example/examples/DisplayLatLng.js +++ b/example/examples/DisplayLatLng.js @@ -43,15 +43,25 @@ class DisplayLatLng extends React.Component { this.map.animateToRegion(this.randomRegion()); } - randomRegion() { - const { region } = this.state; + animateRandomCoordinate() { + this.map.animateToCoordinate(this.randomCoordinate()); + } + + randomCoordinate() { + const region = this.state.region; return { - ...this.state.region, latitude: region.latitude + ((Math.random() - 0.5) * (region.latitudeDelta / 2)), longitude: region.longitude + ((Math.random() - 0.5) * (region.longitudeDelta / 2)), }; } + randomRegion() { + return { + ...this.state.region, + ...this.randomCoordinate(), + }; + } + render() { return ( @@ -74,13 +84,19 @@ class DisplayLatLng extends React.Component { onPress={() => this.jumpRandom()} style={[styles.bubble, styles.button]} > - Jump + Jump this.animateRandom()} style={[styles.bubble, styles.button]} > - Animate + Animate (Region) + + this.animateRandomCoordinate()} + style={[styles.bubble, styles.button]} + > + Animate (Coordinate) @@ -112,16 +128,20 @@ const styles = StyleSheet.create({ alignItems: 'stretch', }, button: { - width: 80, - paddingHorizontal: 12, + width: 100, + paddingHorizontal: 8, alignItems: 'center', - marginHorizontal: 10, + justifyContent: 'center', + marginHorizontal: 5, }, buttonContainer: { flexDirection: 'row', marginVertical: 20, backgroundColor: 'transparent', }, + buttonText: { + textAlign: 'center', + }, }); module.exports = DisplayLatLng; diff --git a/lib/ios/AirGoogleMaps/AIRGoogleMapManager.m b/lib/ios/AirGoogleMaps/AIRGoogleMapManager.m index 785d58d4d..6a3b92e90 100644 --- a/lib/ios/AirGoogleMaps/AIRGoogleMapManager.m +++ b/lib/ios/AirGoogleMaps/AIRGoogleMapManager.m @@ -26,6 +26,7 @@ #import "RCTConvert+AirMap.h" #import +#import static NSString *const RCTMapViewKey = @"MapView"; @@ -75,10 +76,31 @@ - (UIView *)view if (![view isKindOfClass:[AIRGoogleMap class]]) { RCTLogError(@"Invalid view returned from registry, expecting AIRGoogleMap, got: %@", view); } else { - [AIRGoogleMap animateWithDuration:duration/1000 animations:^{ - GMSCameraPosition* camera = [AIRGoogleMap makeGMSCameraPositionFromMap:(AIRGoogleMap *)view andMKCoordinateRegion:region]; - [(AIRGoogleMap *)view animateToCameraPosition:camera]; - }]; + // Core Animation must be used to control the animation's duration + // See http://stackoverflow.com/a/15663039/171744 + [CATransaction begin]; + [CATransaction setAnimationDuration:duration/1000]; + AIRGoogleMap *mapView = (AIRGoogleMap *)view; + GMSCameraPosition *camera = [AIRGoogleMap makeGMSCameraPositionFromMap:mapView andMKCoordinateRegion:region]; + [mapView animateToCameraPosition:camera]; + [CATransaction commit]; + } + }]; +} + +RCT_EXPORT_METHOD(animateToCoordinate:(nonnull NSNumber *)reactTag + withRegion:(CLLocationCoordinate2D)latlng + withDuration:(CGFloat)duration) +{ + [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary *viewRegistry) { + id view = viewRegistry[reactTag]; + if (![view isKindOfClass:[AIRGoogleMap class]]) { + RCTLogError(@"Invalid view returned from registry, expecting AIRGoogleMap, got: %@", view); + } else { + [CATransaction begin]; + [CATransaction setAnimationDuration:duration/1000]; + [(AIRGoogleMap *)view animateToLocation:latlng]; + [CATransaction commit]; } }]; }