Skip to content

Commit 735a8f6

Browse files
committed
feat: Add origins route
1 parent 231e0fc commit 735a8f6

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The demo shows calling Google Maps Platform APIs from Google Cloud Functions.
88

99
- Cloud Functions (Node 10)
1010
- [Function Framework](https://github.com/GoogleCloudPlatform/functions-framework-nodejs) for local development.
11-
- [Node.js Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-js)
11+
- Google Maps Services ([Node.js Client](https://github.com/googlemaps/google-maps-services-js))
1212
- Place Details API
1313
- Places Photos API
1414
- Call Routes API

index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import {Request, Response} from 'express';
22

3-
import route1 from './src/route1';
4-
import route2 from './src/route2';
3+
import directions from './src/directions';
4+
import origins from './src/origins';
55

66
/**
77
* Entry point into the Functions Framework.
88
* @see https://github.com/GoogleCloudPlatform/functions-framework-nodejs
99
*/
1010
exports.function = (req: Request, res: Response) => {
1111
const paths = {
12-
'/foo': route1,
13-
'/bar': route2,
12+
'/directions': directions,
13+
'/origins': origins,
14+
// Default route (at the end)
15+
'/': () => res.send(Object.keys(paths)),
1416
};
1517
// Find the first route that matches
1618
for (const [path, route] of Object.entries(paths)) {
File renamed without changes.

src/origins.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {Request, Response} from 'express';
2+
3+
/**
4+
* Returns an array of lat/lng origin locations.
5+
* Simulates origin requests from many locations.
6+
*/
7+
export default (req: Request, res: Response) => {
8+
// Validate query parameter.
9+
const origin = req.query.origin;
10+
if (!origin) {
11+
return res.send({
12+
error: 'Error: `origin` query parameter required. Example: /origins?origin=1',
13+
});
14+
}
15+
16+
// Select a random place around the origin.
17+
const getDotAroundOrigin = () => {
18+
const place0 = { lat: 37.621491, lng: -122.378912 }; // SFO
19+
const place1 = { lat: 37.826837, lng: -122.498978 }; // Marin
20+
const place2 = { lat: 37.769548, lng: -122.486010 }; // GG Park
21+
const place3 = { lat: 37.795455, lng: -122.393306 }; // Ferry Building
22+
const place4 = { lat: 37.808171, lng: -122.270019 }; // Fox Theatres
23+
const place5 = { lat: 37.750565, lng: -122.203004 }; // Oracle Arena
24+
const place6 = { lat: 37.715740, lng: -122.219267 }; // OAK
25+
const place7 = { lat: 37.521822, lng: -121.924796 }; // Old Mission Park
26+
const place8 = { lat: 37.368574, lng: -121.927630 }; // SJC
27+
const place9 = { lat: 37.422051, lng: -122.084025 }; // Googleplex
28+
const places = [place0, place1, place2, place3, place4, place5, place6, place7, place8, place9];
29+
const place = places[+origin];
30+
31+
// Validate place
32+
if (!place) return res.send('Error: Invalid `origin` query parameter. Example: /origins?origin=1');
33+
34+
// Randomize the dot's location a bit
35+
const RANDOMNESS = 0.025;
36+
place.lat += ((Math.random() - 0.5) * RANDOMNESS);
37+
place.lng += ((Math.random() - 0.5) * RANDOMNESS);
38+
return place;
39+
};
40+
41+
// Select N dots
42+
const N = 10;
43+
const dots = [];
44+
for (let i = 0; i < N; ++i) {
45+
dots[i] = getDotAroundOrigin();
46+
}
47+
res.send(dots);
48+
};

src/route2.ts

-16
This file was deleted.

0 commit comments

Comments
 (0)