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 7431193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:15:26+00:00 2026-05-29T09:15:26+00:00

I am using this code to calculate the the distance between two points. I

  • 0

I am using this code to calculate the the distance between two points. I am using the Haversine formula

private double CalcDistance(Location Start, Location End) 
    {
        // TODO Auto-generated method stub
        double distance;
        double lat1 = Start.getLatitude();
        double lat2 = End.getLatitude();
        double lng1 = Start.getLongitude();
        double lng2 = End.getLongitude();
        double dLat = Math.abs(Math.toRadians(lat2-lat1));
        double dLng = Math.abs(Math.toRadians(lng2 - lng1));

        distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

        double c = 2 * Math.asin(Math.sqrt(distance));

        //Return the answer in Kilometre (6371km the mean radius of the earth)
        return c * 6371;
    }

The valus that is produced only changes if the Longitude of the Location is changed and stays the same if the Latitude is changed. Is there any reason my code only does this.

  • 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-29T09:15:26+00:00Added an answer on May 29, 2026 at 9:15 am

    Hey I’m not entirely sure, but just looking at your code you might have made an error, I dont understand it but perhaps you could change

    distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                    Math.sin(dLng/2) * Math.sin(dLng/2);
    

    to

    distance = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                    Math.sin(dLng/2) * Math.sin(dLng/2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this method to calculate distance between 2 coordinates http://code.google.com/apis/maps/articles/phpsqlsearch.html#findnearsql for example i
Distance between two points: sqrt((x1-x2)^2 + (y1-y2)^2) Is there a way to do this
I'm trying to calculate a distance between two sets of coordinate points in an
I'm currently using this code to calculate the sunrise / sunset times. (To be
I'm using this code, to make a request to a given URL: private static
I'm using this code to calculate the average of some numbers - note that
this code is supposed to calculate : p/4+v/8 using shift operations Data Segment Db
I would like to calculate distance between two groups. I am very confused. I
Using this code to zip a folder and it works perfect on small files
Im using this code for mysql connection $con = mysql_connect(localhost:/var/lib/mysql/mysql.sock, abc , xyz); if

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.