How to calculate distance between any two geo points (latitudes and longitudes) in android


/**
 * @param fromLatitude
 * @param fromLongitude
 * @param toLatitude
 * @param toLongitude
 * @return distance between the two latitudes and longitudes in kms.
 */
 public static double calculateDistance(double fromLatitude,double fromLongitude,double toLatitude,double toLongitude)
 {

 float results[] = new float[1];

try {
 Location.distanceBetween(fromLatitude,fromLongitude, toLatitude, toLongitude, results);
 } catch (Exception e) {
 if (e != null)
 e.printStackTrace();
 }

 int dist = (int) results[0];
 if(dist<=0)
 return 0D;

 DecimalFormat decimalFormat = new DecimalFormat("#.##");
 results[0]/=1000D;
 String distance = decimalFormat.format(results[0]);
 double d = Double.parseDouble(distance);
 return d;
 }
Advertisements

Tags:

About krishjlk

http://www.facebook.com/krishna.kanth.121

One response to “How to calculate distance between any two geo points (latitudes and longitudes) in android”

  1. Sameer says :

    Nice piece of code

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: