Skip to content

Commit e3fba19

Browse files
committed
Add initialRegion, options props
1 parent 6a2b886 commit e3fba19

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

docs/stories/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ storiesOf('MapView', module)
1414
.add('onRegionChangeComplete', () => (
1515
<View style={styles.container}>
1616
<MapView
17-
region={{ latitude: 48.86, longitude: 2.34 }}
17+
initialRegion={{ latitude: 48.86, longitude: 2.34 }}
1818
onRegionChangeComplete={action('onRegionChangeComplete toggled')}
1919
/>
2020
</View>
@@ -23,6 +23,21 @@ storiesOf('MapView', module)
2323
<View style={styles.container}>
2424
<MapView region={{ latitude: 48.86, longitude: 2.34 }} onPress={action('onPress toggled')} />
2525
</View>
26+
))
27+
.add('options', () => (
28+
<View style={styles.container}>
29+
<MapView
30+
initialRegion={{ latitude: 48.86, longitude: 2.34 }}
31+
options={{
32+
zoomControlOptions: {
33+
position: window.google.maps.ControlPosition.RIGHT_CENTER,
34+
},
35+
mapTypeControl: false,
36+
streetViewControl: false,
37+
fullscreenControl: false,
38+
}}
39+
/>
40+
</View>
2641
));
2742

2843
storiesOf('Marker', module).add('basic', () => (

src/index.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { View, ActivityIndicator, StyleSheet } from 'react-native';
2+
import { View, StyleSheet } from 'react-native';
33
import { withGoogleMap, GoogleMap } from 'react-google-maps';
44
import Marker from './Marker';
55
import Polyline from './Polyline';
@@ -9,41 +9,49 @@ const GoogleMapContainer = withGoogleMap(props => (
99
));
1010

1111
class MapView extends Component {
12-
constructor(props) {
13-
super(props);
14-
this.state = { center: { lat: props.region.latitude, lng: props.region.longitude } };
15-
}
16-
1712
handleMapMounted = map => (this.map = map);
1813

1914
onDragEnd = () => {
20-
const center = this.map.getCenter();
21-
!!this.props.onRegionChangeComplete &&
22-
this.props.onRegionChangeComplete({ latitude: center.lat(), longitude: center.lng() });
15+
const { onRegionChangeComplete } = this.props;
16+
if (this.map && onRegionChangeComplete) {
17+
const center = this.map.getCenter();
18+
onRegionChangeComplete({
19+
latitude: center.lat(),
20+
longitude: center.lng(),
21+
});
22+
}
2323
};
2424

2525
render() {
26+
const { region, initialRegion, onRegionChange, onPress, options } = this.props;
2627
const style = this.props.style || styles.container;
2728

28-
if (!this.state.center) {
29-
return (
30-
<View style={style}>
31-
<ActivityIndicator />
32-
</View>
33-
);
34-
}
29+
const centerProps = region
30+
? {
31+
center: {
32+
lat: region.latitude,
33+
lng: region.longitude,
34+
},
35+
}
36+
: {
37+
defaultCenter: {
38+
lat: initialRegion.latitude,
39+
lng: initialRegion.longitude,
40+
},
41+
};
3542

3643
return (
3744
<View style={style}>
3845
<GoogleMapContainer
3946
handleMapMounted={this.handleMapMounted}
4047
containerElement={<div style={{ height: '100%' }} />}
4148
mapElement={<div style={{ height: '100%' }} />}
42-
center={this.state.center}
43-
onDragStart={!!this.props.onRegionChange && this.props.onRegionChange}
49+
{...centerProps}
50+
onDragStart={onRegionChange}
4451
onDragEnd={this.onDragEnd}
4552
defaultZoom={15}
46-
onClick={this.props.onPress}>
53+
onClick={onPress}
54+
options={options}>
4755
{this.props.children}
4856
</GoogleMapContainer>
4957
</View>

0 commit comments

Comments
 (0)