Tuesday, December 4, 2007

Solved Distance Calculation Puzzle !

When you check distance from you home to a store, to a restaurant, to a medical service provider and a big list - on their respective website. You will feel oh its hardly few miles from here. When you actually travel or try to find map from Yahoo, maps.google or mapquest - you will realize its not as near as it claimed on their website !! Is that a trick to attracts customers ?? I thought so until I came to know a better argument.

I am in process to implement "Distance Calculator" - that we used to see on websites. I found that all this calculators work on the basis of Latitude and Longitude. So thats in fact air distance between two places like from your home to a shop. Scientifically air distance would be shorter than road distance. A piece of Java code to calculate it :)

import java.lang.Math;
import java.lang.Double;

public int calculateDistance(double latitudeA, double
latitudeB, double longitudeA, double longitudeB)
{
double theDistance = (Math.sin(Math.toRadians(
latitudeA)) *
Math.sin(Math.toRadians(
latitudeB)) +
Math.cos(Math.toRadians(
latitudeA)) *
Math.cos(Math.toRadians(
latitudeB)) *
Math.cos(Math.toRadians(
longitudeA - longitudeB))); // Sin(laA)*Sin(laB) + Cos(laA)*Cos(laB)*Cos(loA-loB)

return = (Math.toDegrees(Math.acos(theDistance))) * 69.09; //
}

No comments: