11import React , { Component } from 'react' ;
2- import { View , ActivityIndicator , StyleSheet } from 'react-native' ;
2+ import { View , StyleSheet } from 'react-native' ;
33import { withGoogleMap , GoogleMap } from 'react-google-maps' ;
44import Marker from './Marker' ;
55import Polyline from './Polyline' ;
@@ -9,41 +9,49 @@ const GoogleMapContainer = withGoogleMap(props => (
99) ) ;
1010
1111class 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