This module orchestrates API requests to Google Maps Platform.
To use, do the following:
-
Generate an API key from a GCP account with the Places API enabled.
-
Run
npm install
. -
Require and instantiate the module, pasting your API Key:
const GMP = require('./gmpclient'); const client = new GMP('YOUR_API_KEY');
-
Pre-populate the high RPS cache:
Since it takes a few API calls to pre-populate the cache, you should call
client.populateCache()
right after the client is instantiated so there's no waiting later on:await client.populateCache();
-
Call the API:
let initialOriginsCollection = getOrigins(false); let initialOrigins = initialOriginsCollection.features; for (let i = 0; i < initialOrigins.length; i++) { let originID = initialOrigins[i].properties.place_id; let originDetails = getPlaceDetails(originId;) if (originDetails.photos) { let firstPhotoRef = originDetails.photos[0].photo_reference; } let originCoord = initialOrigins[i].geometry.coordinates; let destinationCoord = [37.4268384,-122.082891]; let originRoutes = getDirections(originCoord, destinationCoord); }
-
getPlaceDetails
: Fetches the full details about a place.- Parameter:
placeId
{String} (Required) A Google Maps Place ID - Return: {JSON} Object in the format described in Places Details Responses
- Parameter:
-
getPlacesPhoto
: Serves a photo's binary file- Parameters :
-
photoref
{String} (Required) A string identifier that uniquely identifies a photo. Photo references are returned from a Place Details request. -
maxwidth
ormaxheight
{Integer} (At least one of these required) Specifies the maximum desired height or width, in pixels, of the image returned by the Place Photos service. If the image is smaller than the values specified, the original image will be returned. If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio. Both the maxheight and maxwidth properties accept an integer between 1 and 1600.
-
- Return : {Image file} The response of a successful Place Photo request will be an image. The type of the image will depend upon the type of the originally submitted photo. More details at Place Photo Response
- Parameters :
-
getDirections
: Generates driving directions between two points- Parameters :
-
origin
{Array} (Required) A list of a latitude and longitude in decimal format -
destination
{Array} (Required) A list of latitude and longitude in decimal format -
mode
{String} (Required) The mode of transit. 'driving', 'walking', 'bicycling', 'transit' are supported.
-
- Return : {JSON} Object in the format described in Directions Sample Responses
- Parameters :
-
getOrigins
: Provides 10 origins in the SF bay area at a time- Parameters :
-
scaled
{Boolean} (Required) Indicates whether we want origins in interactive mode (10 pre-set origins for displaying directions) or in scaled mode (10 random origins from around the bay area for simulating a large number of simultaneous requests)
-
- Return : {GeoJSON} A GeoJSON FeatureCollection object in the format described in Load GeoJSON though the feature types are Points instead of Polygons. An example featuring a FeatureCollection of Points is used in the Visualizing Data Tutorial.
- Parameters :
-
populateCache
: Pre-populates the highRPS cache. You should call this on startup.