aretherecookies-mobile/js/apis/GoogleMapsApi.js
2019-09-21 15:45:07 +00:00

33 lines
776 B
JavaScript

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