0

Possible Duplicate:
J2ME/Android/BlackBerry - driving directions, route between two locations

I want to calculate distance between two geopoints in android. I used Location.distanceTo and Location.distanceBetween but this method return DIRECT length between two points. I want to calculate the route between two points and then calculate the total distance between all route points.

I don't want to show points or route on map. I just want to calculate the distance from current location to given points nothings else.

ie. I want to calculate the Route length between two geo points. Can anyone help me to calculate the route length between two geo points?

Community
  • 1
  • 1
Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76

3 Answers3

2

I found my answer I used this Google api and this will return all the data about route.

API : Google Api for Route Calculation

You can find more info on Google Map Api

thank you all for your support.

Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
0

This method returns distance:

public double distance(float latA,float longA,float latB,float longB){
        double d2r = Math.PI / 180;



            double dlong = (longA - longB) * d2r;
            double dlat = (latA - latB) * d2r;
            double a = Math.pow(Math.sin(dlat / 2.0), 2)
                    + Math.cos(latB * d2r) * Math.cos(latA * d2r)
                    * Math.pow(Math.sin(dlong / 2.0), 2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            double d = 6367 * c;

            return d;



}

But this is to find out displacement. And no idea about how to calculate distance between all route points. May be you can use this method and loop it.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

I think what you are looking for is in this answer from a similar question

public class MapRouteActivity extends MapActivity {    
 LinearLayout linearLayout;
 MapView mapView;
 private Road mRoad;    
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mapView = (MapView) findViewById(R.id.mapview);
  mapView.setBuiltInZoomControls(true);    
  new Thread() {
   @Override
   public void run() {
    double fromLat = 49.85, fromLon = 24.016667; 
    double toLat = 50.45, toLon = 30.523333;
    String url = RoadProvider
      .getUrl(fromLat, fromLon, toLat, toLon);
    InputStream is = getConnection(url);
    mRoad = RoadProvider.getRoute(is);
    mHandler.sendEmptyMessage(0);
   }
  }.start();
 }

 Handler mHandler = new Handler() {
  public void handleMessage(android.os.Message msg) {
   TextView textView = (TextView) findViewById(R.id.description);
   textView.setText(mRoad.mName + " " + mRoad.mDescription);
   MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
   List<Overlay> listOfOverlays = mapView.getOverlays();
   listOfOverlays.clear();
   listOfOverlays.add(mapOverlay);
   mapView.invalidate();
  };
 };

 private InputStream getConnection(String url) {
  InputStream is = null;
  try {
   URLConnection conn = new URL(url).openConnection();
   is = conn.getInputStream();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return is;
 }    
 @Override
 protected boolean isRouteDisplayed() {
  return false;
 }
}
Community
  • 1
  • 1
SERPRO
  • 10,015
  • 8
  • 46
  • 63