-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeocoder-yahoo.js
67 lines (58 loc) · 1.56 KB
/
geocoder-yahoo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*jslint node: true, indent: 2, sloppy: true, white: true, vars: true */
/*
* DEPRECATED
* Use geocoder-yboss instead.
*
*
* Geocode functionality using the Yahoo Placefinder geocoder
*/
var request = require('request');
var url = require('url');
var Q = require('q');
var apiUrl = 'http://where.yahooapis.com/geocode';
var appId = process.env.YDN_KEY || '';
var start = 0;
var count = 1;
var flags = 'J';
module.exports = (function () {
var self = {};
self.code = function (line1, line2) {
var urlObj = url.parse(apiUrl);
urlObj.query = {
appid: appId,
start: start.toString(),
count: count.toString(),
flags: flags,
line1: line1,
line2: line2
};
var def = Q.defer();
request.get(url.format(urlObj), function(error, resp, body) {
if (error || resp.statusCode !== 200) {
def.reject(error || new Error('Received status: ' + resp.statusCode));
return;
}
try {
var data = JSON.parse(body);
var coords = {
lat: data.ResultSet.Results[0].latitude,
lon: data.ResultSet.Results[0].longitude,
meta: {
quality: data.ResultSet.Results[0].quality,
service: 'Yahoo',
line2: line2
}
};
def.resolve(coords);
} catch (e) {
console.log('Caught exception getting data from Yahoo Placefinder:');
console.log(e.message);
console.log('Yahoo response body:');
console.log(body);
def.reject(e);
}
});
return def.promise;
};
return self;
}());