0

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.
Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

0

How did you "check" the direct distance in Google Maps? By standard it shows you the road distance, not the direct distance...

Try using the Haversine formula to double check the results of the distanceTo() function:

Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • I tried with second option .......... it gives me 3km .... while i shown above gives me 5km(after converting from meters to km) ..... How can be there such a difference when google maps show it as 10km – Devrath Apr 24 '14 at 10:05
  • The second one returns miles! Change earthRadius to 6371, see http://en.wikipedia.org/wiki/Earth_radius – Tobi Apr 24 '14 at 10:07
  • @ Tobi ...... It is 5.201 ........ so both distance between & hervashine return same distance but why the google map is showing 10 when i validate it in google ........ is it because of road distance ? – Devrath Apr 24 '14 at 10:12
  • 1
    Just have a look at the Google Map. You can see that its not giving you the direct distance but the road distance! – Tobi Apr 24 '14 at 10:13