| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- exports = module.exports = {
- search: search,
- details: details
- };
- var request = require('request'),
- yelp = require('yelp-fusion');
- var YELP_CLIENTID = 'VMEhsiPTvZni4eGAFSaPeA';
- var YELP_APIKEY = 'uN7lv_ywa-5osEguCmaGeE8vnGKZezJO3c2-ADLd1ySCPcGTWiK71XwdgvGNeavvw04Yc84rHaJzkSAhvNmFubQ5zyMAsdR0gCuLpVFhqHXTshl67BdbDO6WYSYlXHYx';
- const yelpClient = yelp.client(YELP_APIKEY);
- const HEADERS = {
- 'User-Agent': 'Cloudron Maps Application'
- };
- function search(req, res, next) {
- console.log('Search query:', req.query);
- const queryString = req.query.search;
- const lat = req.query.lat;
- const lon = req.query.lon;
- function amendDetails(results) {
- results.forEach(function (r) {
- // icons
- r.icon = 'fa-address-card';
- if (r.type === 'fast_food' || r.type === 'restaurant') r.icon = 'fa-utensils';
- if (r.type === 'doctors') r.icon = 'fa-user-md';
- if (r.type === 'beach') r.icon = 'fa-umbrella-beach';
- if (r.type === 'dentist') r.icon = 'fa-tooth';
- if (r.type === 'car') r.icon = 'fa-car';
- if (r.type === 'playground') r.icon = 'fa-child';
- if (r.type === 'recreation_ground') r.icon = 'fa-running';
- if (r.type === 'sports_centre' || r.type === 'pitch') r.icon = 'fa-futbol';
- if (r.type === 'bicycle-rental') r.icon = 'fa-bicycle';
- if (r.type === 'school') r.icon = 'fa-graduation-cap';
- if (r.type === 'post_box' || r.type === 'post_office') r.icon = 'fa-envelope-square';
- if (r.type === 'canal' || r.type === 'river' || r.type === 'water') r.icon = 'fa-water';
- if (r.type === 'station' || r.type === 'stop') r.icon = 'fa-subway';
- if (r.type === 'attraction') r.icon = 'fa-monument';
- if (r.type === 'city' || r.type === 'village' || r.type === 'administrative') r.icon = 'fa-city';
- if (r.type === 'administrative' || r.class === 'office') r.icon = 'fa-building';
- if (r.type === 'pub' || r.type === 'bar') r.icon = 'fa-beer';
- if (r.type === 'hotel' || r.type === 'hostel') r.icon = 'fa-bed';
- if (r.type === 'supermarket' || r.type === 'retail') r.icon = 'fa-shopping-cart';
- // ensure we have some city if only town is mentioned
- if (!r.address.city) r.address.city = r.address.town;
- // name
- r.name = r.display_name.split(',')[0];
- if (r.class === 'place' || r.class === 'building') r.name = r.address.road + ' ' + r.address.house_number;
- if (r.type === 'city') r.name = r.address.city;
- if (!r.name) r.name = r.display_name;
- });
- return results;
- }
- function removeDuplicates(results) {
- var tmp = [];
- results.forEach(function (a) {
- if (tmp.find(function (b) { return b.place_id === a.place_id; })) return;
- tmp.push(a);
- });
- return tmp;
- }
- function query(detail, callback) {
- var params = {
- format: 'json',
- limit: 20,
- addressdetails: 1,
- q: (detail && queryString.indexOf(detail) === -1) ? (queryString + ' , ' + detail) : queryString
- };
- request.get('https://nominatim.openstreetmap.org/search.php', { qs: params, headers: HEADERS }, function (error, response, body) {
- if (error) return console.error(error);
- return callback(JSON.parse(body));
- });
- }
- function noLocationSearch() {
- query(null, function (result) {
- result = removeDuplicates(result);
- res.status(200).send(amendDetails(result));
- });
- }
- if (!lat || !lon) return noLocationSearch();
- var results = [];
- var params = {
- format: 'json',
- lat: lat,
- lon: lon
- };
- request.get('https://nominatim.openstreetmap.org/reverse.php', { qs: params, headers: HEADERS }, function (error, response, body) {
- if (error) return console.error(error);
- const position = JSON.parse(body);
- if (!position || !position.address) return noLocationSearch();
- query(position.address.city + ' , ' + position.address.country, function (result) {
- results = removeDuplicates(results.concat(result));
- if (results.length >= 20) return res.status(200).send(amendDetails(results));
- query(position.address.country, function (result) {
- results = removeDuplicates(results.concat(result));
- if (results.length >= 20) return res.status(200).send(amendDetails(results));
- query(null, function (result) {
- results = removeDuplicates(results.concat(result));
- return res.status(200).send(amendDetails(results));
- });
- });
- });
- });
- }
- function details(req, res, next) {
- const name = req.query.name;
- const lat = req.query.lat;
- const lon = req.query.lon;
- yelpClient.search({
- term: name,
- latitude: lat,
- longitude: lon
- }).then(response => {
- if (!response.jsonBody.businesses[0]) return res.status(200).send({});
- yelpClient.business(response.jsonBody.businesses[0].id).then(response => {
- res.status(200).send(response.jsonBody);
- }).catch(e => {
- console.log(e);
- });
- }).catch(e => {
- console.log(e);
- });
- }
|