<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,follow" />
<title>Calculate distance</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=your_API KEY" type="text/javascript"></script>
<!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
<script type="text/javascript">
var geocoder, location1, location2, gDir;
function initialize() {
geocoder = new GClientGeocoder();
gDir = new GDirections();
GEvent.addListener(gDir, "load", function() {
var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
});
}
function showLocation() {
geocoder.getLocations(document.forms[0].address1.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode 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, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode 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);
}
});
}
});
}
</script>
</head>
<body onload="initialize()">
<form action="#" onsubmit="showLocation(); return false;">
<p>
<input type="text" name="address1" value="Address 1" />
<input type="text" name="address2" value="Address 2" />
<input type="submit" value="Search" />
</p>
</form>
<p id="results"></p>
</body>
</html>
Calculate Driving distance using google map
Started by OutshineGuy, Jul 03 2010 05:36 AM
8 replies to this topic
#1
Posted 03 July 2010 - 05:36 AM
Do you want to use Google Map for calculating distance between two locations. here is the full source code.
----------
Reserved
Reserved
#2
Posted 03 July 2010 - 07:22 AM
Wow!! it's really great. I liked it
#3
Posted 25 January 2012 - 03:03 PM
I think the topic is of interest to many
#4
Posted 01 February 2012 - 08:47 AM
I would like more specifics
#5
Posted 10 February 2012 - 05:35 PM
Subject promises to be interesting , perhaps the stock popcorn
#6
Posted 11 February 2012 - 10:21 AM
I think the theme will be of interest to many
#7
Posted 16 February 2012 - 11:19 AM
IMHO, it is debatable all this
#8
Posted 27 February 2012 - 03:16 AM
This is very intersting topic, Yes i used google map, while i am driving
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












