What i am doing:: I am trying to calculate the distance between two points in android using Latitude and Longitude co-ordinates of source and destionation
What is happening:: I am getting the output as shown in the log
Coordinates i am using
- SourceLatitude::
12.918286
- SourceLongitude::
77.669493
- DestinationLatitude::
12.959926
- DestinationLongitude::
77.647614
When i checked in google Maps i get Distance= 10.6 km
Question ::
- When i compile the code below I get the Log as shown clearly i am
getting the value
5182.4204
- Why is this happening ?
- How can i get value converted to
kilometers
MainActivity.java
public class MainActivity extends Activity {
public static String srcLatitude="12.918286";
public static String srcLongitude="77.669493";
public static String destLatitude="12.959926";
public static String destLongitude="77.647614";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("- Destination -", destLatitude+","+destLongitude);
Log.d("- Source -", srcLatitude+","+srcLongitude);
float CalculateDistance=CalculateDistance(srcLatitude,srcLongitude,destLatitude,destLongitude);
Log.d("- Result -", CalculateDistance+"");
}
private float CalculateDistance(String srcLatitude,
String srcLongitude, String destLatitude, String destLongitude) {
Location locationA = new Location("point A");
//Convert from string to double and then process
locationA.setLatitude(Double.parseDouble(destLatitude));
locationA.setLongitude(Double.parseDouble(destLongitude));
Location locationB = new Location("point B");
locationB.setLatitude(Double.parseDouble(srcLatitude));
locationB.setLongitude(Double.parseDouble(srcLongitude));
return locationA.distanceTo(locationB);
}
}
Log::
04-24 15:06:57.674: D/dalvikvm(588): Not late-enabling CheckJNI (already on)
04-24 15:07:00.143: D/- Destination -(588): 12.959926,77.647614
04-24 15:07:00.143: D/- Source -(588): 12.918286,77.669493
04-24 15:07:00.193: D/- Result -(588): 5182.4204
04-24 15:07:00.563: D/gralloc_goldfish(588): Emulator without GPU emulation detected.
{EDIT}
package com.example.latitudelongitudegoogleway;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
public static String srcLatitude="12.918286";
public static String srcLongitude="77.669493";
public static String destLatitude="12.959926";
public static String destLongitude="77.647614";
public static double hsrcLatitude=12.918286;
public static double hsrcLongitude=77.669493;
public static double hdestLatitude=12.959926;
public static double hdestLongitude=77.647614;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("- Destination -", destLatitude+","+destLongitude);
Log.d("- Source -", srcLatitude+","+srcLongitude);
float CalculateDistance=CalculateDistance(srcLatitude,srcLongitude,destLatitude,destLongitude);
Log.d("- Result -", CalculateDistance+"");
double hCalculateDistance=distFrom(hsrcLatitude,hsrcLongitude,hdestLatitude,hdestLongitude);
Log.d("- Result -", hCalculateDistance+"");
}
private float CalculateDistance(String srcLatitude,
String srcLongitude, String destLatitude, String destLongitude) {
Location locationA = new Location("point A");
//Convert from string to double and then process
locationA.setLatitude(Double.parseDouble(destLatitude));
locationA.setLongitude(Double.parseDouble(destLongitude));
Location locationB = new Location("point B");
locationB.setLatitude(Double.parseDouble(srcLatitude));
locationB.setLongitude(Double.parseDouble(srcLongitude));
return locationA.distanceTo(locationB)/1000;
}
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
* Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist/1000;
}
}
Log::
04-24 15:37:43.113: D/- Source -(851): 12.918286,77.669493
04-24 15:37:43.133: D/- Result -(851): 5.1824203
04-24 15:37:43.133: D/- Result -(851): 3.2323368667198737
04-24 15:37:43.353: D/gralloc_goldfish(851): Emulator without GPU emulation detected.