Skip to content

Instantly share code, notes, and snippets.

@studioTeaTwo
Last active May 5, 2016 17:43
Show Gist options
  • Save studioTeaTwo/33b52ecfa056fb36a88045e182561dba to your computer and use it in GitHub Desktop.
Save studioTeaTwo/33b52ecfa056fb36a88045e182561dba to your computer and use it in GitHub Desktop.
/**
* HomeController
*
* @description :: Server-side logic for managing Homes
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
"use strict";
const co = require('co');
var map = require('../services/GoogleStaticMapAPI');
module.exports = {
index: (req, res) => {
res.view("pages/admin/locations.swig");
},
createRoom: (req, res) => {
co(function *(){
res.view("pages/admin/create.swig");
});
},
locationDetail: (req, res) => {
co(function *(){
let locationId = req.params.location_id;
return yield Locations.findOne({where: {locationId:locationId}});
}).then((location) => {
sails.log(location);
if(location){
var locationpoint = [ location.latitude,location.longitude ].toString();
map.getImage(locationpoint, 'url').then(function(value){
sails.log('2');
res.view('pages/admin/detail.swig', {
location: location,
imageurl: value
});
}).catch(function(error){
sails.log.serverError(error);
});
sails.log('3');
}
else{
res.notFound();
}
sails.log('4');
}).catch((err) => {
return res.serverError(err);
});
}
};
"use strict";
const map = require('googlemaps');
const config = require('../../config/googlemap');
module.exports = {
getImage: (locationpoint, option) => {
return new Promise(function (resolve, reject) {
if(option != 'url' || option != 'binary'){
reject(new Error('StaticMapAPI param : ' + option));
}
var gmAPI = new map(config);
var params = {
center: locationpoint,
zoom: 15,
size: '500x400',
maptype: 'roadmap',
style: [
{
feature: 'road',
element: 'all',
rules: {
hue: '0x00ff00'
}
}
]
};
if(option == 'url'){
var imageurl = gmAPI.staticMap(params); // return static map URL
sails.log(imageurl);
resolve(imageurl);
sails.log('1');
}else{
// fetch asynchronously the binary image
gmAPI.staticMap(params, function(err, binaryImage) {
if(err) reject(new Error(err));
resolve(binaryImage);
});
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment