how to calculate distances between multiple points (1 origin to different destinations) in one goal?

923 views
Skip to first unread message

kaiik

unread,
Nov 3, 2011, 7:46:47 AM11/3/11
to google-map...@googlegroups.com
Hello guys,

I have done the code to calculate the distance between 1 origin point and 1 destination point (very easy, to be honest). Is there a possibility that I can calculate the distances between one 1 origin and multiple destinations when I click the submit button?

my original code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Calculate distances</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">

    var location1;
    var location2;
   
    var address1;
    var address2;

    var latlng;
    var geocoder;
    var map;
   
    var distance;
   

function initialize()
    {
        geocoder = new google.maps.Geocoder(); // creating a new geocode object
       
        // getting the two address values
        address1 = document.getElementById("address1").value;
        address2 = document.getElementById("address2").value;
       
        // finding out the coordinates
        if (geocoder)
        {
            geocoder.geocode( { 'address': address1}, function(results, status)
            {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    //location of first address (latitude + longitude)
                    location1 = results[0].geometry.location;
                } else
                {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
            geocoder.geocode( { 'address': address2}, function(results, status)
            {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    //location of second address (latitude + longitude)
                    location2 = results[0].geometry.location;
                    // calling the showMap() function to create and show the map
                    showMap();
                } else
                {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    }


function showMap()
    {
       
        // show route between the points
       
        directionsService = new google.maps.DirectionsService();
        directionsDisplay = new google.maps.DirectionsRenderer(
        {
           
        });
        directionsDisplay.setMap(map);
        var request = {
            origin:location1,
            destination:location2,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.IMPERIAL
           
        };
       
        directionsService.route(request, function(response, status)
        {
            if (status == google.maps.DirectionsStatus.OK)
            {
                directionsDisplay.setDirections(response);
                distance = "The distance between the two points on the chosen route is: "+response.routes[0].legs[0].distance.text;
                distance += "<br/>The aproximative driving time is: "+response.routes[0].legs[0].duration.text;
                document.getElementById("distance_road").innerHTML = distance;
            }
        });
       
       
       
        // compute distance between the two points
        var R = 6371;
        var dLat = toRad(location2.lat()-location1.lat());
        var dLon = toRad(location2.lng()-location1.lng());
       
        var dLat1 = toRad(location1.lat());
        var dLat2 = toRad(location2.lat());
       
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(dLat1) * Math.cos(dLat1) *
                Math.sin(dLon/2) * Math.sin(dLon/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c;
       
        document.getElementById("distance_direct").innerHTML = "<br/>The distance between the two points (in a straight line) is: "+d;
    }
   
    function toRad(deg)
    {
        return deg * Math.PI/180;
    }


</script>
</head>


<body bgcolor="#eeeeee">
    <div id="form" style="width:100%; height:20%">
        <table align="center" valign="center">
            <tr>
                <td colspan="7" align="center"><b>Find the distance between two locations</b></td>
            </tr>
            <tr>
                <td colspan="7">&nbsp;</td>
            </tr>
            <tr>
                <td>First address:</td>
                <td>&nbsp;</td>
                <td><input type="text" name="address1" id="address1" size="50"/></td>
                <td>&nbsp;</td>
                <td>Second address:</td>
                <td>&nbsp;</td>
                <td><input type="text" name="address2" id="address2" size="50"/></td>
            </tr>
            <tr>
                <td colspan="7">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="7" align="center"><input type="button" value="Show" onClick="initialize();"/></td>
            </tr>
        </table>
    </div>
    <center><div style="width:100%; height:10%" id="distance_direct"></div></center>
    <center><div style="width:100%; height:10%" id="distance_road"></div></center>
   
   
</body>
</html>


Very appreciate for the help!

Rossko

unread,
Nov 3, 2011, 3:14:53 PM11/3/11
to Google Maps JavaScript API v3
> Is there a possibility that I
> can calculate the distances between one 1 origin and multiple destinations
> when I click the submit button?

Distance matrix service
http://code.google.com/apis/maps/documentation/javascript/services.html#distance_matrix
Reply all
Reply to author
Forward
0 new messages