-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoCompletePredictions.tsx
More file actions
129 lines (120 loc) · 3.16 KB
/
AutoCompletePredictions.tsx
File metadata and controls
129 lines (120 loc) · 3.16 KB
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
129
import React, { useState, useEffect } from 'react';
import {
View,
TextInput,
ScrollView,
Text,
StyleSheet,
Pressable,
} from 'react-native';
import { useDebounce } from './Utils';
import RNGooglePlacesCompat from 'react-native-google-places-compat';
import type { GMSTypes } from '../../src/types';
import { LOCATION_ONLY_FIELDS } from '../../src/';
export default function SearchScreen() {
const [query, setQuery] = useState('');
const [predictions, setPredictions] = useState<
GMSTypes.AutocompletePrediction[]
>([]);
const [selectedPlace, setSelectedPlace] = useState<GMSTypes.Place>();
const debouncedQuery = useDebounce(query, 500); // Debounce query with a 500ms delay
useEffect(() => {
RNGooglePlacesCompat.setSessionBasedAutocomplete(true);
}, []);
useEffect(() => {
const fetchPredictions = async () => {
if (debouncedQuery) {
try {
const results = await RNGooglePlacesCompat.getAutocompletePredictions(
debouncedQuery,
{
countries: ['US', 'IN'],
}
);
setPredictions(results);
} catch (error) {
console.error(error);
setPredictions([]);
}
} else {
setPredictions([]);
}
};
fetchPredictions();
}, [debouncedQuery]); // Effect depends on the debounced query
const selectPlace = async (placeId: string) => {
try {
setPredictions([]);
console.log('Searching place by id - ', placeId);
const place = await RNGooglePlacesCompat.lookUpPlaceByID(
placeId,
LOCATION_ONLY_FIELDS
);
setSelectedPlace(place);
console.log('Place found - ', place);
} catch (error) {
console.error(error);
}
};
return (
<View style={styles.container}>
<TextInput
style={styles.searchBar}
onChangeText={setQuery}
value={query}
placeholder="Search places"
placeholderTextColor="#999"
/>
<ScrollView>
{predictions.map((prediction) => (
<Pressable
key={prediction.placeID}
style={styles.predictionItem}
onPress={() => selectPlace(prediction.placeID)}
>
<Text>{prediction.primaryText}</Text>
<Text>{prediction.secondaryText}</Text>
</Pressable>
))}
</ScrollView>
{selectedPlace && (
<View style={styles.placeDetails}>
<Text style={styles.placeName}>{selectedPlace.name}</Text>
<Text>{selectedPlace.address}</Text>
<Text>
{selectedPlace.location.latitude},{selectedPlace.location.longitude}
</Text>
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 10,
width: '100%',
},
searchBar: {
height: 40,
width: '100%',
borderColor: 'gray',
borderWidth: 1,
paddingLeft: 10,
borderRadius: 5,
},
predictionItem: {
padding: 10,
borderBottomWidth: 1,
borderBottomColor: '#ccc',
color: '#777',
},
placeDetails: {
marginTop: 20,
alignItems: 'center',
},
placeName: {
fontSize: 18,
fontWeight: 'bold',
},
});