Skip to content

Instantly share code, notes, and snippets.

@marlonlom
Last active January 26, 2018 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlonlom/9bb1da21e86247909024d91ef0b65f8f to your computer and use it in GitHub Desktop.
Save marlonlom/9bb1da21e86247909024d91ef0b65f8f to your computer and use it in GitHub Desktop.
Google staticmaps Router implementation. Used for map images downloading using Nodejs + ExpressJS
/**
* Google staticmaps Router implementation.
*/
const express = require('express'),
request = require('request'),
router = express.Router(),
StaticMaps = {
baseHost: "https://maps.googleapis.com/maps/api"
};
/**
* Download google staticmaps image.
*/
router.get('/staticmap', (req, resp) => {
let map_position = req.query.center,
marker_label = req.query.marker_label,
image_url = StaticMaps.baseHost
.concat('/staticmap?center=#MAP_LOCATION&zoom=16&scale=1&size=260x200&maptype=roadmap&format=jpg&visual_refresh=true&markers=size:small%7Ccolor:0xff0000%7Clabel:#MARKER_LABEL')
.replace('#MARKER_LABEL', encodeURIComponent(marker_label))
.replace('#MAP_LOCATION', map_position);
request(image_url).pipe(resp);
});
module.exports = router;
@marlonlom
Copy link
Author

Available request url parameters:

  • center = location (text as "latitude,longitude")
  • marker_label=marker title

Usage:

Adds as a router to ExpressJS Server implementation.

const express = require('express');
const app = express();
const port = process.env.PORT || 8090;

...

app.use('/', require('./google_staticmaps'));
...

Then, in a web browser:

<server_url>/staticmap?center=#center&marker_label=#marker_label

Feel free to use and extend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment