2012-01-17 88 views
1

我想把经度和纬度传递给另一个活动,并检查这个通过的坐标和当前的坐标之间的距离。在设置纬度和经度的问题android

在第一个活动:

GeoPoint p1 = mapView.getProjection().fromPixels((int) event.getX(),(int event.getY()); 
setter(p1.getLatitudeE6()/ 1E6, p1.getLongitudeE6() /1E6); 

public void setter(Double lati,Double longi) 
    { 
     latitude=lati; 
     longitude=longi; 
    } 

上的按钮单击事件我与捆绑的帮助下通过这一点。这工作正常。

在第二个活动:

public Location selected_location=null; 
Double lati,longi; 

Bundle b=this.getIntent().getExtras(); 
     lati=b.getDouble("latitude"); 
     longi=b.getDouble("longitude"); 

直到这么多它工作正常。我甚至打印了这些值。真正的问题是下面给出的线:

selected_location.setLatitude(lati); 
selected_location.setLongitude(longi); 

我想设置传递的纬度和经度值到位置变量。但是这导致活动终止。

如果可能的话,请提出解决方案。如果问题太幼稚,请忽略。

+0

当您收到此异常时,您可以发布LogCat输出吗?编辑:鲍里斯发布你需要的东西。 – bschultz 2012-01-17 17:29:00

+0

我把行 selected_location.setLatitude(lati); selected_location.setLongitude(longi);在try catch块内。我得到的例外是空指针异常 – 2012-01-17 17:51:46

回答

1

如果您的目标只是计算您不需要构造位置对象的距离,请使用此method。它是静态的,可以处理长期和纬度值。如果你把异常的堆栈跟踪,我也可以帮助调试错误。

编辑请求的例子:

float myGetDistance(double startLatitude, double startLongitude, double endLatitude, double endLongitude) { 
    float [] results = new float[1]; // You need only the distance, thus only one element 
    Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results); 
    return results[0]; 
} 
+0

先生,我对这个功能有点困惑。例如,在我们使用distanceTo方法的情况下,firstlocation.distanceTo(secondlocation)。你可以用这个distanceBetween方法显示一个简单的例子 – 2012-01-17 17:42:35

0

可以完成它给出了两个点坐标之间的距离是这样的:

final float[] results= new float[1]; 
// The computed distance in meters is stored in results[0]. 
// If results has length 2 or greater, the initial bearing is stored in results[1]. 
// If results has length 3 or greater, the final bearing is stored in results[2]. 
Location.distanceBetween(refLat, refLong, latitude, longitude, results); 
final float distance = results[0]; // meter! 

您可以重用后计算的结果阵列。如果您需要方位信息,请使用声明大小为3的结果数组,如果您不需要它,请使用大小1并以这种方式节省计算不需要的信息的时间。