Android 如何找到两个地质点之间的距离?
上面的代码不起作用,我的距离是0.0公里? 同样在location类的构造函数中,字符串提供程序是什么意思。 在上面的代码中,我使用PointA和PointB作为提供程序 为什么上面的代码不起作用 先谢谢你 LOGCAT 05-09 17:48:56.144:信息/当前位置(1573):纬度0.0液化天然气0.0Android 如何找到两个地质点之间的距离?,android,geolocation,Android,Geolocation,上面的代码不起作用,我的距离是0.0公里? 同样在location类的构造函数中,字符串提供程序是什么意思。 在上面的代码中,我使用PointA和PointB作为提供程序 为什么上面的代码不起作用 先谢谢你 LOGCAT 05-09 17:48:56.144:信息/当前位置(1573):纬度0.0液化天然气0.0 05-09 17:48:56.155:信息/检查位置loc(1573):lat 54.4288665 lng 10.169366 double distance; Locatio
05-09 17:48:56.155:信息/检查位置loc(1573):lat 54.4288665 lng 10.169366
double distance;
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
试试看
更新:
如果你从GeoPoint获得纬度/经度,那么它们是微度。你必须乘以1e6 这不是答案,但请注意构造函数的签名是
位置(字符串提供程序)
i、 e.传递给构造函数的字符串应该是
LocationManager.GPS\u提供程序
、LocationManager.NETWORK\u提供程序
或LocationManager.PASSIVE\u提供程序
,而不是“A点”
和“B点”
这只是一个简单的片段,因为我没有看到一个完整而简单的解决方案,上面有地质点:
double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )
{
double nRadius = 6371; // Earth's radius in Kilometers
// Get the difference between our two points
// then convert the difference into radians
double nDLat = ToRad(nLat2 - nLat1);
double nDLon = ToRad(nLon2 - nLon1);
// Here is the new line
nLat1 = ToRad(nLat1);
nLat2 = ToRad(nLat2);
double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );
double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = nRadius * nC;
return nD; // Return our calculated distance
}
如果您需要仪表,只需直接返回dist[0]。如上所述,Location类就是一种方法。以下是我使用的代码:
public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
float [] dist = new float[1];
Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
return dist[0] * 0.000621371192f;
}
在本例中,pointA和pointB都是地质点类的实例。您确定latA/latB和lngA/lngB的值不同,并且在正确的范围内吗?你能提供你正在使用的值的例子吗?另外,请注意,
distance to
的结果以米为单位,而不是以公里为单位。是的。我正在转换成米。我已经公布了日志结果。对于不同的位置,我得到的值是不同的……您不需要转换为米,结果已经是米了。请发布完整的代码示例,因为我相信这里的问题不在您发布的代码中。像这样发布“distanceBetween(a,b)”代码片段不是答案。这里的问题是为什么locationA.distanceTo(locationB)
(据称)给出0米的结果?对于相同的输入,这将给出相同的结果。我认为使用的输入有问题,源于原始问题中未显示的代码。@user590849-您是否可能从GeoPoint获得lon/lat?这些单位是微度——你应该乘以1e6。
public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
float [] dist = new float[1];
Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
return dist[0] * 0.000621371192f;
}
Location locationA = new Location("point A");
locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointA.getLongitudeE6() / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);