I am kind of stuck inside an 'undefined' exception. I am programming a tool that registers a traveled distance between two places. The application uses google maps distance matrix. When the user picks a vehicle, fills in an origin and a destination, and clicks 'create trip object' a tripObject is created which is then represented inside a div-element
. As a basic test the application alerts the distance between the two places in kms. This works fine. But I can't figure out how to register that value inside the tripObject, It keeps giving me an 'undefined' notification when a distance should be displayed inside the div-element, the rest is working fine.
I understand that this notification is caused by some logical mistakes. But for now on I haven't figure out what logical mistake caused this problem. I notice that I am facing these problems frequently. So could anyone explain to me what logical mistake I made?. Below is the code its HTML and Javascript combined.
var lastTripNr = 0;
var distance;
function calculateMapsDistance(orig,dest)
{
var calculatedDistance;
var directionsService = new google.maps.DirectionsService();
var request =
{
origin: orig,
destination: dest,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request,function(response,status)
{
if (status==google.maps.DirectionsStatus.OK)
{
alert((response.routes[0].legs[0].distance.value)/1000);
calculatedDistance = response.routes[0].legs[0].distance.value /1000;
}
else
{
alert("no route found!");
}
});
return calculatedDistance;
}
function createTripObject()
{
lastTripNr++;
var date = new Date();
var dateString = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
var selectionElement = document.getElementById("vehicle");
var selectedVehicle = selectionElement.options[selectionElement.selectedIndex].value;
var origin = document.getElementById("from").value;
var destination = document.getElementById("to").value;
var distance = calculateMapsDistance(origin,destination);
var tripObject =
{
nr: lastTripNr,
date: dateString,
vehicle: selectedVehicle,
orig: origin,
dest: destination,
dist: distance
};
document.getElementById("content").innerHTML += "<p>"+tripObject.nr + " | " + tripObject.date + " | " + tripObject.vehicle + " | " + tripObject.orig + " | " + tripObject.dest + " | " + tripObject.dist +
"</p>";
}
<html>
<head>
<title>basic directions service</title>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
</head>
<body>
<table id="interface">
<tr>
<th>vehicle</th>
<th>from:</th>
<th>to:</th>
<th>distance:</th>
</tr>
<tr>
<td>
<select id="vehicle">
<option value="Agila">Opel Agila</option>
<option value="Peugeot">Peugeot 108</option>
<option value="Atos">Hyundai Atos</option>
<option value="Matiz">Chevrolet Matiz</option>
<option value="Overig">overig</option>
</select>
</td>
<td><input id="from" type="text"></td>
<td><input id="to" type="text"></td>
</tr>
<tr>
<td colspan = "4"><button onclick="createTripObject()">create trip object</button></td>
</tr>
</table>
<div id="content"></div>
<script>
</script>
</body>
</html>