-7

I have my API to calculate the distance. However, I am unable to get the codes to use the API. I am novice in android.Please provide the codes to use the API. I was using the latitude longitude but distance was not showing correctly. Guys help!

public String[] Distance(final double lat1, final double lon1, final double lat2, final double lon2) {
    final String[] parsedDistance = new String[1];
    final String[] response = new String[1];
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                Request request = new Request.Builder()
                        .url("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=lat1%2Clon1&destinations=lat2%2Clon2")
                        .get()
                        .addHeader("key", "AIzaSyBP1g2YfJmsZ8zVhQBKffnGxL7TyqTYFxk")
                        .addHeader("cache-control", "no-cache")
                        .build();

            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return parsedDistance;
}
Ankit
  • 17
  • 3
  • 4
    Show us what you've tried. Your question should have a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Denny Apr 25 '17 at 07:06
  • Your url is wrong, that's not how you use parameters in a String. Use `+` to concatenate strings. – Denny Apr 26 '17 at 08:41
  • I have used this also- https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=12.933049,77.613932&destinations=12.960460,77.646935&key=AIzaSyBP1g2YfJmsZ8zVhQBKffnGxL7TyqTYFxk" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving" – Ankit Apr 26 '17 at 08:43
  • Edit it in your code. – Denny Apr 26 '17 at 08:47
  • Also look here: http://stackoverflow.com/questions/14444228/android-how-to-draw-route-directions-google-maps-api-v2-from-current-location-t – Denny Apr 26 '17 at 08:53
  • thank u Denny. my above codes are correct? should I use that same codes?Also, lat1 and lon1 not fix. – Ankit Apr 26 '17 at 09:04
  • 1
    You should start start with a fresh example and look for more tutorials on the internet – Denny Apr 26 '17 at 09:08

1 Answers1

0

Find the solution for distance calculation :

double radlat1 = Math.PI * currentLat/ 180;
double radlat2 = Math.PI * mAtm[i].lat / 180;
double radlon1 = Math.PI * currentLng/ 180;
double radlon2 = Math.PI * mAtm[i].lon / 180;
double theta = lnglocation - mAtm[i].lon;
double radtheta = Math.PI * theta / 180;
double dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
                            dist = Math.acos(dist);
                            dist = dist * 180 / Math.PI;
                            dist = dist * 60 * 1.1515;

  DecimalFormat df = new DecimalFormat("####0.00");
  double dist = Double.parseDouble((df.format(dist * 1.609344)));
Rajasekhar
  • 2,345
  • 1
  • 13
  • 20