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.

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.

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]$ 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
----------------------------------------------

Facebook Comments

comments