Jump to content

  • Log in with Facebook
         Sign In   
  • Create Account

Calculate Driving distance using google map


  • Please log in to reply
9 replies to this topic

Poll: Calculate Driving distance using google map (5 member(s) have cast votes)

Will u use google map for your website?

  1. Yes (4 votes [80.00%] - View)

    Percentage of vote: 80.00%

  2. No (1 votes [20.00%] - View)

    Percentage of vote: 20.00%

Vote Guests cannot vote

#1 Outshine Guy

Outshine Guy

    Administrator

  • Administrators
  • 11 posts
  • LocationIndia

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.


<!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>

-------
RESERVED

#2 manishk

manishk

    Major

  • Members
  • PipPipPip
  • 46 posts

Posted 03 July 2010 - 07:22 AM

Wow!! it's really great. I liked it

#3 ProfesorIvanov

ProfesorIvanov

    Corporal

  • Members
  • Pip
  • 1 posts

Posted 25 January 2012 - 03:03 PM

I think the topic is of interest to many

#4 DoctorPavlov

DoctorPavlov

    Corporal

  • Members
  • Pip
  • 3 posts

Posted 01 February 2012 - 08:47 AM

I would like more specifics

#5 DoctorPavlov

DoctorPavlov

    Corporal

  • Members
  • Pip
  • 3 posts

Posted 10 February 2012 - 05:35 PM

Subject promises to be interesting , perhaps the stock popcorn <_<

#6 ProfesorPauls

ProfesorPauls

    Corporal

  • Members
  • Pip
  • 1 posts

Posted 11 February 2012 - 10:21 AM

I think the theme will be of interest to many

#7 DoctorPavlov

DoctorPavlov

    Corporal

  • Members
  • Pip
  • 3 posts

Posted 16 February 2012 - 11:19 AM

IMHO, it is debatable all this

#8 mydiscoutlet

mydiscoutlet

    Corporal

  • Members
  • Pip
  • 7 posts

Posted 27 February 2012 - 03:16 AM

This is very intersting topic, Yes i used google map, while i am driving

#9 hamm

hamm

    Major

  • Members
  • PipPipPip
  • 32 posts

Posted 13 October 2012 - 08:17 PM

wow nice post ,, plz keep posting . thanks

#10 raja77

raja77

    Sergeant

  • Members
  • PipPip
  • 28 posts

Posted 29 December 2012 - 01:16 AM

can i use your code in my website?. very nice indeed




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users