Skip to content

Commit 6b0cf63

Browse files
committed
add animateCamera and fix animateToCenter
1 parent 66a0798 commit 6b0cf63

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

docs/stories/index.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { action } from '@storybook/addon-actions';
88
storiesOf('MapView', module)
99
.add('basic', () => (
1010
<View style={styles.container}>
11-
<MapView region={{ latitude: 48.86, longitude: 2.34 }} />
11+
<MapView defaultZoom={15} region={{ latitude: 48.86, longitude: 2.34 }} />
12+
<MapView defaultZoom={10} region={{ latitude: 48.86, longitude: 2.34 }} />
1213
</View>
1314
))
1415
.add('onRegionChangeComplete', () => (
@@ -48,26 +49,47 @@ storiesOf('MapView', module)
4849
storiesOf('Marker', module)
4950
.add('basic', () => (
5051
<View style={styles.container}>
51-
<MapView region={{ latitude: 48.88, longitude: 2.32 }}>
52+
<MapView ref={map => (this.map = map)} region={{ latitude: 48.88, longitude: 2.32 }}>
5253
<MapView.Marker
5354
title="BAM"
5455
description="Shape the future of mobile with us"
5556
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
56-
onPress={action('onPresss')}
57+
onPress={() => {
58+
this.map.animateToRegion({
59+
latitude: 48.8828463,
60+
longitude: 2.3229091,
61+
latitudeDelta: 0.1,
62+
longitudeDelta: 0.1,
63+
});
64+
}}
65+
/>
66+
<MapView.Marker
67+
title="BAM"
68+
description="Shape the future of mobile with us"
69+
coordinate={{ latitude: 48.8828463, longitude: 2.3 }}
70+
onPress={() => {
71+
this.map.animateCamera({
72+
zoom: 20,
73+
center: {
74+
lat: 48.8828463,
75+
lng: 2.3,
76+
},
77+
});
78+
}}
5779
/>
5880
</MapView>
5981
</View>
6082
))
6183
.add('Callout', () => (
6284
<View style={styles.container}>
63-
<MapView region={{ latitude: 48.88, longitude: 2.32 }}>
85+
<MapView ref={map => (this.map = map)} region={{ latitude: 48.88, longitude: 2.32 }}>
6486
<MapView.Marker
6587
title="BAM"
6688
ref={marker => (this.marker = marker)}
6789
description="Shape the future of mobile with us"
6890
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
6991
onPress={() => {
70-
this.marker.showCallout();
92+
this.marker1.showCallout();
7193
}}>
7294
<MapView.Callout onPress={action('onPress callout')}>
7395
<View style={{ padding: 10 }}>

src/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class MapView extends Component {
1919
this.props.onMapReady && this.props.onMapReady();
2020
};
2121

22+
animateCamera(camera) {
23+
this.setState({ zoom: camera.zoom });
24+
this.setState({ center: camera.center });
25+
}
26+
2227
animateToRegion(coordinates) {
2328
this.setState({ center: { lat: coordinates.latitude, lng: coordinates.longitude } });
2429
}
@@ -35,38 +40,44 @@ class MapView extends Component {
3540
};
3641

3742
render() {
38-
const { region, initialRegion, onRegionChange, onPress, options } = this.props;
43+
const { region, initialRegion, onRegionChange, onPress, options, defaultZoom } = this.props;
3944
const { center } = this.state;
4045
const style = this.props.style || styles.container;
4146

42-
const centerProps = region
47+
const googleMapProps = center
48+
? { center }
49+
: region
4350
? {
4451
center: {
4552
lat: region.latitude,
4653
lng: region.longitude,
4754
},
4855
}
49-
: center
50-
? { center }
5156
: {
5257
defaultCenter: {
5358
lat: initialRegion.latitude,
5459
lng: initialRegion.longitude,
5560
},
5661
};
5762
const zoom =
58-
region && region.latitudeDelta
63+
defaultZoom ||
64+
(region && region.latitudeDelta
5965
? Math.round(Math.log(360 / region.latitudeDelta) / Math.LN2)
6066
: initialRegion && initialRegion.latitudeDelta
6167
? Math.round(Math.log(360 / initialRegion.latitudeDelta) / Math.LN2)
62-
: 15;
68+
: 15);
69+
googleMapProps['zoom'] = this.state.zoom ? this.state.zoom : zoom;
6370
return (
6471
<View style={style}>
6572
<GoogleMapContainer
73+
ref={map => (this.map = map)}
6674
handleMapMounted={this.handleMapMounted}
6775
containerElement={<div style={{ height: '100%' }} />}
6876
mapElement={<div style={{ height: '100%' }} />}
69-
{...centerProps}
77+
onZoomChanged={() => {
78+
this.setState({ zoom: this.map.getZoom });
79+
}}
80+
{...googleMapProps}
7081
onDragStart={onRegionChange}
7182
onIdle={this.onDragEnd}
7283
defaultZoom={zoom}

0 commit comments

Comments
 (0)