Skip to content

Commit

Permalink
Merge pull request Shelf-nu#634 from Shelf-nu/fix-geocoding
Browse files Browse the repository at this point in the history
fix: updating how geocoding is handled to fit to the new API requirements
  • Loading branch information
DonKoko authored Dec 28, 2023
2 parents 06674f5 + 169df43 commit 8cfe0b8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ SMTP_USER="[email protected]"
MAPTILER_TOKEN="maptiler-token"
MICROSOFT_CLARITY_ID="microsoft-clarity-id"

INVITE_TOKEN_SECRET="secret-test-invite"
INVITE_TOKEN_SECRET="secret-test-invite"
GEOCODE_API_KEY="geocode-api-key"
6 changes: 6 additions & 0 deletions app/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare global {
SMTP_HOST: string;
SMTP_USER: string;
MAINTENANCE_MODE: string;
GEOCODE_API_KEY: string;
}
}
}
Expand Down Expand Up @@ -107,6 +108,11 @@ export const FORMBRICKS_ENV_ID = getEnv("FORMBRICKS_ENV_ID", {
isRequired: false,
});

export const GEOCODE_API_KEY = getEnv("GEOCODE_API_KEY", {
isSecret: true,
isRequired: false,
});

export const MAINTENANCE_MODE =
getEnv("MAINTENANCE_MODE", {
isSecret: false,
Expand Down
12 changes: 10 additions & 2 deletions app/utils/geolocate.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { GEOCODE_API_KEY } from "./env";

export const geolocate = async (
address: string | null
): Promise<{ lat: number; lon: number } | null> => {
if (!address || address === "") return null;
if (!address || address === "" || !GEOCODE_API_KEY) return null;
// Create URL object and add the address to the url params
const url = new URL("https://geocode.maps.co/search");
url.searchParams.append("q", address);
url.searchParams.append("api_key", GEOCODE_API_KEY);

const request = await fetch(url.href);

const request = await fetch(`https://geocode.maps.co/search?q=${address}`);
if (!request.ok) return null;
const response = await request.json();
/** Here we take the frist entry of the array.
* When there are more entries that means the address is not accurate enought so we just take the first option
Expand Down
5 changes: 5 additions & 0 deletions docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,8 @@ Shelf hosted version has some premium features that are locked behind different
```
ENABLE_PREMIUM_FEATURES="true"
```

## Geocoding

The locations module allows the users to put an address in a free text field for their "location". For geocoding we use [https://geocode.maps.co/](https://geocode.maps.co/) which is a free api for up to 1 000 000 requests per month.
If you want to use it you need to provide an env variable called `GEOCODE_API_KEY` with your api key. If you would like to use a different api, you can find the code for requesting the location details in `app/utils/geolocate.server.ts`.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8cfe0b8

Please sign in to comment.