-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (53 loc) · 1.49 KB
/
index.js
File metadata and controls
60 lines (53 loc) · 1.49 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
/**
* Simple client for geocoding and reverse
* geocoding using the Open Street Map API
* @author Matt Null -- hello@mattnull.com
* http://github.com/mattnull
*/
var request = require('request');
var OSMGeocoder = {
geocode : function(address, callback){
callback = callback || function(){};
if(!address){
callback('Please provide an address.', false);
return;
}
address = address.replace(/ /g, '+');
request({
url : 'http://nominatim.openstreetmap.org/search?format=json&q='+address+'&addressdetails=1',
headers : {
'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36'
}
}, function (err, res, body) {
if(!err && body){
body = JSON.parse(body)
}
else if(err){
console.log(err);
}
callback(err, body);
});
},
reverse : function(lat, lon, callback){
callback = callback || function(){};
if(!lat || !lon){
callback('Please provide lat and lon.', false);
return;
}
request({
url : 'http://nominatim.openstreetmap.org/reverse?format=json&lat='+lat+'&lon='+lon+'&addressdetails=1',
headers : {
'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36'
}
}, function (err, res, body) {
if(!err && body){
body = JSON.parse(body);
}
else if(err){
console.log(err);
}
callback(err, body);
});
}
};
module.exports = OSMGeocoder;