-2

I am very new to android Google maps i write the code for find distance between two places:

private String getDistance(final String start, final String end) {

           new Thread(new Runnable() {
                @Override
                public void run() {
     float distance = 0.0f;
               StringBuffer url = new StringBuffer("http://maps.googleapis.com/maps/api/directions/json?origin=");
                 url.append(start.replace(" ", "").trim().toString());
                 url.append("&destination=");
                 url.append(end.replace(" ", "").trim().toString());
                 url.append("&mode=transit");
                 url.append("&language=en-EN");
                 url.append("&sensor=false");



                 DefaultHttpClient httpClient = new DefaultHttpClient();
                 HttpGet httpGet = new HttpGet(url.toString());
                 HttpResponse httpResponse = httpClient.execute(httpGet);
                 HttpEntity httpEntity = httpResponse.getEntity();
                 String line = EntityUtils.toString(httpEntity);

                 JSONObject jsonObject = new JSONObject(line);
                 JSONArray jsonarray = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs");

                 for (int i = 0; i < jsonarray.length(); i++) {
                     JSONObject obj = jsonarray.getJSONObject(i);
                     distance  = Float.valueOf(obj.getJSONObject("distance").getString("text").replace("km", ""));
                     System.out.println("Distance == " + obj.getJSONObject("distance").getString("text").replace("km", ""));
                 }


              BigDecimal   kmVal = BigDecimal.valueOf(distance);
              System.out.println("===========distance=============="+kmVal);

            } catch (Exception e) {
                e.printStackTrace();
            }
                }
                }).start();

            return distance  ;

    }

I am getting the Distance with differences of 30 km. I am getting 30 km extra instead of actual distance.

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
user1787493
  • 179
  • 1
  • 4
  • 10
  • 2
    The first thing you should do is separate out the different responsibilities in your code...fetching the data and calculating distance should be separate concerns. Then try running in the debugger – Mitch Wheat Nov 05 '12 at 08:33
  • ok how can i get currect distance? – user1787493 Nov 05 '12 at 08:35
  • by breaking the problem down, and debugging...like I just mentioned... – Mitch Wheat Nov 05 '12 at 08:36
  • my concern is i am getting distance in km but with 30k extara distance? – user1787493 Nov 05 '12 at 08:38
  • 1
    my concern is you are not listening to advice. – Mitch Wheat Nov 05 '12 at 08:38
  • So separate the code into two methods. One to get the data and one to calculate the distance then debug. Just like Mitch says. – Simon Nov 05 '12 at 08:39
  • you need to learn about threads, first. Why do you use a Thread, what do you expect will happen with your program, what does final mean, and then, what is the actual distance, how do you know the output is wrong ... – njzk2 Nov 05 '12 at 08:39
  • sorry for that i will fallow ur instructions – user1787493 Nov 05 '12 at 08:40
  • While we are raising concerns, I have one too. What's up with asking the same question, not once, not twice, but counting this one, **4 times**? [First](http://stackoverflow.com/q/13206451/450534), [Second](http://stackoverflow.com/q/13208501/450534) and [Third](http://stackoverflow.com/q/13208946/450534). They all seem to be different, but essentially, for the same purpose. – Siddharth Lele Nov 05 '12 at 09:12

2 Answers2

0

You don't have to query the google maps to get the distance between two places. If you know the latitude and longitude of both places you can just construct Location objects and use it's distanceTo() method which will give you the exact distance in meters.

Location from = new Location("");
from.setLatitude(yourStartLatitude);
from.setLongitude(yourStartLongitude);

Location to = new Location("");
to.setLatitude(yourEndLatitude);
to.setLongitude(yourEndLongitude);

float distance = from.distanceTo(to); // The distance in meters
Adam Monos
  • 4,287
  • 1
  • 23
  • 25