-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add ReverseGeocode fn to convert coordinates into a human-readable …
…address
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createReverseGeocode } from './reverseGeocode'; | ||
import { expect, it } from 'vitest'; | ||
|
||
const reverseGeocode = createReverseGeocode({ | ||
mapboxAccessToken: process.env.MAPBOX_ACCESS_TOKEN || 'YOUR_FALLBACK_MAPBOX_ACCESS_TOKEN', | ||
}); | ||
|
||
it('should successfully return an address from coordinates', async () => { | ||
const result = await reverseGeocode.execute({ | ||
latitude: 40.7128, | ||
longitude: -74.006, | ||
}); | ||
|
||
console.log(result) | ||
|
||
expect(result).toMatchObject({ | ||
address: expect.stringContaining(','), | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// reverseGeocode.ts | ||
import { z } from 'zod'; | ||
|
||
function createReverseGeocode({ | ||
mapboxAccessToken, | ||
}: { | ||
mapboxAccessToken: string; | ||
}) { | ||
if (!mapboxAccessToken) { | ||
throw new Error('Please set the Mapbox Access Token in the plugin settings.'); | ||
} | ||
|
||
const paramsSchema = z.object({ | ||
latitude: z.number(), | ||
longitude: z.number(), | ||
}); | ||
|
||
const name = 'reverseGeocode'; | ||
const description = ` | ||
Converts latitude and longitude coordinates to a human-readable address using the Mapbox Geocoding API. | ||
latitude: The latitude of the location to be geocoded. | ||
longitude: The longitude of the location to be geocoded. | ||
`; | ||
|
||
const execute = async ({ latitude, longitude }: z.infer<typeof paramsSchema>) => { | ||
const accessToken = mapboxAccessToken; | ||
|
||
if (!accessToken) { | ||
throw new Error('Please set the Mapbox Access Token in the plugin settings.'); | ||
} | ||
|
||
const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${longitude},${latitude}.json?access_token=${accessToken}`; | ||
|
||
let response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
|
||
const data = await response.json(); | ||
if (!data || !data.features || !data.features.length) { | ||
throw new Error('No address found for the provided coordinates.'); | ||
} | ||
|
||
// Assuming the first feature is the most relevant result | ||
const place = data.features[0]; | ||
return { | ||
address: place.place_name, | ||
}; | ||
}; | ||
|
||
return { paramsSchema, name, description, execute }; | ||
} | ||
|
||
export { createReverseGeocode }; |