0

I am trying to create a mileage calculator for an app, and I found the code online.

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var directionDisplay;
var map;


function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
    zoom:12,
   `enter code here` mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: copenhagen
}

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}


var directionsService = new google.maps.DirectionsService();

function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");

var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
    }
});
}
</script>

<title>Distance Calculator</title>

<style type="text/css">

        body {
            font-family:Helvetica, Arial;
        }
        #map_canvas {
            height: 50%;
        }
</style>
</head>
<body>

<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
    <div>
        <p>
            <label for="start">Start: </label>
            <input type="text" name="start" id="start" />

            <label for="end">End: </label>
            <input type="text" name="end" id="end" />

            <input type="submit" value="Calculate Route" onclick="calcRoute()" />
        </p>
        <p>
            <label for="distance">Distance (km): </label>
            <input type="text" name="distance" id="distance" readonly="true" />
        </p>
    </div>
    <div id="map_canvas"></div>
</body>
</html>

javascript google geolocation maps 

I want there to be a click to email link on the link on the page which opens up users email (such as Windows Live Mail on a PC or the Yahoo Mail app on a tablet) and says in the body of the email "Your mileage is ???". I can do this, but I want the mileage to be inputted automatically. I have added in this code in the html section of the script...

<p><a href="mailto:youremail.yahoo.com?subject=Mileage
&body=calcRoute('string', 'distance')">Click to send email</a></p>

...and it opens the email but doesn't show the calculated mileage. Instead in the body it shows calcRoute('string, distance'). How do I get it to show the mileage?

I understand this question is long but I would appreciate it if you could take the time to answer. It is very important that I get an answer that works as soon as possible. Thank you.

2 Answers 2

1

You have to compute the mailto attribute value in the link onclick event. Supposing calcRoute function returns a string, the following code should work:

<a href="mailto:youremail.yahoo.com?subject=Mileage&body="
   onclick="this.href += calcRoute('string', 'distance');"
>
Click to send email
</a>
3
  • I'm sorry but it doesn't work. It got closer than anything I've achieved, but on the email body it just says undefined. Commented Jul 29, 2013 at 16:43
  • Having the "undefined" string in email body means that the calcRoute function does not return anything. Commented Jul 29, 2013 at 19:28
  • But how do I get it to do that? I know I may be bothering you but I do not have an in depth understanding of javascript and I would really appreciate it if you would tell me the code I need to write and where to write it. Thanks. Commented Jul 30, 2013 at 0:50
0

You should check out this link should be helpful

My guess is that it's not detecting the subject. Are you sure your javascript CalcRoute is returning a variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.