I've created a price/distance calculator for a local taxi firm and currently it calculates the cost of a journey between 2 selected points. I'm trying to add the ability to select more than one destination but I'm having difficulties.
I've created the form, which has 3 points - a pick-up point, a destination, and an optional way point on the journey, which is hidden and only available to view if a button asking if an additional destination is required is clicked.
I've tried to create an if statement in the JavaScript which works as follows - if the additional destination is empty, then only calculate the distance between the pickup and drop-off points, but if the additional destination is populated then calculate the distance between all 3 points.
This hasn't and has become pretty complicated. Is there a simpler way to do the above? I'm no JS expert so I'm learning as I go...
Thanks in advance.
My code:
var geocoder = null;
var location1 = null;
var location2 = null;
var gDir = null;
var directions = null;
var total = 0;
function roundNumber(num, dec) {
var result = Math.floor(num*Math.pow(10 ,dec))/Math.pow(10,dec);
return result;
}
function from(form) {
address1=form.start.options[form.start.selectedIndex].value
form.address1.value=address1
form.address1.focus()
}
function to(form) {
address2=form.end.options[form.end.selectedIndex].value
form.address2.value=address2
form.address2.focus()
}
function initialize() {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(54.019066,-1.381531),9);
map.setMapType(G_NORMAL_MAP);
geocoder = new GClientGeocoder();
gDir = new GDirections(map);
GEvent.addListener(gDir, "load", function() {
var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
var miles = drivingDistanceMiles.toFixed(0);
//var cost = (((miles - 1) * 1.9) + 3.6).toFixed(2);
var meters = gDir.getDistance().meters.toFixed(1);
if (document.forms[0].vehicle.value == "minibus")
{
if(miles < 70){
var cost = miles *2.30;
}
if(miles >70){
var cost = miles *1.70;
}
}
else
{
if(miles < 70){
var cost = miles *1.75;
}
if(miles >70){
var cost = miles *1.2;
}
}
document.getElementById('from').innerHTML = '<strong>From: </strong>' + location1.address;
document.getElementById('to').innerHTML = '<strong>To: </strong>' + location2.address;
document.getElementById('cost').innerHTML = '<span class="fare"><strong>Estimated Taxi FARE:</strong>' + ' £' + cost.toFixed(0) + '</span>';
document.getElementById('miles').innerHTML = '<strong>Distance: </strong>' + miles + ' Miles';
document.getElementById('price2').value = cost.toFixed(2);
document.getElementById('pickup').value = location1.address;
document.getElementById('dest').value = location2.address;
});
}
function showLocation()
// start of possible values for address not recognized on google search
// values for address1
{
if (document.forms[0].address1.value == "heathrow" || document.forms[0].address1.value == "Heathrow" || document.forms[0].address1.value == "heathrow airport" || document.forms[0].address1.value == "Heathrow Airport" || document.forms[0].address1.value == "London Heathrow" || document.forms[0].address1.value =="london heathrow" )
{
(document.forms[0].address1.value = "Heathrow Airport");
}
if (document.forms[0].address2.value == "heathrow" || document.forms[0].address2.value == "Heathrow" || document.forms[0].address2.value == "heathrow airport" || document.forms[0].address2.value == "Heathrow Airport" || document.forms[0].address2.value == "London Heathrow" || document.forms[0].address2.value =="london heathrow" )
{
(document.forms[0].address2.value = "Heathrow Airport");
}
if (document.forms[0].address1.value == "Leeds Railway Station" || document.forms[0].address1.value == "Leeds Train Station" || document.forms[0].address1.value == "Leeds Rail Station" || document.forms[0].address1.value == "leeds railway station" || document.forms[0].address1.value == "leeds train station" || document.forms[0].address1.value =="leeds rail station" )
{
(document.forms[0].address1.value = "Leeds LS1 4");
}
if (document.forms[0].address2.value == "Leeds Railway Station" || document.forms[0].address2.value == "Leeds Train Station" || document.forms[0].address2.value == "Leeds Rail Station" || document.forms[0].address2.value == "leeds railway station" || document.forms[0].address2.value == "leeds train station" || document.forms[0].address2.value =="leeds rail station" )
{
(document.forms[0].address2.value = "Leeds LS1 4");
}
if (document.forms[0].address1.value == "York Railway Station" || document.forms[0].address1.value == "York Train Station" || document.forms[0].address1.value == "York Rail Station" || document.forms[0].address1.value == "york railway station" || document.forms[0].address1.value == "york train station" || document.forms[0].address1.value =="york rail station" )
{
(document.forms[0].address1.value = "Station Road, York YO24 1");
}
if (document.forms[0].address2.value == "York Railway Station" || document.forms[0].address2.value == "York Train Station" || document.forms[0].address2.value == "York Rail Station" || document.forms[0].address2.value == "york railway station" || document.forms[0].address2.value == "york train station" || document.forms[0].address2.value =="york rail station" )
{
(document.forms[0].address2.value = "Station, York YO24 1");
}
if (document.forms[0].address1.value == "Thirsk Railway Station" || document.forms[0].address1.value == "Thirsk Train Station" || document.forms[0].address1.value == "Thirsk Rail Station" || document.forms[0].address1.value == "thirsk railway station" || document.forms[0].address1.value == "thirsk train station" || document.forms[0].address1.value =="thirsk rail station" )
{
(document.forms[0].address1.value = "Station Road, Thirsk YO7 4");
}
if (document.forms[0].address2.value == "Thirsk Railway Station" || document.forms[0].address2.value == "Thirsk Train Station" || document.forms[0].address2.value == "Thirsk Rail Station" || document.forms[0].address2.value == "thirsk railway station" || document.forms[0].address2.value == "thirsk train station" || document.forms[0].address2.value =="thirsk rail station" )
{
(document.forms[0].address2.value = "Station Road, Thirsk YO7 4");
}
if (document.forms[0].address1.value == "Northallerton Railway Station" || document.forms[0].address1.value == "Northallerton Train Station" || document.forms[0].address1.value == "Northallerton Rail Station" || document.forms[0].address1.value == "northallerton railway station" || document.forms[0].address1.value == "northallerton train station" || document.forms[0].address1.value =="northallerton rail station" )
{
(document.forms[0].address1.value = "Boroughbridge Road, Northallerton DL7 8");
}
if (document.forms[0].address2.value == "Northallerton Railway Station" || document.forms[0].address2.value == "Northallerton Train Station" || document.forms[0].address2.value == "Northallerton Rail Station" || document.forms[0].address2.value == "northallerton railway station" || document.forms[0].address2.value == "northallerton train station" || document.forms[0].address2.value =="northallerton rail station" )
{
(document.forms[0].address2.value = "Boroughbridge Road, Northallerton DL7 8");
}
geocoder.getLocations(document.forms[0].address1.value + document.forms[0].uk.value || document.forms[0].start.value + document.forms[0].uk.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to find the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(document.forms[0].address2.value + document.forms[0].uk.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to find the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
}
});
}
});
}