0

Search Nearby Locations

by
Published Nov 5, 2024

Retrieves up to 10 locations found near the given latitude/longitude.

Script tripadvisor Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Tripadvisor = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	resource: Tripadvisor,
8
	latLong: string,
9
	options?: {
10
		category?: 'hotels' | 'attractions' | 'restaurants' | 'geos'
11
		address?: string
12
		radius?: number
13
		radiusUnit?: 'km' | 'mi' | 'm'
14
		language?: string
15
	}
16
) {
17
	const queryParams = new URLSearchParams({
18
		key: resource.apiKey,
19
		latLong
20
	})
21

22
	if (options?.category) {
23
		queryParams.append('category', options.category)
24
	}
25

26
	if (options?.address) {
27
		queryParams.append('address', options.address)
28
	}
29

30
	if (options?.radius) {
31
		queryParams.append('radius', options.radius.toString())
32
	}
33

34
	if (options?.radiusUnit) {
35
		queryParams.append('radiusUnit', options.radiusUnit)
36
	}
37

38
	if (options?.language) {
39
		queryParams.append('language', options.language)
40
	}
41

42
	const endpoint = `https://api.content.tripadvisor.com/api/v1/location/nearby_search?${queryParams.toString()}`
43

44
	const response = await fetch(endpoint, {
45
		method: 'GET',
46
		headers: {
47
			accept: 'application/json'
48
		}
49
	})
50

51
	if (!response.ok) {
52
		throw new Error(`HTTP error! status: ${response.status}`)
53
	}
54

55
	const data = await response.json()
56

57
	return data
58
}
59