Created
September 25, 2015 19:27
-
-
Save ziad-saab/a2d18c249e970544f1c4 to your computer and use it in GitHub Desktop.
Promises Exercise 3 Possible Solution
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
var request = require('request'); | |
var prompt = require('prompt'); | |
var Promise = require('bluebird'); | |
var promptPromisified = Promise.promisifyAll(prompt); | |
var requestPromisified = Promise.promisify(request); | |
//necessary function for distanceTo | |
Number.prototype.toRadians = function() { | |
return this * Math.PI / 180; | |
} | |
//Returns distance between point1 and point2; | |
var distanceTo = function(point1, point2, radius) { | |
radius = (radius === undefined) ? 6371 : Number(radius); | |
var R = radius; | |
var φ1 = point1.lat.toRadians(), λ1 = point1.lng.toRadians(); | |
var φ2 = point2.lat.toRadians(), λ2 = point2.lng.toRadians(); | |
var Δφ = φ2 - φ1; | |
var Δλ = λ2 - λ1; | |
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + | |
Math.cos(φ1) * Math.cos(φ2) * | |
Math.sin(Δλ/2) * Math.sin(Δλ/2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
var d = R * c; | |
return d; | |
}; | |
// // Wishful thinking (the code below does NOT work) | |
// var userLocation = prompt.get(['location']); | |
// var userGeoLocation = request('https://maps.googleapis.com/maps/api/geocode/json?address='+userInput); | |
// var issPosition = request('http://api.open-notify.org/iss-now.json'); | |
// //var dist= distance(.....); | |
// //console.log("your distance is " + distance); | |
// // End of wishful thinking (the code above does NOT work) | |
// Ask the user for their location, and tell them the lat/long for that location | |
function getUserLatLong() { | |
return promptPromisified.getAsync(['location']).then( | |
function(result) { | |
return requestPromisified('https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyCqB6bDHnvEmYxcRm5qg6yso2V9Q4MbOiE&address='+result.location); | |
} | |
).then( | |
function(result2) { | |
var body = result2[1]; | |
var location = JSON.parse(body).results[0].geometry.location; | |
// console.log("Your location is: "); | |
// console.log(location); | |
return location; | |
} | |
); | |
} | |
function getIssLatLong() { | |
return requestPromisified('http://api.open-notify.org/iss-now.json').then( | |
function(result) { | |
var body = result[1]; | |
var issLocation = JSON.parse(body); | |
return {lat: issLocation.iss_position.latitude, lng: issLocation.iss_position.longitude}; | |
} | |
); | |
} | |
function getDistanceToIss() { | |
var userLocPromise = getUserLatLong(); | |
var issLocPromise = getIssLatLong(); | |
return Promise.join(userLocPromise, issLocPromise, function(userLoc, issLoc) { | |
var distance = distanceTo(userLoc, issLoc); | |
return distance; | |
}); | |
} | |
getDistanceToIss().then( | |
function(distance) { | |
console.log("The distance in metres is: " + distance); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment