Skip to content

Commit 4634231

Browse files
ryankaskMasu Lin
authored and
Masu Lin
committed
[iOS - Google Maps] Fix animateToCoordinate and animateToRegion (react-native-maps#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
1 parent d62f5ef commit 4634231

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

example/examples/DisplayLatLng.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,25 @@ class DisplayLatLng extends React.Component {
4343
this.map.animateToRegion(this.randomRegion());
4444
}
4545

46-
randomRegion() {
47-
const { region } = this.state;
46+
animateRandomCoordinate() {
47+
this.map.animateToCoordinate(this.randomCoordinate());
48+
}
49+
50+
randomCoordinate() {
51+
const region = this.state.region;
4852
return {
49-
...this.state.region,
5053
latitude: region.latitude + ((Math.random() - 0.5) * (region.latitudeDelta / 2)),
5154
longitude: region.longitude + ((Math.random() - 0.5) * (region.longitudeDelta / 2)),
5255
};
5356
}
5457

58+
randomRegion() {
59+
return {
60+
...this.state.region,
61+
...this.randomCoordinate(),
62+
};
63+
}
64+
5565
render() {
5666
return (
5767
<View style={styles.container}>
@@ -74,13 +84,19 @@ class DisplayLatLng extends React.Component {
7484
onPress={() => this.jumpRandom()}
7585
style={[styles.bubble, styles.button]}
7686
>
77-
<Text>Jump</Text>
87+
<Text style={styles.buttonText}>Jump</Text>
7888
</TouchableOpacity>
7989
<TouchableOpacity
8090
onPress={() => this.animateRandom()}
8191
style={[styles.bubble, styles.button]}
8292
>
83-
<Text>Animate</Text>
93+
<Text style={styles.buttonText}>Animate (Region)</Text>
94+
</TouchableOpacity>
95+
<TouchableOpacity
96+
onPress={() => this.animateRandomCoordinate()}
97+
style={[styles.bubble, styles.button]}
98+
>
99+
<Text style={styles.buttonText}>Animate (Coordinate)</Text>
84100
</TouchableOpacity>
85101
</View>
86102
</View>
@@ -112,16 +128,20 @@ const styles = StyleSheet.create({
112128
alignItems: 'stretch',
113129
},
114130
button: {
115-
width: 80,
116-
paddingHorizontal: 12,
131+
width: 100,
132+
paddingHorizontal: 8,
117133
alignItems: 'center',
118-
marginHorizontal: 10,
134+
justifyContent: 'center',
135+
marginHorizontal: 5,
119136
},
120137
buttonContainer: {
121138
flexDirection: 'row',
122139
marginVertical: 20,
123140
backgroundColor: 'transparent',
124141
},
142+
buttonText: {
143+
textAlign: 'center',
144+
},
125145
});
126146

127147
module.exports = DisplayLatLng;

lib/ios/AirGoogleMaps/AIRGoogleMapManager.m

+26-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "RCTConvert+AirMap.h"
2727

2828
#import <MapKit/MapKit.h>
29+
#import <QuartzCore/QuartzCore.h>
2930

3031
static NSString *const RCTMapViewKey = @"MapView";
3132

@@ -75,10 +76,31 @@ - (UIView *)view
7576
if (![view isKindOfClass:[AIRGoogleMap class]]) {
7677
RCTLogError(@"Invalid view returned from registry, expecting AIRGoogleMap, got: %@", view);
7778
} else {
78-
[AIRGoogleMap animateWithDuration:duration/1000 animations:^{
79-
GMSCameraPosition* camera = [AIRGoogleMap makeGMSCameraPositionFromMap:(AIRGoogleMap *)view andMKCoordinateRegion:region];
80-
[(AIRGoogleMap *)view animateToCameraPosition:camera];
81-
}];
79+
// Core Animation must be used to control the animation's duration
80+
// See http://stackoverflow.com/a/15663039/171744
81+
[CATransaction begin];
82+
[CATransaction setAnimationDuration:duration/1000];
83+
AIRGoogleMap *mapView = (AIRGoogleMap *)view;
84+
GMSCameraPosition *camera = [AIRGoogleMap makeGMSCameraPositionFromMap:mapView andMKCoordinateRegion:region];
85+
[mapView animateToCameraPosition:camera];
86+
[CATransaction commit];
87+
}
88+
}];
89+
}
90+
91+
RCT_EXPORT_METHOD(animateToCoordinate:(nonnull NSNumber *)reactTag
92+
withRegion:(CLLocationCoordinate2D)latlng
93+
withDuration:(CGFloat)duration)
94+
{
95+
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
96+
id view = viewRegistry[reactTag];
97+
if (![view isKindOfClass:[AIRGoogleMap class]]) {
98+
RCTLogError(@"Invalid view returned from registry, expecting AIRGoogleMap, got: %@", view);
99+
} else {
100+
[CATransaction begin];
101+
[CATransaction setAnimationDuration:duration/1000];
102+
[(AIRGoogleMap *)view animateToLocation:latlng];
103+
[CATransaction commit];
82104
}
83105
}];
84106
}

0 commit comments

Comments
 (0)