-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
175 lines (148 loc) · 4.79 KB
/
App.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { StatusBar } from 'expo-status-bar';
import { useState, useEffect } from 'react';
import { ActivityIndicator, FlatList, Platform, Pressable, SafeAreaView, StyleSheet, Text, TextInput, View } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
const NOMINATIM_BASE_URL = `https://nominatim.openstreetmap.org/search?`;
export default function App() {
const [searchString, setSearchString] = useState('');
const [locationInfo, setLocationInfo] = useState([]);
const [abortController, setAbortController] = useState(new AbortController());
const [loading, setLoading] = useState(false);
const [typingTimeout, setTypingTimeout] = useState(0);
useEffect(() => {
return () => {
// Cleanup the AbortController when the component unmounts
abortController.abort();
};
}, [abortController]);
const handleLocationSearch = (text) => {
// console.log(text);
setLoading(true);
abortController.abort();
const newAbortController = new AbortController();
setAbortController(newAbortController);
// Clear the previous typingTimeout if it exists
clearTimeout(typingTimeout);
const params = {
q: typeof text !== 'string' ? searchString : text,
format: 'json',
addressdetails: 1,
limit: 40,
'accept-language': 'en-US,en;q=0.5'
}
const requestedOptions = {
method: 'GET',
redirect: 'follow'
}
const queryString = new URLSearchParams(params).toString();
// fetch(`${NOMINATIM_BASE_URL}${queryString}`, requestedOptions)
// .then((response) => response.json())
// .then((data) => {
// // console.log(data);
// let x = data.map((val, i) => {
// return {
// osm_id: val.osm_id,
// display_name: val.display_name,
// city: val.address?.city,
// lat: val.lat,
// lon: val.lon
// };
// })
// console.log(x);
// setLocationInfo(x);
// })
// .catch((err) => console.log(err.message));
const newTypingTimeout = setTimeout(() => {
// Make the API request
fetch(`${NOMINATIM_BASE_URL}${queryString}`, requestedOptions)
.then((response) => response.json())
.then((data) => {
// console.log(data);
let x = data.map((val, i) => {
return {
osm_id: val.osm_id,
display_name: val.display_name,
city: val.address?.city,
lat: val.lat,
lon: val.lon
};
})
console.log(x);
setLocationInfo(x);
})
.catch((err) => console.log(err.message))
.finally(() => setLoading(false));
}, 400); // Adjust the debounce time as needed
setTypingTimeout(newTypingTimeout);
}
const Item = ({ osm_id, display_name, lat, lon }) => (
<Pressable
android_ripple={{
color: 'gray'
}}
onPress={() => console.log(lat, lon, display_name.split(',')[0])}
style={{
padding: 10
}}
>
<Text>{display_name}</Text>
<Text>Latitude: {Number(lat).toPrecision(6)}, Longitude: {Number(lon).toPrecision(6)}</Text>
</Pressable>
);
const renderItem = ({ item }) => (
<Item
osm_id={item.osm_id}
display_name={item.display_name}
lat={item.lat}
lon={item.lon}
/>
);
return (
<SafeAreaView style={{ flex: 1, paddingTop: Platform.OS !== 'ios' ? 30 : null }}>
<StatusBar style="auto" />
<View style={styles.container}>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<TextInput
style={{
width: 250,
height: 40,
padding: 10,
borderColor: 'gray',
borderWidth: 1,
borderRadius: 5,
}}
onChangeText={text => {
setSearchString(text);
handleLocationSearch(text);
}}
onSubmitEditing={handleLocationSearch}
value={searchString}
placeholder="Enter your location manually"
placeholderTextColor={'gray'}
/>
<Ionicons
name="ios-search-circle"
size={35}
style={{ backgroundColor: 'black', marginLeft: 5, borderRadius: 5 }}
color="white"
onPress={handleLocationSearch}
/>
</View>
{loading ? <ActivityIndicator style={{paddingTop: 10}} size="small" color="#0000ff" /> : null}
<FlatList
data={locationInfo}
renderItem={renderItem}
keyExtractor={item => item.osm_id}
/>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});