Get the distance between two locations in android? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Get the distance between two locations in android? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

i need to get distance between two location, but i need to get distance like blue line in the picture. picure

I try next:

public double getDistance(LatLng LatLng1, LatLng LatLng2) {
    double distance = 0;
    Location locationA = new Location("A");
    Location locationB = new Location("B");
    distance = locationA.distanceTo(locationB);

    return distance;

but i get red line distance.


Use the Google Maps Directions API. You’ll need to request the directions over HTTP. You can do this directly from Android, or via your own server.

For example, directions from Montreal to Toronto:


You’ll end up with some JSON. In routes[].legs[].distance, you’ll get an object like this:

     "legs" : [
           "distance" : {
              "text" : "542 km",
              "value" : 542389

You can also get the polyline information directly from the response object.

We are here to answer your question about Get the distance between two locations in android? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji