|
| 1 | +import Expo, { Components } from 'expo'; |
| 2 | +import React from 'react'; |
| 3 | +import ProgressiveInput from 'react-native-progressive-input'; |
| 4 | +import { StyleSheet, Text, View, ListView, TouchableOpacity } from 'react-native'; |
| 5 | + |
| 6 | +const GOOGLE_API_KEY = 'AIzaSyB7-8qph-zszuxivIm7cwT5b37D22bm1A4'; |
| 7 | +const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1.id !== r2.id }); |
| 8 | +const latitudeDelta = 0.0922; |
| 9 | +const longitudeDelta = 0.0421; |
| 10 | + |
| 11 | +class App extends React.Component { |
| 12 | + constructor(props) { |
| 13 | + super(props); |
| 14 | + this.state = { |
| 15 | + isLoading: false, |
| 16 | + dataSource: ds.cloneWithRows([]), |
| 17 | + value: '', |
| 18 | + }; |
| 19 | + this.searchLocation = this.searchLocation.bind(this); |
| 20 | + this.renderRow = this.renderRow.bind(this); |
| 21 | + this.renderSeparator = this.renderSeparator.bind(this); |
| 22 | + this.onInputCleared = this.onInputCleared.bind(this); |
| 23 | + } |
| 24 | + |
| 25 | + async searchLocation(query) { |
| 26 | + const url = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${GOOGLE_API_KEY}&input=${query}`; |
| 27 | + this.setState({ isLoading: true, value: query }); |
| 28 | + const response = await fetch(url); |
| 29 | + const jsonResponse = await response.json(); |
| 30 | + this.setState({ isLoading: false, dataSource: ds.cloneWithRows(jsonResponse.predictions) }); |
| 31 | + } |
| 32 | + |
| 33 | + renderRow(prediction) { |
| 34 | + return ( |
| 35 | + <TouchableOpacity onPress={() => this.onListItemClicked(prediction)} style={styles.listItem}> |
| 36 | + <Text>{prediction.description}</Text> |
| 37 | + </TouchableOpacity> |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + renderSeparator() { |
| 42 | + return <View style={styles.listItemSeparator} />; |
| 43 | + } |
| 44 | + |
| 45 | + onInputCleared() { |
| 46 | + this.setState({ value: '', isLoading: false, dataSource: ds.cloneWithRows([]) }); |
| 47 | + } |
| 48 | + |
| 49 | + async onListItemClicked(prediction) { |
| 50 | + this.setState({ value: prediction.description, dataSource: ds.cloneWithRows([]), isLoading: true }); |
| 51 | + const url = `https://maps.googleapis.com/maps/api/place/details/json?placeid=${prediction.place_id}&key=${GOOGLE_API_KEY}`; |
| 52 | + const response = await fetch(url); |
| 53 | + const jsonResponse = await response.json(); |
| 54 | + const { lat, lng } = jsonResponse.result.geometry.location; |
| 55 | + this.mapView.animateToRegion({ |
| 56 | + longitude: lng, |
| 57 | + latitude: lat, |
| 58 | + latitudeDelta, |
| 59 | + longitudeDelta, |
| 60 | + }); |
| 61 | + this.setState({ isLoading: false }); |
| 62 | + } |
| 63 | + |
| 64 | + render() { |
| 65 | + return ( |
| 66 | + <View style={styles.container}> |
| 67 | + <Expo.MapView |
| 68 | + style={styles.map} |
| 69 | + ref={m => this.mapView = m} |
| 70 | + initialRegion={{ |
| 71 | + latitude: 37.78825, |
| 72 | + longitude: -122.4324, |
| 73 | + latitudeDelta, |
| 74 | + longitudeDelta, |
| 75 | + }} |
| 76 | + /> |
| 77 | + <ProgressiveInput |
| 78 | + value={this.state.value} |
| 79 | + style={styles.progressiveInput} |
| 80 | + isLoading={this.state.isLoading} |
| 81 | + onChangeText={this.searchLocation} |
| 82 | + onInputCleared={this.onInputCleared} |
| 83 | + /> |
| 84 | + <View style={styles.listViewContainer}> |
| 85 | + <ListView |
| 86 | + enableEmptySections |
| 87 | + style={styles.listView} |
| 88 | + dataSource={this.state.dataSource} |
| 89 | + renderRow={this.renderRow} |
| 90 | + renderSeparator={this.renderSeparator} |
| 91 | + /> |
| 92 | + </View> |
| 93 | + </View> |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +const styles = StyleSheet.create({ |
| 99 | + container: { |
| 100 | + flex: 1, |
| 101 | + backgroundColor: 'grey', |
| 102 | + flexDirection: 'column', |
| 103 | + justifyContent: 'flex-start', |
| 104 | + }, |
| 105 | + map: { |
| 106 | + position: 'absolute', |
| 107 | + top: 0, |
| 108 | + right: 0, |
| 109 | + left: 0, |
| 110 | + bottom: 0, |
| 111 | + }, |
| 112 | + progressiveInput: { |
| 113 | + backgroundColor: 'white', |
| 114 | + marginTop: 20, |
| 115 | + marginLeft: 10, |
| 116 | + marginRight: 10, |
| 117 | + }, |
| 118 | + listViewContainer: { |
| 119 | + flex: 0, |
| 120 | + }, |
| 121 | + listView: { |
| 122 | + backgroundColor: 'white', |
| 123 | + margin: 10, |
| 124 | + }, |
| 125 | + listItem: { |
| 126 | + padding: 10, |
| 127 | + }, |
| 128 | + listItemSeparator: { |
| 129 | + borderWidth: 0.5, |
| 130 | + borderColor: 'lightgrey', |
| 131 | + }, |
| 132 | +}); |
| 133 | + |
| 134 | +Expo.registerRootComponent(App); |
0 commit comments