Oct 12, 2011

Distance between two locations with the help of Latitude and Longitude

Finding Distance between two locations with Latitude and Longitude.

Lets see how can we find out the distance and duration between two diffrent locations,
if you have the latitude and longitude of two places.

Here is the pic of the application :-

Steps for finding distance with latitude/longitude.
1) Lets say we have latitude and longitude set P1 and P2.
    Google MAP comes up with the class GLatLng. This class helps to encapsulate Latitude
    and Longitude.
GLatLng is a point in geographical coordinates longitude and latitude. Notice that although usual map projections associate longitude with the x-coordinate of the map, and latitude with the y-coordinate, the latitude cooridnate is always written first, followed by the longitude, as it is custom in cartography.  

2) From this GLatLng points we need to get place name first.
class GClientGeocoder will help us to get the place name from LatLng.
This class is used to communicate directly with Google servers to obtain geocodes for user specified addresses. In addition, a geocoder maintains its own cache of addresses, which allows repeated queries to be answered without a round trip to the server. As a general best practice, it is not recommended to use GClientGeocoder functions in a loop. Developers that have multiple addresses to geocode should probably use our HTTP Geocoder instead.

Method :-
getLocations(latlng:GLatLng, callback:function)

This method performs reverse-geocoding, the conversion of a latitude/longitude pair into human-readable addresses. getLocations() sends a request to the Google geocoding service, asking it to return the address for the given latlng and pass the response in the given callback. As this method requires a call to a Google server, you must also pass a callback method to handle the response. This response will contain a Status code, and if successful, one or more Placemark objects. Note that this method may instead pass an addressable String, as indicated above; in that case, the service will do a standard geocode. If however, the first argument contains a GLatLng, the service will do a reverse-geocode.

Here is the example of function :-
function showAddress(response) {
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
        'orig latlng:' + response.name + ' ' + 
        'latlng:' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + ' ' +
        'Status Code:' + response.Status.code + ' ' +
        'Status Request:' + response.Status.request + ' ' +
        'Address:' + place.address + ' ' +
        'Accuracy:' + place.AddressDetails.Accuracy + ' ' +
        'Country code: ' + place.AddressDetails.Country.CountryNameCode);

3) With the help of place name, we will query map for getting the distance and duration.
A sample code snippet for displaying location with map :-   
// Create a directions object and register a map and DIV to hold the 
// resulting computed directions

var map;
var directionsPanel;
var directions;

function initialize() {
  map = new GMap2(document.getElementById("map_canvas"));
  directionsPanel = document.getElementById("my_textual_div");
  directions = new GDirections(map, directionsPanel);
  directions.load("from:"+ fromAddress + "to: " + toAddress);

  fromAddress and toAddress is the same which we can retrieve easily with as we have discussed in step 2.

Let's see the demo in a sample application


From:    To: 
Place A:   Place B: 

Formatted DirectionsMap


Post a Comment