Skip to content

Instantly share code, notes, and snippets.

@aliaramli
Created February 21, 2021 11:04
Show Gist options
  • Save aliaramli/53da7c01549475a9b3ea51ec819c1a94 to your computer and use it in GitHub Desktop.
Save aliaramli/53da7c01549475a9b3ea51ec819c1a94 to your computer and use it in GitHub Desktop.
$(function() {
// add input listeners
google.maps.event.addDomListener(window, 'load', function () {
var from_places = new google.maps.places.Autocomplete(document.getElementById('from_places'));
var to_places = new google.maps.places.Autocomplete(document.getElementById('to_places'));
google.maps.event.addListener(from_places, 'place_changed', function () {
var from_place = from_places.getPlace();
var from_address = from_place.formatted_address;
$('#origin').val(from_address);
});
google.maps.event.addListener(to_places, 'place_changed', function () {
var to_place = to_places.getPlace();
var to_address = to_place.formatted_address;
$('#destination').val(to_address);
});
});
// calculate distance
function calculateDistance() {
var origin = $('#origin').val();
var destination = $('#destination').val();
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL, // miles and feet.
// unitSystem: google.maps.UnitSystem.metric, // kilometers and meters.
avoidHighways: false,
avoidTolls: true
}, callback);
}
// get distance results
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
$('#result').html(err);
} else {
var origin = response.originAddresses[0];
var destination = response.destinationAddresses[0];
if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
$('#result').html("Better get on a plane. There are no roads between " + origin + " and " + destination);
} else {
var distance = response.rows[0].elements[0].distance;
var duration = response.rows[0].elements[0].duration;
console.log(response.rows[0].elements[0].distance);
var distance_in_kilo = distance.value / 1000; // the kilom
var distance_in_mile = distance.value / 1609.34; // the mile
var duration_text = duration.text;
var duration_value = duration.value;
var delivery_rate = 0.8;
var delivery_charge = 3;
if (distance_in_kilo >3){
delivery_charge = distance_in_kilo * delivery_rate;
delivery_charge = Math.round(delivery_charge.toFixed(2)*10)/10;
}
$('#in_mile').text(distance_in_mile.toFixed(2));
$('#in_kilo').text(distance_in_kilo.toFixed(2));
$('#delivery_charge').text("RM "+ delivery_charge.toFixed(2));
}
}
}
// print results on submit the form
$('#distance_form').submit(function(e){
e.preventDefault();
calculateDistance();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment