Last active
November 4, 2016 15:30
-
-
Save mimiflynn/c1abb475bc70980d81e43e1e95cc035c to your computer and use it in GitHub Desktop.
various utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import request from 'request'; | |
/** | |
* Module of Google maps API utils | |
* @module /app/utils/maps | |
*/ | |
// create a static map url | |
const createStaticMap = ({ lat, long }) => { | |
const url = 'https://maps.googleapis.com/maps/api/staticmap?center=' + lat + ',' + long + '&zoom=13&size=470x450&maptype=roadmap&markers=color:red%7Clabel:CityMD%7C' + lat + ',' + long + '&key=' + global.CONSTANTS.MAPS_KEY; | |
return url; | |
} | |
// request geocode data and return location info for search | |
const geocode = ({ search }) => { | |
console.log('searchTerms ', search); | |
const url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + search + '&key=' + global.CONSTANTS.MAPS_KEY; | |
console.log('geocode url: ', url); | |
return new Promise ((resolve, reject) => { | |
request({ | |
url | |
}, (error, response, body) => { | |
if (error) { | |
console.log('Geocode error: ', error); | |
reject(Error('error')); | |
} else { | |
try { | |
const cleanBody = JSON.parse(body); | |
let location = {}; | |
if (cleanBody.results.length !== 0) { | |
location = cleanBody.results[0].geometry.location | |
} | |
resolve(location); | |
} catch (error) { | |
reject(error); | |
} | |
} | |
}); | |
}); | |
}; | |
export default { createStaticMap, geocode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cleanResponse = (response) => { | |
return JSON.parse(JSON.stringify(response)); | |
}; | |
const getFirstValue = (response) => { | |
// check if response is empty | |
if (emptyObject(response)) { | |
// response is empty, send new object | |
console.log('Response is empty'); | |
return {}; | |
} else { | |
// not empty | |
const responseKey = Object.keys(response)[0]; | |
return (response[responseKey]); | |
} | |
}; | |
const emptyObject = (object) => { | |
return Object.keys(object).length === 0 && object.constructor === Object; | |
}; | |
export { cleanResponse, getFirstValue, emptyObject }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment