Empowering you to understand your world

Node.js Geocoding Example: Find Businesses Using Google Places

The following Node.js code sample is a simple program that accepts a zip code, fetches the coordinates for it, and then uses the coordinates to get a list of businesses within the radius specified (in metres) using the Google Places API and the help of the ‘Https’ and ‘follow-redirects’ libraries in Node.js.

Https was used because it is an SSL connection, and ‘follow-redirects’ was used to automatically follow redirects to the API’s respective SSL port number, as opposed to the standard port 80 used for non-SSL requests.

Before proceeding, install the ‘follow-redirects’ module in your project’s directory:

[username@localhost] ~/geocodetest $ npm install follow-redirects

This code sample operates by sending HTTP GET requests to the Google Places API, a service which you must sign up for in order to use. This geolocation API example searches for restaurants, as denoted by the use of ‘restaurant’ in the ‘keyword’ parameter. This is useful for travel apps intended to find businesses nearest to the user.

Change the highlighted areas marked [YOUR API KEY] to your Google Places API key (required for this to work) and ‘keyword’ to another type of business like ‘hotel’ and get a feel for how it works.

The ‘getCoordinates’ function (the first function to be executed in this app) takes a parameter ‘zipcode’ and sends in an HTTP GET request to the Google Places geocoding API. It is concatenated into the ‘path’ field, which is where the GET URL’s path is being constructed and stored.

‘host’, ‘path’, and ‘method’ are all fields in a JSON object that will be sent to Google’s API in the GET request. ‘method’ is the desired HTTP request method. There, we specify that we want to send a ‘GET’ request. Therefore we are passing two parameters to ‘https.request’ as follows:

https.request({JSON Object}, callbackfunction});

The callback function in this case is ‘CoordinateResponse’. If you guessed that CoordinateResponse is executed when Google responds with the coordinates, you’re correct! CoordinateResponse then parses a JSON object sent by Google that contains the coordinates we need, and finally passes them onto (and executes) the ‘placeSearch’ function.

The placeSearch function then sends the coordinates and the radius you specified via a GET request to the Google Places Nearby Search API, which uses those coordinates to find places within the radius (specified in meters) of those coordinates. The &type parameter of our GET request can be set to any type of place you wish. For this example, I set it to ‘restaurant’.

The placeResponse function (passed as a callback to our ‘http.request’ function in placeSearch) then executes and parses/prints the response from the Google Nearby Places API, which should contain that list of places we’ve been longing for. It will print the places to the screen in a human readable format with the help of ‘JSON.parse’.

var https = require('follow-redirects').https;

var placeDetails = function() {
	this.places = [];
}

//Step 1: Get coordinates based on the entered zipcode.

function getCoordinates(zipcode) {
	https.request({
		host: 'maps.googleapis.com',
		path: '/maps/api/geocode/json?address=' + zipcode + '&key=[YOUR API KEY]',
		method: 'GET'},
		CoordinateResponse).end();
}

//Step 2: Find places within the specified radius, based on the coordinates provided by the getCoordinates function.

function placeSearch(latitude, longitude, radius) {
	https.request({
		host: 'maps.googleapis.com',
		path: '/maps/api/place/nearbysearch/json?location=' + latitude + ',' + longitude + '&radius=' + radius + '&type=restaurant&key=[YOUR API KEY]',
		method: 'GET'},
		PlaceResponse).end();
}

function CoordinateResponse(response) {
	var data = "";
	var sdata = "";
	var latitude = "";
	var longitude = "";

	response.on('data', function(chunk) {
		data += chunk;
	});
	response.on('end', function() {
		sdata = JSON.parse(data);
		latitude = sdata.results[0].geometry.viewport.northeast.lat;
		longitude = sdata.results[0].geometry.viewport.northeast.lng;
		placeSearch(latitude, longitude, 50000);
	});
}

function PlaceResponse(response) {
	var p;
	var data = "";
	var sdata = "";
	var PD = new placeDetails();

	response.on('data', function(chunk) {
		data += chunk;
	});
	response.on('end', function() {
		sdata = JSON.parse(data);
		if (sdata.status === 'OK') {
			console.log('Status: ' + sdata.status);
			console.log('Results: ' + sdata.results.length);
			for (p = 0; p < sdata.results.length; p++) {
				PD.places.push(sdata.results[p]);
			}
			for (r = 0; r < sdata.results.length; r++) {
				console.log('----------------------------------------------');
				console.log(PD.places[r].name);
				console.log('Place ID (for Place Detail search on Google):' + PD.places[r].place_id);
				console.log('Rating: ' + PD.places[r].rating);
				console.log('Vicinity: ' + PD.places[r].vicinity);
			}
		} else {
			console.log(sdata.status);
		}
	});
}

getCoordinates(37202); //Enter a zip code here to try it out (Nashville in this case)

Google Places Geocoding API Documentation

Google Place Search API Documentation

The program above should print something like this to the screen (most results omitted to avoid cluttering the page):

[username@localhost] ~/geocodetest $ node geo.js
Status: OK
Results: 20
----------------------------------------------
Ruth's Chris Steak House
Place ID (for Place Detail search on Google):ChIJSf7y-pVmZIgRnMUdIudF0DI
Rating: 4.3
Vicinity: 2100 West End Avenue, Nashville
----------------------------------------------
SUBWAY®Restaurants
Place ID (for Place Detail search on Google):ChIJx3zZ-a9mZIgR212C4bwy9bQ
Rating: 3.9
Vicinity: 2817 West End Avenue, Nashville
----------------------------------------------
Loveless Cafe
Place ID (for Place Detail search on Google):ChIJGVuc4xGGZIgR7fI2E4yqTpU
Rating: 4.6
Vicinity: 8400 Tennessee 100, Nashville
----------------------------------------------
Burger Up
Place ID (for Place Detail search on Google):ChIJ6Qp7PDBkZIgRis06sOe6NvY
Rating: 4.5
Vicinity: 2901 12th Avenue South, Nashville
----------------------------------------------
Caney Fork River Valley Grille
Place ID (for Place Detail search on Google):ChIJIei6-NVpZIgRjqrdFMGxTMU
Rating: 4.2
Vicinity: 2400 Music Valley Dr, Nashville
----------------------------------------------

Successfully tested on Node.js versions:

  • 8.10.0.

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published