-3

I want your help to calculate the distance b/t two longitudes. Can anybody tell me how to find the distance? I am new on Android studio. I want to find the distance in my app. Destination address in defined by mlat and mlong and current address is calculated through GPS whose value goes in longitude and latitude.

My code is:

    double mlat =30.726030;
    double mlong=76.759268;
    imgButton1 =(ImageButton)findViewById(R.id.imageButton1);
    mgButton1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
                Location gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
                if (gpsLocation != null) {
                     double latitude = gpsLocation.getLatitude();
                     double longitude = gpsLocation.getLongitude();    
                     String result = "Latitude: " + gpsLocation.getLatitude() +
                                    " Longitude: " + gpsLocation.getLongitude();
                     tvAddress.setText(result);
                } else {
                     showSettingsAlert();
                }
          }
   });

Tell me how to find the distance between two longitudes and latitudes.

Arnaud
  • 6,869
  • 8
  • 49
  • 68

3 Answers3

0

Try Below

Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distanceInMeters = loc1.distanceTo(loc2);
Abhishek Patel
  • 4,190
  • 1
  • 22
  • 38
0

This is simple coordinate math. Or you can this method that I found by googling.

Display Name
  • 882
  • 1
  • 10
  • 20
0

Use the distanceBetween method to calculate distance:

Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Arnaud
  • 6,869
  • 8
  • 49
  • 68
Ravi Varma
  • 43
  • 2
  • 5