0

I have 2 textbox in which i am providing source and destination address.

1) Source = SHAHIBAUG UNDER BRIDGE, Shahibagh, Ahmedabad, Gujarat

2) Destination = CG Road, Shreyas Colony, Navrangpura, Ahmedabad, Gujarat

Distance = 4.6 (From Google Map) && 2.6656852 (Using distanceTo method of Location)

final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "Fare", "test");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Location source_location = new Location("");
                        Location destination_location = new Location("");
                        Geocoder geoCode = new Geocoder(getApplicationContext());

                        source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLatitude());
                        source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLongitude());
                        source_location.set(source_location);

                        destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLatitude());
                        destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLongitude());
                        destination_location.set(destination_location);

                        float distance = source_location.distanceTo(destination_location)/1000;
                        System.out.println("Distance == " + distance);
                        kmVal = BigDecimal.valueOf(distance);
                        calculateFares();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (dialog!=null) {
                            dialog.dismiss();   
                        }
                    }
                }
            }).start();

I don't know why I am getting wrong distance. I have one doubt in my code that is I have used getFromLocationName method and in that I am passing 1 argument for getting only one result. Does that makes any difference? Anyone has any idea please kindly help.

Scorpion
  • 6,831
  • 16
  • 75
  • 123
  • try to calculate distance from source lat,lng and destination lat,lng – Mohammod Hossain Sep 03 '12 at 08:27
  • I need road distance of this 2 locations. Is it possible through the Geocoder and Location API available in android or have to use Google Maps. – Scorpion Sep 03 '12 at 09:03
  • check this http://stackoverflow.com/questions/7696112/gettting-different-distance-when-using-the-google-map-and-user-defined-function/7752341#7752341 – MKJParekh Sep 03 '12 at 09:16

2 Answers2

1

Google maps give you the Road distance, the Geocoder gives you the distance as the crow flies.

njzk2
  • 38,969
  • 7
  • 69
  • 107
0

Solved it by Using Google Maps API. Hope it will help other. It gives you accurate Distance.

final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "", "Calculating...");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        float distance = 0.0f;
                        StringBuffer url = new StringBuffer("http://maps.googleapis.com/maps/api/directions/json?origin=");
                        url.append(source_input.getText().toString().replace(" ", "").trim().toString());
                        url.append("&destination=");
                        url.append(destination_input.getText().toString().replace(" ", "").trim().toString());
                        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", ""));
                        }


                        kmVal = BigDecimal.valueOf(distance);
                        calculateFares();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (dialog!=null) {
                            dialog.dismiss();   
                        }
                    }
                }
            }).start();
Scorpion
  • 6,831
  • 16
  • 75
  • 123