2

I need to calculate time and distance between current location to given location using gps.

any one please give idea

  final TextView speedView = (TextView) findViewById(R.id.speedtxt);
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
     locationListener = new LocationListener() {
         public void onLocationChanged(Location location) {
             if (location.hasSpeed()) {
                float speed = location.getSpeed() * 3.6f;// km/h
                speedView.setText(String.format("%.0f km/h", speed));
            }
        }
        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
Manikandan K
  • 1,081
  • 1
  • 9
  • 17

2 Answers2

1

Location has the distanceTo method that returns the approximate distance in meters between this location and the given location. E.g

float distance = gpsLocation.distanceTo(dstLocation);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

have a look at Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) method

http://developer.android.com/reference/android/location/Location.html

//finding the distance between destination and mylocation

                Location.distanceBetween(startLatitude , startLongitude, endLatitude , endLongitude , results);
// to convert metre to KM
                if(results[0]>1000)
                {
                    distString = (double)Math.round(results[0]/1000)+"km";

                }
                else
                {
                    distString = Math.round(results[0])+"m";
                }
                Log.d("dist string",distString);
// Store the distances in array

                distance[i]=distString;

and just chek, there is already asked question

Calculating distance between two geographic locations

Community
  • 1
  • 1
Blue_Alien
  • 2,148
  • 2
  • 25
  • 29