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

  • Home
  • SEARCH
  • 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 6237765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:05:29+00:00 2026-05-24T11:05:29+00:00

Here my code I used below code to calculate the distance between two location

  • 0

Here my code I used below code to calculate the distance between two location using their latitude and longitude. It is giving wrong distance. sometimes getting right and sometimes getting irrelevant distance.

We are getting lat1 and lng1 from database.

//getting lat2 and lng2 from GPS as below

public class MyLocationListener implements LocationListener {

  @Override
  public void onLocationChanged(Location loc)
  {
    lat2=loc.getLatitude();
    lng2=loc.getLongitude();
    String Text = "My current location is: " +"Latitud = "+ loc.getLatitude() +"Longitud = " + loc.getLongitude();

    //System.out.println("Lat & Lang form Loc"+Text);
    //Toast.makeText( getApplicationContext(), Text,Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onProviderDisabled(String provider)
  {
  }

  @Override
  public void onProviderEnabled(String provider)
  {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras)
  {
  }


  //Calculating distance
  double earthRadius = 3958.75;

  double dLat = Math.toRadians(lat1-lat2);
  double dLng = Math.toRadians(lng1-lng2);
  double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
             Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(lat1)) *
             Math.sin(dLng/2) * Math.sin(dLng/2);
  double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  double dist = earthRadius * c;
  • 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-05-24T11:05:30+00:00Added an answer on May 24, 2026 at 11:05 am

    As shown in the article at https://dzone.com/articles/distance-calculation-using-3 and in the answer at Calculating distance between two points, using latitude longitude? —

    private double distance(double lat1, double lon1, double lat2, double
    lon2) {
        double theta = lon1 - lon2;
        double dist = Math.sin(deg2rad(lat1)) 
                        * Math.sin(deg2rad(lat2))
                        + Math.cos(deg2rad(lat1))
                        * Math.cos(deg2rad(lat2))
                        * Math.cos(deg2rad(theta));
        dist = Math.acos(dist);
        dist = rad2deg(dist);
        dist = dist * 60 * 1.1515;
        return (dist); }
    
    private double deg2rad(double deg) {
        return (deg * Math.PI / 180.0); }
    
    private double rad2deg(double rad) {
        return (rad * 180.0 / Math.PI); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code currently used. public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource
here the following code is used to view the present modal view controller. [[self
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
i used the code below for my listview and textview . Code: textcontent.setText(Html.fromHtml(item.get_text())); textcontent.setAutoLinkMask(Linkify.WEB_URLS);
I am trying out WebDriverBacked selenium. I used the below code. But, it gives
Can anyone enlighten me as to the differences between the two statements below. [self
Below are 2 code snippets used to check if a folder exists in a
I have two related qestions on the code included below 1) I am trying
I used below code to insert a new info to a existing contacts, it's
I displayed Image from the web. For xml I used below code: main.xml: <com.imloader.LoaderImageView

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.