1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    <script type="text/javascript">

    var map;
    var directionsService = new google.maps.DirectionsService();
    var geocoder = new google.maps.Geocoder();
    var directionsDisplay = new google.maps.DirectionsRenderer(); 
    var calcDist;

    function initialize(address) {
        if (geocoder) {
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                   var myOptions = {
                        zoom: 13,
                        center: results[0].geometry.location,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    }; 
                    map = new google.maps.Map(document.getElementById("map"), myOptions);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        } 
    }


    function calcMileage(distType, start, end, fieldName) {

        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById("dir"));

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

        };

        //send request
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {

                directionsDisplay.setDirections(response);

                //This example only uses one trip and one route
                var myRoute = response.trips[0].routes[0];

                //Gives you distance in meters
                var myRouteMeters = myRoute.distance.value;

                //Gives you text representation using units of origin country (34.5 mi)
                var myRouteOrigin = myRoute.distance.text;

                //alert(myRouteOrigin);

                //Removing "mi" from mileage
                var myRouteMiles = myRouteOrigin.split(' ');
                calcDist = myRouteMiles[0];

            }
            else {
                calcDist = 0;
            }

            $('#mileage').val(myRouteMeters * 0.000621371192);
            $('#kilometers').val(myRouteMeters * 0.001);
        });

    }

    $(document).ready(function() {
        initialize('60545 United States');

        $('#getMileage').click(function() {
            var postalcode1 = $('#postalcode1').val();
            var country1 = $('#country1').val();
            var postalcode2 = $('#postalcode2').val();
            var country2 = $('#country2').val();

            calcMileage('mi', postalcode1 + ' ' + country1, postalcode2 + ' ' + country2, 'miDistance');
            return false;
        });
    });

</script>