2018-05-19 11:17:08 -05:00
|
|
|
// @flow
|
2019-09-21 15:45:07 +00:00
|
|
|
import { GoogleAPIKey } from "../constants/AppConstants";
|
|
|
|
|
import { path } from "ramda";
|
2018-05-19 11:17:08 -05:00
|
|
|
|
2019-09-21 15:45:07 +00:00
|
|
|
const url = "https://maps.googleapis.com/maps/api/geocode/json";
|
2018-05-19 11:17:08 -05:00
|
|
|
|
|
|
|
|
type Location = {
|
|
|
|
|
lat: number,
|
2019-09-21 15:45:07 +00:00
|
|
|
lng: number
|
2018-05-19 11:17:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type AddressComponent = {
|
|
|
|
|
long_name: string,
|
|
|
|
|
short_name: string,
|
2019-09-21 15:45:07 +00:00
|
|
|
types: Array<string>
|
2018-05-19 11:17:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type GeocodeResult = {
|
|
|
|
|
results: Array<{
|
|
|
|
|
address_components: Array<AddressComponent>,
|
|
|
|
|
formatted_address: string,
|
|
|
|
|
geometry: {
|
2019-09-21 15:45:07 +00:00
|
|
|
location: Location
|
2018-05-19 11:17:08 -05:00
|
|
|
},
|
|
|
|
|
place_id: string,
|
2019-09-21 15:45:07 +00:00
|
|
|
types: Array<string>
|
|
|
|
|
}>
|
2018-05-19 11:17:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getCoordsFromZip = async (zip: string): Promise<Location> => {
|
2019-09-21 15:45:07 +00:00
|
|
|
const res: GeocodeResult = await (await fetch(`${url}?key=${GoogleAPIKey}&address=${zip}`)).json();
|
|
|
|
|
return path(["results", 0, "geometry", "location"], res) || { lat: 0, lng: 0 };
|
2018-05-19 11:17:08 -05:00
|
|
|
};
|