A simple factory using google places API to fetch predictions for places and get place details.
npm i -S google-places-autocomplete-service
Then add the Google Places API script to your page:
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>First add the dependency to your app:
import googlePlacesService from 'google-places-autocomplete-service';
Then initialize the service:
this.googlePlaces = googlePlacesService(options);
Then get predictions:
this.googlePlaces.getPredictions('example text', callback);
Then get place info:
this.googlePlaces.getPlace(predictionObject, callback);
The service accepts some options on initialization, for example:
this.googlePlaces = googlePlacesService({
type: 'geocode',
filterByCountry: 'US',
outputPlaceTypes: [
'postalCode',
'locality',
'administrativeAreaLevel1',
'country'
],
searchStrategies: [
'searchByPlaceId'
]
});
This option will be used in getPredictions
to filter predictions results - if none is passed geocode
will be used as a default.
- geocode
- address
- establishment
- (regions)
- (cities)
This option will be used in getPredictions
to restrict the predictions results to a country.
Accepts uppercase ISO Alpha-2 country code - if none is passed then it will output global predictions.
This option will be used in getPlace
to make sure it will return a place with those place info types included.
- streetAddress
- route
- streetNumber
- neighborhood
- postalCode
- sublocality
- locality
- administrativeAreaLevel1
- administrativeAreaLevel2
- administrativeAreaLevel3
- administrativeAreaLevel4
- administrativeAreaLevel5
- country
This option will be used in getPlace
and will search for the specified place with the strategies passed - if none is passed then it will use all the strategies needed.
longitude
andlatitude
are optional, but required if use onlysearchWithGeocoder
and nothing else.
Retrieves place autocomplete predictions based on the supplied arguments.
Return value: None
input
: string - The user entered input string.callback
: function - What to do with the results i.e:(predictions) => console.log(predictions)
.regex
: regexp - (optional) A regex to filter out predictions.
The predictions object passing to the callback, is an object of placeIds as keys and the corresponding info of the prediction. Like that:
{
ChIJOwg_06VPwokRYv534QaPC8g: {
body: "New York, NY, United States"
terms: [
"New York",
"NY",
"United States"
],
type: "locality"
},
...
}
Retrieves details about the place.
Return value: None
prediction
: object - the prediction :)placeId
: string - the prediction's place ID.type
: string - prediction's first of types.terms
: array - prediction's string tokens.body
: string - prediction's body.
callback
: function - What to do with the result i.e:(placeInfo) => console.log(placeInfo)
.
The placeInfo passing to the callback, is an object with place's info like coords, prediction terms that couldn't find, status and of course all the place types that could find. For example:
{
coords: "40.6781784, -73.9441579",
sublocality: "Brooklyn",
locality: "New York",
administrativeAreaLevel1: "New York",
administrativeAreaLevel1Code: "NY",
country: "United States",
notValid: [],
status: "SUCCESS"
}
Create a ticket here
Issue a pull request.