diff --git a/CHANGELOG.md b/CHANGELOG.md index e7f34a58..f9e14557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ way to update this template, but currently, we follow a pattern: --- ## Upcoming version 2018-08-XX +* [change] Add support for default locations in the + LocationAutocompleteInput component. Common searches can be + configured to show when the input has focus. This reduces typing and + Google Places geolocation API usage. The defaults can be configured + in `src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js`. + [#894](https://github.com/sharetribe/flex-template-web/pull/894) * [change] Removed the `country` parameter from the search page as it was not used anywhere. [#893](https://github.com/sharetribe/flex-template-web/pull/893) diff --git a/docs/google-maps.md b/docs/google-maps.md index 01c91d96..4fb336cc 100644 --- a/docs/google-maps.md +++ b/docs/google-maps.md @@ -25,3 +25,14 @@ local development, you can add the variable in the Gitignored `.env` file in the ``` REACT_APP_GOOGLE_MAPS_API_KEY=my-key-here ``` + +## Setup common locations to reduce typing + +The location autocomplete input in the landing page and the topbar can +be configured to have specific locations shown by default when the +user focuses on the input and hasn't yet typed in any searches. This +reduces the typing required for common searches, and also reduces the +need to use the Google Places geolocation API that much. + +The default locations can be configured in +[src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js](../src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js). diff --git a/src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js b/src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js index b8942cd6..d4bba93d 100644 --- a/src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js +++ b/src/components/LocationAutocompleteInput/GeocoderGoogleMaps.js @@ -3,6 +3,30 @@ import { getPlacePredictions, getPlaceDetails } from '../../util/googleMaps'; import css from './LocationAutocompleteInput.css'; +// A list of default predictions that can be shown when the user +// focuses on the autocomplete input without typing a search. This can +// be used to reduce typing and Geocoding API calls for common +// searches. +// +// Example: +// +// [ +// { +// place_id: 'Place ID from Google Maps Places API', +// description: 'Place name to show in the autocomplete dropdown', +// }, +// ] +// +// To know which values to set as defaults, log a real prediction +// object from the Places API call and copy the Place ID from the +// response. +export const defaultPredictions = [ + // { + // place_id: 'ChIJkQYhlscLkkYRY_fiO4S9Ts0', + // description: 'Helsinki, Finland', + // }, +]; + // When displaying data from the Google Maps Places API, and // attribution is required next to the results. // See: https://developers.google.com/places/web-service/policies#powered @@ -44,6 +68,13 @@ class GeocoderGoogleMaps { }); } + /** + * Get the ID of the given prediction. + */ + getPredictionId(prediction) { + return prediction.place_id; + } + /** * Get the address text of the given prediction. */ diff --git a/src/components/LocationAutocompleteInput/GeocoderMapbox.js b/src/components/LocationAutocompleteInput/GeocoderMapbox.js index b5d0495a..471fa18a 100644 --- a/src/components/LocationAutocompleteInput/GeocoderMapbox.js +++ b/src/components/LocationAutocompleteInput/GeocoderMapbox.js @@ -3,6 +3,8 @@ import config from '../../config'; const { LatLng: SDKLatLng, LatLngBounds: SDKLatLngBounds } = sdkTypes; +const placeId = prediction => prediction.id; + const placeAddress = prediction => prediction.place_name; const placeOrigin = prediction => { @@ -24,6 +26,34 @@ const placeBounds = prediction => { return null; }; +// A list of default predictions that can be shown when the user +// focuses on the autocomplete input without typing a search. This can +// be used to reduce typing and Geocoding API calls for common +// searches. +// +// Example: +// +// [ +// { +// id: 'any unique id', +// place_name: 'Place name to show in the autocomplete dropdown', +// center: [longitude, latitude], +// bbox: [minX, minY, maxX, maxY], +// }, +// ] +// +// To know which values to set as defaults, log a real prediction +// object from the Geocoding API call and check the values from the +// response. +export const defaultPredictions = [ + // { + // id: 'default-helsinki', + // place_name: 'Helsinki, Finland', + // center: [24.94861, 60.17333], + // bbox: [24.82617, 60.075361, 25.313112, 60.297839], + // }, +]; + export const GeocoderAttribution = () => null; /** @@ -66,6 +96,13 @@ class GeocoderMapbox { }); } + /** + * Get the ID of the given prediction. + */ + getPredictionId(prediction) { + return placeId(prediction); + } + /** * Get the address text of the given prediction. */ diff --git a/src/components/LocationAutocompleteInput/LocationAutocompleteInputImpl.js b/src/components/LocationAutocompleteInput/LocationAutocompleteInputImpl.js index 930e00e5..816be92e 100644 --- a/src/components/LocationAutocompleteInput/LocationAutocompleteInputImpl.js +++ b/src/components/LocationAutocompleteInput/LocationAutocompleteInputImpl.js @@ -3,7 +3,8 @@ import { any, arrayOf, bool, func, number, shape, string, oneOfType, object } fr import classNames from 'classnames'; import debounce from 'lodash/debounce'; import { propTypes } from '../../util/types'; -import Geocoder, { GeocoderAttribution } from './GeocoderGoogleMaps'; +import Geocoder, { GeocoderAttribution, defaultPredictions } from './GeocoderGoogleMaps'; +// import Geocoder, { GeocoderAttribution, defaultPredictions } from './GeocoderMapbox'; import css from './LocationAutocompleteInput.css'; @@ -69,12 +70,12 @@ const LocationPredictionsList = props => { return (