Skip to content

Commit

Permalink
wip: create rough draft geocoding function
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanzitting committed Jan 11, 2022
1 parent 43be2bf commit febe6b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
export const REDIS_PORT = parseNumber(process.env.REDIS_PORT) || 6379;
export const REDIS_PASSWORD = process.env.REDIS_PASSWORD || '';
export const GOOGLE_RECAPTCHA_SECRET_KEY = process.env.GOOGLE_RECAPTCHA_SECRET_KEY || '';
export const GOOGLE_GEOCODING_SECRET_KEY = process.env.GOOGLE_GEOCODING_SECRET_KEY || '';
26 changes: 26 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fetch from 'cross-fetch';
import { GOOGLE_GEOCODING_SECRET_KEY } from 'config';

export function parseNumber(str?: string): number | undefined {
const parsed = Number(str);

Expand All @@ -7,3 +10,26 @@ export function parseNumber(str?: string): number | undefined {
export function _throw(msg: string): never {
throw msg;
}

export async function useThisFunctionWheneverYouFindYourselfInTheSituationWhereYouHaveAnAddressButYouWantCoordinates(address: string): Promise<object> {
// Prepare the address for submission to Google's API
address = address.replace(/ /g, "%20");
let requestUrl: string = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${GOOGLE_MAPS_API_KEY}`

// Submit it to the API
const geocodingRes = await fetch(requestUrl)
.catch(err => {
console.log(err);
});

console.log(geocodingRes);

// Process the response
const coordinates: object = {
lat: geocodingRes.geometry.location.lat,
lng: geocodingRes.geometry.location.lng
};

// Output the resulting Coordinates
return coordinates;
}

0 comments on commit febe6b7

Please sign in to comment.