Building a Serverless Store Locator on Google Cloud

Building a Serverless Store Locator on Google Cloud

Published on May 22, 2020 by Stanley Zheng

A common example is a store with multiple locations or multiple customer points wants to answer the question, what are the closest 5 stores to me? We hope to show a case example leveraging node and postgres.

This case example is based on this code example is Node.js findable on Github at For more cloud and serverless examples check our github.

The example is a typical node app along with an ETL process used to see the database. The technologies leveraged in this project includes

    • Google Maps API
    • Postgres and Postgis
    • Node

This project was deployed using Postgres 12 on CloudSQL on Google Cloud. The node endpoint, was hosted on a Google Cloud Function. After running node etl.js . This seeded the database with the data/storelocations.csv.

The endpoint for function closest accepts both address or zipcode. This is allowed by using the Google Maps Reverse geodecoder that is really robust at reversing any free-text into a geo long-lat point.

the in the function findviaaddress, the main business logic is found in the below. Taking the lat-long and connecting it to the points in the database.

let origin = st.geomFromText(SRID=4326;POINT(${} ${d.lng}_``);_

This function relies on the knex-postgis module. If you want to do it via raw SQL it is entirely possible.

SELECT   stop_id, 
         St_distance(St_pointfromtext('POINT(-73.985068 40.691330)', 4326)::geography, stop_loc::geography) distance
FROM     stops 
WHERE    parent_station IS NULL 
ORDER BY distance limit 10;
// Credit Andrew Chien 	

Writing applications below can be super simple! Let me know how your experience writing geo-spatial applications go. T