|
| 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 | +}; |
0 commit comments