mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 07:54:57 -06:00
use nearbysearch instead for getting just restaurants
This commit is contained in:
parent
e294a4a866
commit
28cfd7fc0a
1 changed files with 24 additions and 15 deletions
|
|
@ -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<GooglePlaceObj> };
|
||||
type GoogleFindPlaceResponse = {
|
||||
status: string,
|
||||
results: Array<GooglePlaceObj>,
|
||||
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<GooglePlaceObj> => {
|
||||
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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue