-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathindex.js
executable file
·128 lines (113 loc) · 3.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
import { withGoogleMap, GoogleMap } from 'react-google-maps';
import Marker from './Marker';
import Polyline from './Polyline';
import Callout from './Callout';
import Geojson from './Geojson';
const GoogleMapContainer = withGoogleMap(props => (
<GoogleMap {...props} ref={props.handleMapMounted} />
));
function googleToReact(point) {
return {
latitude: point.lat(),
longitude: point.lng(),
};
}
function reactToGoogle(point) {
return {
lat: point.latitude,
lng: point.longitude,
};
}
class MapView extends Component {
state = {
center: null,
};
handleMapMounted = map => {
this.map = map;
this.props.onMapReady && this.props.onMapReady();
};
getCamera = () => {
return {
zoom: this.map.getZoom(),
center: this.map.getCenter(),
heading: this.map.getHeading(),
};
};
animateCamera(camera) {
this.setState({ zoom: camera.zoom });
this.setState({ center: reactToGoogle(camera.center) });
}
animateToRegion(coordinates) {
this.setState({
center: reactToGoogle(coordinates),
});
}
async getMapBoundaries() {
const bounds = this.map.getBounds();
return {
northEast: googleToReact(bounds.getNorthEast()),
southWest: googleToReact(bounds.getSouthWest()),
};
}
onDragEnd = () => {
const { onRegionChangeComplete } = this.props;
if (this.map && onRegionChangeComplete) {
const center = this.map.getCenter();
onRegionChangeComplete(googleToReact(center));
}
};
render() {
const { region, initialRegion, onRegionChange, onPress, options, defaultZoom } = this.props;
const { center } = this.state;
const style = this.props.style || styles.container;
const googleMapProps = center
? { center }
: region
? {
center: reactToGoogle(region),
}
: {
defaultCenter: reactToGoogle(initialRegion),
};
const zoom =
defaultZoom ||
(region && region.latitudeDelta
? Math.round(Math.log(360 / region.latitudeDelta) / Math.LN2)
: initialRegion && initialRegion.latitudeDelta
? Math.round(Math.log(360 / initialRegion.latitudeDelta) / Math.LN2)
: 15);
googleMapProps['zoom'] = this.state.zoom ? this.state.zoom : zoom;
return (
<View style={style}>
<GoogleMapContainer
handleMapMounted={this.handleMapMounted}
containerElement={<div style={{ height: '100%' }} />}
mapElement={<div style={{ height: '100%' }} />}
onZoomChanged={() => {
this.setState({ zoom: this.map.getZoom() });
}}
{...googleMapProps}
onDragStart={onRegionChange}
onIdle={this.onDragEnd}
defaultZoom={zoom}
onClick={onPress}
options={options}>
{this.props.children}
</GoogleMapContainer>
</View>
);
}
}
MapView.Marker = Marker;
MapView.Polyline = Polyline;
MapView.Callout = Callout;
MapView.Geojson = Geojson;
export { Marker, Polyline, Callout, Geojson };
const styles = StyleSheet.create({
container: {
height: '100%',
},
});
export default MapView;