use nearbysearch instead for getting just restaurants

This commit is contained in:
bartronx7 2018-07-08 18:59:47 -05:00
parent e294a4a866
commit 28cfd7fc0a

View file

@ -1,22 +1,21 @@
// @flow // @flow
import { type GooglePlaceObj } from '../records/PlaceRecord'; import { type GooglePlaceObj } from '../records/PlaceRecord';
import { GoogleAPIKey } from '../constants/AppConstants'; import { GoogleAPIKey } from '../constants/AppConstants';
import { pipe, filter, path, contains } from 'ramda'; // import { concat } from 'ramda';
const placesDetailUrl = `https://maps.googleapis.com/maps/api/place/details/json?key=${GoogleAPIKey}`; 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 photosUrl = `https://maps.googleapis.com/maps/api/place/photo?key=${GoogleAPIKey}`;
const placesFromTextUrl = `https://maps.googleapis.com/maps/api/place/findplacefromtext/json?key=${GoogleAPIKey}`; const placesFromTextUrl = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=${GoogleAPIKey}`;
type GooglePlaceDetailsResponse = { error_message: ?string, result: GooglePlaceObj }; type GooglePlaceDetailsResponse = { error_message: ?string, result: GooglePlaceObj };
type GoogleFindPlaceResponse = { status: string, candidates: Array<GooglePlaceObj> }; type GoogleFindPlaceResponse = {
status: string,
results: Array<GooglePlaceObj>,
next_page_token?: string,
};
const milesToMeters = miles => Math.ceil(miles > 0 ? miles / 0.00062137 : 0); 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> => { export const getPlaceDetails = async (placeid: ?string): Promise<GooglePlaceObj> => {
if (!placeid || typeof placeid !== 'string') { if (!placeid || typeof placeid !== 'string') {
throw new Error('placeid looks wrong'); throw new Error('placeid looks wrong');
@ -48,6 +47,11 @@ export const getURLForPhotoReference = (opts?: { maxheight?: number, maxwidth?:
return `${photosUrl}${photoref}${maxHeight}${maxWidth}`; return `${photosUrl}${photoref}${maxHeight}${maxWidth}`;
}; };
// const getPageResults = async ({ pageToken }: { pageToken: string }) => {
// const reqUrl = `${placesFromTextUrl}&pageToken=${pageToken}`;
// return (await fetch(reqUrl)).toJson();
// };
export const findNearbyPlaces = async ({ export const findNearbyPlaces = async ({
location, location,
search, search,
@ -65,16 +69,21 @@ export const findNearbyPlaces = async ({
coords: { latitude, longitude }, coords: { latitude, longitude },
} = location; } = location;
const input = `input=${encodeURIComponent(search || '*')}&inputtype=textquery`; const keyword = search ? `keyword=${encodeURIComponent(search)}` : '';
const loc = `locationbias=circle:${milesToMeters(radius)}@${latitude},${longitude}`; const loc = `location=${latitude},${longitude}`;
const fields = 'fields=id,name,geometry,photos,types,formatted_address,opening_hours,place_id'; const rad = `radius=${milesToMeters(radius)}`;
const reqUrl = `${placesFromTextUrl}&${input}&${loc}&${fields}`; const reqUrl = `${placesFromTextUrl}&${keyword}&${loc}&${rad}&type=restaurant`;
const { candidates, status }: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json(); const response: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json();
if (status !== 'OK') { // if (response.next_page_token) {
// const page = await getPageResults({ pageToken: response.next_page_token });
// return concat(response.results, page.results);
// }
if (response.status !== 'OK') {
throw new Error('google find places request failed'); throw new Error('google find places request failed');
} }
return filter(isEstablishment, candidates); return response.results;
}; };