-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_api.js
More file actions
65 lines (63 loc) · 2.54 KB
/
map_api.js
File metadata and controls
65 lines (63 loc) · 2.54 KB
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
'use strict';
var http = require('http'),
EventEmitter = require('events').EventEmitter,
Proto = new EventEmitter(),
i,
delay = 1000, /*introduce some interval to avoid OVER_QUERY_LIMIT*/
getAddressString = function (lookup) {
var address = {},
component,
components = lookup.results[0].address_components,
components_len = components.length;
for (i = 0; i < components_len; i += 1) {
component = components[i];
if (component.types.indexOf('route') !== -1) {address.street = component.long_name; }
if (component.types.indexOf('street_number') !== -1) {address.number = component.long_name; }
if (component.types.indexOf('locality') !== -1) {address.locality = component.long_name; }
}
//return address string replacing undefined with empty string
return (address.street === undefined ? '' : address.street)
+ (address.number === undefined ? '' : ' ' + address.number)
+ (address.locality === undefined ? '' : ', ' + address.locality);
},
addressLookup = function (temp) {
var lat = temp.coords.lat,
lng = temp.coords.lng,
options = {
hostname: 'maps.googleapis.com',
port: 80,
path: '/maps/api/geocode/json?latlng=' + lat + ',' + lng
+ '&sensor=false',
method: 'GET'
},
data = '',
request = http.request(options, function (res) {
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
var response = JSON.parse(data.toString());
switch (response.status) {
case 'OK':
temp.coords.address = getAddressString(response);
Proto.emit('got-address', temp); /* return address */
console.error('[google]'.grey, response.status.green, temp.coords.address);
return;
case 'ZERO_RESULTS':
Proto.emit('got-address', ''); /* return empty string */
return;
default:
setTimeout(function () {
Proto.emit('lookup-address', temp); /* retry */
}, delay);
console.error('[google]'.grey, response.status.red, 'retry in', delay);
return;
}
});
});
console.log('[google]'.grey, (options.hostname + options.path).grey);
request.on('error', function (err) {console.log('[google]'.grey, err.red); });
request.end();
};
Proto.on('lookup-address', addressLookup);
module.exports = Proto;