Created
July 23, 2019 15:38
-
-
Save harrybanda/ef11ee7665ce9ff60246d2791adf33dd to your computer and use it in GitHub Desktop.
// source https://jsbin.com/wukikuz
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
<!DOCTYPE html> | |
<html> | |
<body ng-app="myApp" ng-controller="MainController"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.0.8/es5-shim.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> | |
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places,language,drawing&key=AIzaSyAyxQeTDA0nVljW82CpgZR7G5siEq5eiuU"></script> | |
<script id="jsbin-javascript"> | |
/* jshint esnext: true */ | |
let app = angular.module('myApp', []); | |
app.controller('MainController', function($scope) { | |
// test data for 3 tonne non-refrigerated truck | |
const priceData = [ | |
{address: "Lusaka, Zambia", price: 300}, | |
{address: "Kabwe, Zambia", price: 781.25}, | |
{address: "Kitwe, Zambia", price: 5250}, | |
{address: "Kafue, Zambia", price: 875} | |
]; | |
// test delivery data | |
const deliveries = [ | |
{address: "Livingstone, Zambia"}, | |
{address: "Gizenga Rd, Lusaka, Zambia"}, | |
{address: "Chililabombwe, Zambia"}, | |
{address: "Kabwe, Zambia"} | |
]; | |
function pickPrice(deliveryAddress) { | |
let distances = []; | |
let itemsProcessed = 0; | |
priceData.forEach(function (p) { | |
let service = new google.maps.DistanceMatrixService(); | |
service.getDistanceMatrix( | |
{ | |
origins: [deliveryAddress], | |
destinations: [p.address], | |
travelMode: 'DRIVING', | |
unitSystem: google.maps.UnitSystem.METRIC, | |
avoidHighways: false, | |
avoidTolls: false, | |
}, callback(p.address, deliveryAddress)); | |
}); | |
function callback(savedAddress, deliveryAddress){ | |
return function(response, status) { | |
if (status !== "OK") { | |
alert("Error: " + status); | |
} else { | |
itemsProcessed++; | |
let distance = response.rows[0].elements[0].distance.value; | |
distances.push({address: savedAddress, distance: distance}); | |
if (itemsProcessed === priceData.length) { | |
let shortestDist = distances.reduce(function(prev, current) { | |
return (prev.distance < current.distance) ? prev : current | |
}); | |
let price = priceData.find(o => o.address === shortestDist.address); | |
console.log("Closeset town to " + deliveryAddress + " is: " + shortestDist.address); | |
console.log("The price to use is: K" + price.price); | |
console.log("--"); | |
} | |
} | |
} | |
} | |
} | |
// Run code | |
deliveries.map(function(d) { | |
pickPrice(d.address); | |
}); | |
}); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<body ng-app="myApp" ng-controller="MainController"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.0.8/es5-shim.min.js"><\/script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"><\/script> | |
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places,language,drawing&key=AIzaSyAyxQeTDA0nVljW82CpgZR7G5siEq5eiuU"><\/script> | |
</body> | |
</html> | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* jshint esnext: true */ | |
let app = angular.module('myApp', []); | |
app.controller('MainController', function($scope) { | |
// test data for 3 tonne non-refrigerated truck | |
const priceData = [ | |
{address: "Lusaka, Zambia", price: 300}, | |
{address: "Kabwe, Zambia", price: 781.25}, | |
{address: "Kitwe, Zambia", price: 5250}, | |
{address: "Kafue, Zambia", price: 875} | |
]; | |
// test delivery data | |
const deliveries = [ | |
{address: "Livingstone, Zambia"}, | |
{address: "Gizenga Rd, Lusaka, Zambia"}, | |
{address: "Chililabombwe, Zambia"}, | |
{address: "Kabwe, Zambia"} | |
]; | |
function pickPrice(deliveryAddress) { | |
let distances = []; | |
let itemsProcessed = 0; | |
priceData.forEach(function (p) { | |
let service = new google.maps.DistanceMatrixService(); | |
service.getDistanceMatrix( | |
{ | |
origins: [deliveryAddress], | |
destinations: [p.address], | |
travelMode: 'DRIVING', | |
unitSystem: google.maps.UnitSystem.METRIC, | |
avoidHighways: false, | |
avoidTolls: false, | |
}, callback(p.address, deliveryAddress)); | |
}); | |
function callback(savedAddress, deliveryAddress){ | |
return function(response, status) { | |
if (status !== "OK") { | |
alert("Error: " + status); | |
} else { | |
itemsProcessed++; | |
let distance = response.rows[0].elements[0].distance.value; | |
distances.push({address: savedAddress, distance: distance}); | |
if (itemsProcessed === priceData.length) { | |
let shortestDist = distances.reduce(function(prev, current) { | |
return (prev.distance < current.distance) ? prev : current | |
}); | |
let price = priceData.find(o => o.address === shortestDist.address); | |
console.log("Closeset town to " + deliveryAddress + " is: " + shortestDist.address); | |
console.log("The price to use is: K" + price.price); | |
console.log("--"); | |
} | |
} | |
} | |
} | |
} | |
// Run code | |
deliveries.map(function(d) { | |
pickPrice(d.address); | |
}); | |
}); | |
</script></body> | |
</html> |
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
/* jshint esnext: true */ | |
let app = angular.module('myApp', []); | |
app.controller('MainController', function($scope) { | |
// test data for 3 tonne non-refrigerated truck | |
const priceData = [ | |
{address: "Lusaka, Zambia", price: 300}, | |
{address: "Kabwe, Zambia", price: 781.25}, | |
{address: "Kitwe, Zambia", price: 5250}, | |
{address: "Kafue, Zambia", price: 875} | |
]; | |
// test delivery data | |
const deliveries = [ | |
{address: "Livingstone, Zambia"}, | |
{address: "Gizenga Rd, Lusaka, Zambia"}, | |
{address: "Chililabombwe, Zambia"}, | |
{address: "Kabwe, Zambia"} | |
]; | |
function pickPrice(deliveryAddress) { | |
let distances = []; | |
let itemsProcessed = 0; | |
priceData.forEach(function (p) { | |
let service = new google.maps.DistanceMatrixService(); | |
service.getDistanceMatrix( | |
{ | |
origins: [deliveryAddress], | |
destinations: [p.address], | |
travelMode: 'DRIVING', | |
unitSystem: google.maps.UnitSystem.METRIC, | |
avoidHighways: false, | |
avoidTolls: false, | |
}, callback(p.address, deliveryAddress)); | |
}); | |
function callback(savedAddress, deliveryAddress){ | |
return function(response, status) { | |
if (status !== "OK") { | |
alert("Error: " + status); | |
} else { | |
itemsProcessed++; | |
let distance = response.rows[0].elements[0].distance.value; | |
distances.push({address: savedAddress, distance: distance}); | |
if (itemsProcessed === priceData.length) { | |
let shortestDist = distances.reduce(function(prev, current) { | |
return (prev.distance < current.distance) ? prev : current | |
}); | |
let price = priceData.find(o => o.address === shortestDist.address); | |
console.log("Closeset town to " + deliveryAddress + " is: " + shortestDist.address); | |
console.log("The price to use is: K" + price.price); | |
console.log("--"); | |
} | |
} | |
} | |
} | |
} | |
// Run code | |
deliveries.map(function(d) { | |
pickPrice(d.address); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment