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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:14:56+00:00 2026-05-26T19:14:56+00:00

The following code calculate the distance between two geo points and return the distance

  • 0

The following code calculate the distance between two geo points and return the distance array

public static double[] getDistance() {
    double[] distance=new double[20];
    Intent intent=new Intent();
    double latitude[]=intent.getDoubleArrayExtra("latitude");
    double longitude[]=intent.getDoubleArrayExtra("longitude");
    Log.v(TAG, "got latitude array");
    double getlat=intent.getDoubleExtra("geoLat", 0.0);

    double getlng=intent.getDoubleExtra("geoLng", 0.0);
          Log.v(TAG, "got location");
          for(int i=0;i<20;i++) {
   distance[i]=gps2m((float)getlat, (float)getlng, (float)latitude[i], (float)longitude[i]);
 }
return distance;
}

and gps2m is

  private static double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) {
    float pk = (float) (180/3.14169);

    float a1 = lat_a / pk;
    float a2 = lng_a / pk;
    float b1 = lat_b / pk;
    float b2 = lng_b / pk;

    float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);
    float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);
    float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
    double tt = Math.acos(t1 + t2 + t3);
    Log.v(TAG, "returning distance");
    return 6366000*tt;
}
}

and The activity display the distance is as follow

public class CalculateDistance extends Activity{
double Latitude;
private String tag="CalculateDistance";
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
setContentView(R.layout.distance_phone_no);
Intent intent= getIntent();
int value= intent.getIntExtra("clickedid",0);//item id when clicked on listview
Log.v(tag, ""+value);
double latitude[]=GetLatAndLng.getDistance();
Log.v(tag, "got distanc array");
for(int i=0;i<20;i++) 
    if(i==value)
        Latitude=latitude[i];
TextView tv=(TextView)findViewById(R.id.text11);
tv.setText(""+Latitude);
}
}

it giving NullPointer exception The line which causing this is the for loop in getDistance method to calculate distance.

Help me where it is going wrong?
Or any other method to calculate the distance…
I need Help!!!
Thanks in Advance!!!

  • 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-26T19:14:57+00:00Added an answer on May 26, 2026 at 7:14 pm

    here comes a customized snippet, hope this helps

    import static java.lang.Math.acos;
    import static java.lang.Math.cos;
    import static java.lang.Math.sin;
    import static java.lang.Math.toRadians;
    
    public static long getDistanceMeters(double lat1, double lng1, double lat2, double lng2) {
        double l1 = toRadians(lat1);
        double l2 = toRadians(lat2);
        double g1 = toRadians(lng1);
        double g2 = toRadians(lng2);
    
        double dist = acos(sin(l1) * sin(l2) + cos(l1) * cos(l2) * cos(g1 - g2));
        if(dist < 0) {
            dist = dist + Math.PI;
        }
        return Math.round(dist * 6378100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created the following code to calculate the duration between two timestamps which can
I'm trying to calculate a distance between two sets of coordinate points in an
I implemented the following code that use the data points in dat to calculate
I would like to calculate distance between two groups. I am very confused. I
I need to calculate the time complexity of the following code: for (i =
Please your opinion on the following code. I need to calculate the diff in
i am trying to calculate the distance between the statringpoint which hold the initial
I have the following code to calculate the execution time. $TimeTaken['start'] = microtime(); require_once
Given a function zipdistance(zipfrom,zipto) which calculates the distance (in miles) between two zip codes
I have following method that that calculates distance and returns it in miles: public

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.