-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathautocomplete.js
93 lines (73 loc) · 2.13 KB
/
autocomplete.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
import React, { Component } from 'react';
import Map, { Marker } from '../../src/index';
import styles from './autocomplete.module.css';
class Contents extends Component {
state = {
position: null
};
componentDidMount() {
this.renderAutoComplete();
}
componentDidUpdate(prevProps) {
if (this.props !== prevProps.map) this.renderAutoComplete();
}
onSubmit(e) {
e.preventDefault();
}
renderAutoComplete() {
const { google, map } = this.props;
if (!google || !map) return;
const autocomplete = new google.maps.places.Autocomplete(this.autocomplete);
autocomplete.bindTo('bounds', map);
autocomplete.addListener('place_changed', () => {
const place = autocomplete.getPlace();
if (!place.geometry) return;
if (place.geometry.viewport) map.fitBounds(place.geometry.viewport);
else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
this.setState({ position: place.geometry.location });
});
}
render() {
const { position } = this.state;
return (
<div className={styles.flexWrapper}>
<div className={styles.left}>
<form onSubmit={this.onSubmit}>
<input
placeholder="Enter a location"
ref={ref => (this.autocomplete = ref)}
type="text"
/>
<input className={styles.button} type="submit" value="Go" />
</form>
<div>
<div>Lat: {position && position.lat()}</div>
<div>Lng: {position && position.lng()}</div>
</div>
</div>
<div className={styles.right}>
<Map
{...this.props}
center={position}
centerAroundCurrentLocation={false}
containerStyle={{
height: '100vh',
position: 'relative',
width: '100%'
}}>
<Marker position={position} />
</Map>
</div>
</div>
);
}
}
const MapWrapper = props => (
<Map className="map" google={props.google} visible={false}>
<Contents {...props} />
</Map>
);
export default MapWrapper;