Skip to content
This repository has been archived by the owner on Oct 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from pawerda/master
Browse files Browse the repository at this point in the history
PlaceAutocomplete
  • Loading branch information
sri-rang committed May 5, 2015
2 parents fe40630 + 47d69a4 commit 78a3c06
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -5,6 +5,7 @@
var RadarSearch = require("./lib/RadarSearch.js");
var TextSearch = require("./lib/TextSearch.js");
var PlaceDetailsRequest = require("./lib/PlaceDetailsRequest.js");
var PlaceAutocomplete = require("./lib/PlaceAutocomplete.js");
var AddEvent = require("./lib/AddEvent.js");
var DeleteEvent = require("./lib/DeleteEvent.js");
var EventDetails = require("./lib/EventDetails.js");
Expand All @@ -16,11 +17,14 @@
radarSearch: new RadarSearch(apiKey, outputFormat),
textSearch: new TextSearch(apiKey, outputFormat),
placeDetailsRequest: new PlaceDetailsRequest(apiKey, outputFormat),
placeAutocomplete: new PlaceAutocomplete(apiKey, outputFormat),
addEvent: new AddEvent(apiKey, outputFormat),
deleteEvent: new DeleteEvent(apiKey, outputFormat),
eventDetails: new EventDetails(apiKey, outputFormat),
imageFetch: new ImageFetch(apiKey)

};
};

})();

32 changes: 32 additions & 0 deletions lib/PlaceAutocomplete.js
@@ -0,0 +1,32 @@
(function () {
"use strict";

var querystring = require("querystring");
var https = require("https");

var HttpResponseProcessor = require("./HttpResponseProcessor.js");
var validate = require("./validate.js");

module.exports = function (apiKey, outputFormat) {
return function (parameters, callback) {
validate.apiKey(apiKey);
validate.outputFormat(outputFormat);
//Required parameters
parameters.key = apiKey;
parameters.input = parameters.input || 'sydney lyr';

if (typeof parameters.location === "object") parameters.location = parameters.location.toString();

var options = {
hostname: "maps.googleapis.com",
path: "/maps/api/place/autocomplete/" + outputFormat + "?" + querystring.stringify(parameters)
};
var request = https.request(options, new HttpResponseProcessor(outputFormat === "json", callback));
request.on("error", function (error) {
callback(new Error(error));
});
request.end();
};
};

})();
20 changes: 20 additions & 0 deletions readme.md
Expand Up @@ -87,6 +87,24 @@ Yes, fork, hack and send me a PR
});
});

### place autocomplete

var GooglePlaces = require("googleplaces");
var googlePlaces = new GooglePlaces(process.env.GOOGLE_PLACES_API_KEY, process.env.GOOGLE_PLACES_OUTPUT_FORMAT);
var parameters;

/**
* Place autocomplete - https://developers.google.com/places/webservice/autocomplete
*/
parameters = {
input: "world trade "

};
googlePlaces.textSearch(parameters, function (error, response) {
if (error) throw error;
console.log(response.predictions);
});

### text search

var GooglePlaces = require("googleplaces");
Expand Down Expand Up @@ -119,3 +137,5 @@ Yes, fork, hack and send me a PR
if (error) throw error;
console.log(response.results);
});


19 changes: 19 additions & 0 deletions test/PlaceAutocompleteTest.js
@@ -0,0 +1,19 @@
(function () {
"use strict";

var assert = require("assert");

var PlaceAutocomplete = require("../lib/PlaceAutocomplete.js");
var config = require("./config.js");

var placeAutocomplete = new PlaceAutocomplete(config.apiKey, config.outputFormat);

var parameters = {
input: 'sydney lyr'
};
placeAutocomplete(parameters, function (error, response) {
if (error) throw error;
assert.equal(response.status, "OK", "Place autocomplete request response status is OK");
});

})();

0 comments on commit 78a3c06

Please sign in to comment.