address-to-geocode
Advanced tools
Comparing version
49
index.js
// Dependnecies | ||
var Geocoder = require("node-geocoder").getGeocoder("google"); | ||
var myLocation = "New York"; | ||
// Get location | ||
Geocoder.geocode({ | ||
address: myLocation | ||
}, function(err, guess) { | ||
if (err) { throw err; } | ||
/** | ||
* AddressToGeocode | ||
* Converts a string representing an address to geocode data. | ||
* | ||
* @name AddressToGeocode | ||
* @function | ||
* @param {String} address The searched location | ||
* @param {Function} callback The callback function | ||
* @return {Object} AddressToGeocode function | ||
*/ | ||
var AddressToGeocode = function (address, callback) { | ||
Geocoder.geocode({ | ||
address: address | ||
}, function(err, guess) { | ||
if (err) { return callback(err); } | ||
callback(null, AddressToGeocode.handleLocation(guess)); | ||
}); | ||
return AddressToGeocode; | ||
}; | ||
/** | ||
* handleLocation | ||
* | ||
* @name handleLocation | ||
* @function | ||
* @param {Array} guess Locations returned by Geocoder | ||
* @return {Object} An object containing `lat` and `lng` fields. | ||
*/ | ||
AddressToGeocode.handleLocation = function (guess) { | ||
if (!guess || !guess.length) { | ||
return console.error("No location found"); | ||
return {}; | ||
} | ||
return { | ||
lat: guess[0].latitude, | ||
lng: guess[0].longitude | ||
}; | ||
}; | ||
console.log(myLocation + ": (" + guess[0].latitude + ", " + guess[0].longitude + ")"); | ||
console.log("----"); | ||
console.log("Results:", guess); | ||
}); | ||
module.exports = AddressToGeocode; |
{ | ||
"name": "address-to-geocode", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A test using node-geocode module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node index" | ||
"test": "node test/index" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -13,2 +13,40 @@ # address-to-geocode | ||
## Example | ||
```js | ||
// My Location | ||
var myLocation = "New York"; | ||
// Handler to output data | ||
var handler = function (err, location) { | ||
if (err) { throw err; } | ||
console.log(location); | ||
}; | ||
require("../index") | ||
(myLocation, handler) | ||
("Paris", handler) | ||
; | ||
``` | ||
## Documentation | ||
### `AddressToGeocode(address, callback)` | ||
Converts a string representing an address to geocode data. | ||
#### Params: | ||
* **String** *address* The searched location | ||
* **Function** *callback* The callback function | ||
#### Return: | ||
* **Object** AddressToGeocode function | ||
### `AddressToGeocode.handleLocation(guess)` | ||
A function that handles the location. This can be overriden creating your own handler. | ||
#### Params: | ||
* **Array** *guess* Locations returned by Geocoder | ||
#### Return: | ||
* **Object** An object containing `lat` and `lng` fields. | ||
## How to contribute | ||
@@ -15,0 +53,0 @@ |
4580
56.53%6
20%50
233.33%64
146.15%