-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
47 lines (39 loc) · 1.41 KB
/
controller.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
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
geocoder = require('geocoder');
;
var controller = function () {
this.server = new Server('localhost', 27017, {auto_reconnect: true});
this.db = new Db('trouverdupain', this.server);
};
controller.prototype.getCollection = function (callback) {
this.db.collection('geoboulang_osm', callback);
};
controller.prototype.near = function (lat, lng, callback) {
this.getCollection(function (error, collection) {
function extracted(error, data) {
return callback(null, {center: {lat: parseFloat(lat), lng: parseFloat(lng)}, markers: data});
}
if (error) (callback(error));
else {
collection.find({"coordinates": {$near: [parseFloat(lng), parseFloat(lat)]}}, {type: false, _id: false, "properties.bie": false, "properties.tel": false}, {limit: 20}).toArray(extracted);
}
});
};
controller.prototype.geocode = function (addr, callback) {
var me = this;
if (!addr) {
callback('EMPTY');
} else {
geocoder.geocode(addr, function (err, data) {
if (data.status && data.status == 'OK') {
var location = data.results[0].geometry.location;
me.near(location.lat, location.lng, callback);
} else {
callback('NO_ADDR');
}
})
}
};
exports.controller = controller;