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!!!
here comes a customized snippet, hope this helps