From 28cfd7fc0afae40ad8028be2d1fe7c2fa3724330 Mon Sep 17 00:00:00 2001 From: bartronx7 Date: Sun, 8 Jul 2018 18:59:47 -0500 Subject: [PATCH] use nearbysearch instead for getting just restaurants --- js/apis/GooglePlacesApi.js | 39 +++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/js/apis/GooglePlacesApi.js b/js/apis/GooglePlacesApi.js index 60d5950..a37afbb 100644 --- a/js/apis/GooglePlacesApi.js +++ b/js/apis/GooglePlacesApi.js @@ -1,22 +1,21 @@ // @flow import { type GooglePlaceObj } from '../records/PlaceRecord'; 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 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 GoogleFindPlaceResponse = { status: string, candidates: Array }; +type GoogleFindPlaceResponse = { + status: string, + results: Array, + next_page_token?: string, +}; 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 => { if (!placeid || typeof placeid !== 'string') { throw new Error('placeid looks wrong'); @@ -48,6 +47,11 @@ export const getURLForPhotoReference = (opts?: { maxheight?: number, 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 ({ location, search, @@ -65,16 +69,21 @@ export const findNearbyPlaces = async ({ coords: { latitude, longitude }, } = location; - const input = `input=${encodeURIComponent(search || '*')}&inputtype=textquery`; - const loc = `locationbias=circle:${milesToMeters(radius)}@${latitude},${longitude}`; - const fields = 'fields=id,name,geometry,photos,types,formatted_address,opening_hours,place_id'; - const reqUrl = `${placesFromTextUrl}&${input}&${loc}&${fields}`; + const keyword = search ? `keyword=${encodeURIComponent(search)}` : ''; + const loc = `location=${latitude},${longitude}`; + const rad = `radius=${milesToMeters(radius)}`; + 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'); } - return filter(isEstablishment, candidates); + return response.results; };