Calculate Distance using Google Maps API

Calculate Distance on different location in the world by placing the  source and destination place in the below code while runing the below code.

This application using the Maps API to calculate the distance between two place. Below are the code to download and screenshot to display the output while running the application.

GMAP Distance

GMAP Distance

The Output will look like:
 

GMAP Output

GMAP Output

Advertisements

7 Comments

  1. May 6, 2009 at 6:21 am

    Hi, nice post. I have been pondering this topic,so thanks for posting. I will definitely be subscribing to your blog. Keep up great writing

    • rafeekphp said,

      May 6, 2009 at 8:06 am

      Thanks for your appreciation. 🙂

      Rafeek

  2. ALex said,

    May 14, 2009 at 11:50 pm

    … nice post, can u send to me de code. thx

    • rafeekphp said,

      May 15, 2009 at 4:56 am

      Hi Alex,
      This is code to calculate the distance using Google MAP API. YOu can get your own API by login into your gmail account and subcribe the MAP API from Google.

      Google Maps JavaScript API Example: Extraction of Geocoding Data

      var geocoder, location1, location2;

      function initialize() {
      geocoder = new GClientGeocoder();
      }

      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};
      calculateDistance();
      }
      });
      }
      });
      }

      function calculateDistance()
      {
      try
      {
      var R = 3959; // earth’s mean radius in miles
      var dLat = (location2.lat – location1.lat) * Math.PI / 180;
      var dLon = (location2.lon – location1.lon) * Math.PI / 180;
      var lat1 = location1.lat * Math.PI / 180;
      var lat2 = location2.lat * Math.PI / 180;
      var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon/2)
      * Math.sin(dLon/2);
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
      var d = R * c;
      var miledistance = Math.round(d);
      var kmdistance = Math.round(miledistance * 1.609344);
      document.getElementById(‘results’).innerHTML = ‘Address 1: ‘ + location1.address
      + ‘Address 2: ‘ + location2.address + ‘Distance:
      + miledistance + ‘ miles (or ‘ + kmdistance + ‘ kilometers)’;
      }
      catch (error)
      {
      alert(error);
      }
      }

      Thanks
      Rafeek 🙂

      • bob said,

        April 18, 2010 at 7:49 am

        “var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));” can be simplified to “var c = 2 * Math.asin(Math.sqrt(a));”

  3. Ahmed said,

    August 10, 2009 at 2:52 pm

    I was wondering if you could help me showing google map alongside this code, because at them moment its the code only according to google maps terms and conditions a map should be displayed.
    Kind regards

    • rafeekphp said,

      August 15, 2009 at 12:09 pm

      Hi AHmed,
      Currently its designed like accepting google maps t & c. so we cant change now. But i will try to fix this buy getting some way from google to use it.

      Thanks
      Rafeek 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: