Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:26:21+00:00 2026-06-18T02:26:21+00:00

I use distanceBetween() of Location class to calculate the distance between two points as

  • 0

I use distanceBetween() of Location class to calculate the distance between two points as follows:

private float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {

    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
    float [] dist = new float[1];
    Log.i("destination coordinates", "Latitude:" + lat2 + ", Longitude: " + lng2);
        Location.distanceBetween(lat1, lng1, lat2, lng2, dist);

    return dist[0] * 0.000621371192f;
}

Documentation says that distanceBetween() “Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.” However the difference between the result returned by distanceBetween() and real GPS device or Google Navigation app is pretty big. For example my method will return 6.2 miles while google maps shows 10 miles for the same location. I double check the coordinates of the starting point p1 and ending point p2 and they seems to be correct. Is that how distanceBetween() method works or Am I doing something wrong? And by the way Is there a way to use Google Place API to retrieve a distance as a JSON response?

Distance calculated by Google Maps: 6.1 miles

enter image description here

And the result of

Location.distanceBetween(41.742964, -87.995971, 41.811511, -87.967923, dist) is 

4.947700

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:26:22+00:00Added an answer on June 18, 2026 at 2:26 am

    To get a distance from a Google Maps I have used Google Directions API
    and JSON parser to retrieve the distance value:

    private double getDistanceInfo(double lat1, double lng1, String destinationAddress) {
                StringBuilder stringBuilder = new StringBuilder();
                Double dist = 0.0;
                try {
    
                destinationAddress = destinationAddress.replaceAll(" ","%20");    
                String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lng1 + "&destination=" + destinationAddress + "&mode=driving&sensor=false";
    
                HttpPost httppost = new HttpPost(url);
    
                HttpClient client = new DefaultHttpClient();
                HttpResponse response;
                stringBuilder = new StringBuilder();
    
    
                    response = client.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    InputStream stream = entity.getContent();
                    int b;
                    while ((b = stream.read()) != -1) {
                        stringBuilder.append((char) b);
                    }
                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                }
    
                JSONObject jsonObject = new JSONObject();
                try {
    
                    jsonObject = new JSONObject(stringBuilder.toString());
    
                    JSONArray array = jsonObject.getJSONArray("routes");
    
                    JSONObject routes = array.getJSONObject(0);
    
                    JSONArray legs = routes.getJSONArray("legs");
    
                    JSONObject steps = legs.getJSONObject(0);
    
                    JSONObject distance = steps.getJSONObject("distance");
    
                    Log.i("Distance", distance.toString());
                    dist = Double.parseDouble(distance.getString("text").replaceAll("[^\\.0123456789]","") );
    
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                return dist;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I calculate the distance between two points specified by latitude and longitude?
I am trying to calculate squared Euclidean distance between two 4d float vectors using
How can use google map API to calculate the distance between two places based
I'm trying to calculate the distance between two places user Core Location. I've found
I try to calculate the distance between two points (given the latitude/longitude of those
I use this code to calculate to distance between two gps positions. Problem is
I need to calculate distance between two locations in my android application. Then I
I wants calculate the road distance between Current location and the other location. Which
I want to calculate the distance between points in C# with the google maps
I need to calculate distance between two coordinates (lon,lat) on embedded system which doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.