If you are thinking about using Google Cloud functions (and you should), you may be wondering how to use things like Google Maps in the context of cloud functions. Well, cloud functions allow you to run Node.js without the need for the server, so that means that anything you can create in Node, you can run as a cloud function.
Of course Node doesn’t have a DOM, so that means that the usual versions of the MAPS Api don’t work – but there is a little known Node version of Maps available too, that allow you to do geocoding and so on – in fact most of the things you’d probably want to do in a sever like environment. Some of these are available in Apps Script too of course, but if you want to save your quotas, or integrate with other Node functions, you can use Cloud functions. 
The code for this function is on github


You’ll get that here, and you can install it with NPM. In this example I’m going to generate random places within a polygon with represents a given area. There are some functions in MAPS that allow you figure out if a point is within a polygon, but not in the Node version, so I need to use something else – in this case I’ll be using point-in-polygon. Once I’ve installed these into Node, we’re good to go

but I’ll also install the cloud functions emulator which allows me to test everything locally before deploying the cloud function formally.

Ephemeral Exchange

This cloud function is actually a microservice in an ecosystem, which gets its instructions and publishes its results by subscribing to an item in a cross platform cache. Now and again it will be asked to generate a random place, and it will respond by publishing the updated item. Other services in the ecosystem will be listening for that change too and will do things with each of the places generated. You can get the node client for that here if you want.
I won’t go into all the code here but will publish the entire thing at a later time. For now, I’m just going to go into the main points. 

Generating the polygon

I took this from a public GeoJson file which contains amongst other things, a list of points that describe the polygon.

I convert these and write them to cache to be shared by all co-operating processes in the ecosystem

One of the processes wakes up and maps this area when it is detected, like this

Generating a random place

The way I’m approaching this is to generate a random longitude and latitude with the area by first getting the most northeasterly and south-westerly points of the polygon, and randomly generating points within those limits

and keep eliminating edge points that fall outside the polygon.

ns.inside uses point-in-polygon


which uses an array for lat/lng rather than a literal so I need to convert

Eventually, then, I find a point that is wholly inside the polygon, and now I need to find a place close by. I’m actually looking for ‘bars’, but it could be any other type of place too. This is where we use the Node version of the Maps API, so first, we need to create a client.

Now I can get a random point, and find a nearby bar. I have to run it through ns.inside again because the place may be close to the given point, but may actually be outside the polygon. Since the function returns them in order of closeness, I stop when I’ve found a place still inside the polygon.

And that’s it, all that remains is to write the thing to cache, and anybody who cares will have a new place added. Here’s the entire cloud function. 

For more like this, see React, redux, redis, material-UI and firebase.