@@ -7,7 +7,12 @@ import {
7
7
TextInput ,
8
8
View ,
9
9
} from 'react-native' ;
10
- import { AutosuggestClient , AutosuggestSuggestion } from '@what3words/api' ;
10
+ import {
11
+ AutosuggestClient ,
12
+ AutosuggestInputType ,
13
+ type AutosuggestOptions ,
14
+ type AutosuggestSuggestion ,
15
+ } from '@what3words/api' ;
11
16
import { useState } from 'react' ;
12
17
13
18
const API_KEY = '<YOUR_API_KEY>' ;
@@ -43,19 +48,41 @@ export default function App() {
43
48
// Cancel previous request before sending a new one
44
49
if ( timeoutId ) clearTimeout ( timeoutId ) ;
45
50
46
- timeoutId = setTimeout ( async ( ) => {
47
- try {
48
- setIsLoading ( true ) ;
49
- setError ( null ) ;
50
- const options = { input : search } ;
51
- const { suggestions } = await client . run ( options ) ;
52
- setSuggestions ( suggestions ) ;
53
- } catch ( error ) {
54
- setError ( error ) ;
55
- } finally {
56
- setIsLoading ( false ) ;
57
- }
58
- } , DEBOUNCE_TIME ) ;
51
+ // Debounce the request to avoid sending too many requests while the user is typing
52
+ timeoutId = await new Promise ( resolve =>
53
+ setTimeout ( resolve , DEBOUNCE_TIME )
54
+ ) ;
55
+
56
+ try {
57
+ setIsLoading ( true ) ;
58
+ setError ( null ) ;
59
+ const options : AutosuggestOptions = {
60
+ input : search ,
61
+ inputType : AutosuggestInputType . Text ,
62
+ nResults : 3 ,
63
+ focus : { lat : 51.521251 , lng : - 0.203586 } ,
64
+ clipToCountry : [ 'GB' ] ,
65
+ clipToCircle : { center : { lat : 51.521 , lng : - 0.343 } , radius : 142 } ,
66
+ clipToBoundingBox : {
67
+ southwest : { lat : 51.521 , lng : - 0.343 } ,
68
+ northeast : { lat : 52.6 , lng : 2.3324 } ,
69
+ } ,
70
+ clipToPolygon : [
71
+ { lat : 51.521 , lng : - 0.343 } ,
72
+ { lat : 52.6 , lng : 2.3324 } ,
73
+ { lat : 54.234 , lng : 8.343 } ,
74
+ { lat : 51.521 , lng : - 0.343 } ,
75
+ ] ,
76
+ language : 'en' ,
77
+ preferLand : true ,
78
+ } ;
79
+ const { suggestions } = await client . run ( options ) ;
80
+ setSuggestions ( suggestions ) ;
81
+ } catch ( error ) {
82
+ setError ( error ) ;
83
+ } finally {
84
+ setIsLoading ( false ) ;
85
+ }
59
86
}
60
87
61
88
/**
0 commit comments