2
之间的距离计算两个地理点之间的距离,并返回距离阵列错误上计算下面的代码的两个地理点
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;
}
和gps2m是
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;
}
}
和活性显示的距离是如下
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);
}
}
它给NullPointer异常行的哪个因果关系g这是getDistance方法中的for循环来计算距离。
帮我看看哪里出问题了? 或任何其他方法来计算距离... 我需要帮助! 在此先感谢!
怎能'intent.getDoubleArrayExtra'工作,如果'意图=新意图()'? –
您可能还想看看静态['Location.distanceBetween'](http://developer.android.com/reference/android/location/Location.html#distanceBetween%28double,%20double,%20double, %20double,%20float%5b%5d%29)方法。不需要重新发明轮子。 :) –
哦!好点@Sherif !!将其更改为getIntent(); –