2018-07-08 10:59:43 -05:00
|
|
|
// @flow
|
|
|
|
|
import { type GooglePlaceObj } from '../records/PlaceRecord';
|
|
|
|
|
import { GoogleAPIKey } from '../constants/AppConstants';
|
|
|
|
|
import { pipe, filter, path, contains } from 'ramda';
|
|
|
|
|
|
|
|
|
|
const placesDetailUrl = `https://maps.googleapis.com/maps/api/place/details/json?key=${GoogleAPIKey}`;
|
|
|
|
|
const photosUrl = `https://maps.googleapis.com/maps/api/place/photo?key=${GoogleAPIKey}`;
|
|
|
|
|
const placesFromTextUrl = `https://maps.googleapis.com/maps/api/place/findplacefromtext/json?key=${GoogleAPIKey}`;
|
|
|
|
|
|
|
|
|
|
type GooglePlaceDetailsResponse = { error_message: ?string, result: GooglePlaceObj };
|
2018-07-08 11:58:46 -05:00
|
|
|
type GoogleFindPlaceResponse = { status: string, candidates: Array<GooglePlaceObj> };
|
2018-07-08 10:59:43 -05:00
|
|
|
|
|
|
|
|
const milesToMeters = miles => Math.ceil(miles > 0 ? miles / 0.00062137 : 0);
|
|
|
|
|
|
|
|
|
|
const isEstablishment = pipe(
|
|
|
|
|
path(['types']),
|
|
|
|
|
contains('establishment')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const getPlaceDetails = async (placeid: ?string): Promise<GooglePlaceObj> => {
|
|
|
|
|
if (!placeid || typeof placeid !== 'string') {
|
|
|
|
|
throw new Error('placeid looks wrong');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${placesDetailUrl}&placeid=${placeid}`);
|
|
|
|
|
|
|
|
|
|
const { error_message, result }: GooglePlaceDetailsResponse = await res.json();
|
|
|
|
|
|
|
|
|
|
if (error_message) {
|
|
|
|
|
throw new Error(error_message);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getURLForPhotoReference = (opts?: { maxheight?: number, maxwidth?: number } = {}) => (
|
|
|
|
|
photoReference: string
|
|
|
|
|
) => {
|
|
|
|
|
if (!photoReference) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { maxheight, maxwidth } = opts;
|
|
|
|
|
|
|
|
|
|
const maxHeight = `&maxheight=${maxheight || 600}`;
|
|
|
|
|
const maxWidth = maxwidth ? `&maxwidth=${maxwidth}` : '';
|
|
|
|
|
const photoref = `&photoreference=${photoReference}`;
|
|
|
|
|
|
|
|
|
|
return `${photosUrl}${photoref}${maxHeight}${maxWidth}`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const findNearbyPlaces = async ({
|
|
|
|
|
location,
|
|
|
|
|
search,
|
|
|
|
|
radius,
|
|
|
|
|
}: {
|
|
|
|
|
location?: Position,
|
|
|
|
|
search?: string,
|
|
|
|
|
radius: number,
|
|
|
|
|
}): Promise<any> => {
|
|
|
|
|
if (!location) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
coords: { latitude, longitude },
|
|
|
|
|
} = location;
|
|
|
|
|
|
|
|
|
|
const input = `input=${encodeURIComponent(search || '*')}&inputtype=textquery`;
|
|
|
|
|
const loc = `locationbias=circle:${milesToMeters(radius)}@${latitude},${longitude}`;
|
2018-07-08 12:30:00 -05:00
|
|
|
const fields = 'fields=id,name,geometry,photos,types,formatted_address,opening_hours,place_id';
|
2018-07-08 10:59:43 -05:00
|
|
|
const reqUrl = `${placesFromTextUrl}&${input}&${loc}&${fields}`;
|
|
|
|
|
|
2018-07-08 11:58:46 -05:00
|
|
|
const { candidates, status }: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json();
|
2018-07-08 10:59:43 -05:00
|
|
|
|
|
|
|
|
if (status !== 'OK') {
|
|
|
|
|
throw new Error('google find places request failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filter(isEstablishment, candidates);
|
|
|
|
|
};
|