Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用ComputeDistanceBeween()?_Android_Google Maps - Fatal编程技术网

Android 如何使用ComputeDistanceBeween()?

Android 如何使用ComputeDistanceBeween()?,android,google-maps,Android,Google Maps,我使用谷歌地图Android API实用程序库来计算两点之间的距离,我看过文档,但我不知道我哪里做错了。两点之间测得的距离要大得多。下面是我的代码 ParseGeoPoint requesterLocation = object.getParseGeoPoint("requesterLocation"); LatLng from = new LatLng(userLocation.getLatitude(),userLocation.getLongitude()); LatLng to = ne

我使用谷歌地图Android API实用程序库来计算两点之间的距离,我看过文档,但我不知道我哪里做错了。两点之间测得的距离要大得多。下面是我的代码

ParseGeoPoint requesterLocation = object.getParseGeoPoint("requesterLocation");
LatLng from = new LatLng(userLocation.getLatitude(),userLocation.getLongitude());
LatLng to = new LatLng(requesterLocation.getLatitude(),requesterLocation.getLongitude());

//Double distanceInKm = userLocation.distanceInKilometersTo(object.getParseGeoPoint("requesterLocation"));
Double dis = SphericalUtil.computeDistanceBetween(from,to);
Double disOneDp = (double) Math.round(dis * 10) / 10;
listViewContent.add(String.valueOf(disOneDp) + " Km");  

结果是12627.6公里,应该是29.4公里


此处以米(m)为单位计算距离 尝试这些方法将帮助您找到基于半径的距离,如果您正在寻找公路距离,请尝试使用谷歌地图API获取距离


此处以米(m)为单位计算距离


我认为你实际上做错了。 这位官员说,

返回两个板条之间的距离,单位为米

结果以米为单位返回,因此您应该将其转换为公里,如下所示:

Double disInMeters = SphericalUtil.computeDistanceBetween(from,to);
Double disInKilometers = disInMeters / 1000;
// Double disOneDp = (double) Math.round(dis * 10) / 10;
listViewContent.add(String.valueOf(disInKilometers) + " Km"); 

那么,我不知道这有什么用。你能告诉我如何获得最小距离吗?如果你调用
Log.d(标记,“从“+from+”,到“+to)”
,你会在logcat上看到什么?我想问题在于你的坐标,因为你用正确的方法计算了
之间的距离,结果是米,仍然
12627.6/1000!=29.4
。我已经仔细检查了我正在做的所有事情good@pskink这是空中距离,比公路距离短得多,顺便说一句,我想要公路距离我试过了,我想这会返回空中距离,我想要最小距离roadDistance@dhami_ji你应该在你的问题中提到这一点。是空中距离还是公路距离?@dhami_ji问你了吗看到我发布的链接了吗?@pskink是的,我看到了,但我仍在努力使用它,但我认为这将为我提供道路distance@dhami_ji什么“这个”?这个答案?把它传给你的当前位置,然后是最终位置。@dhami_ji。?如果你有任何疑问,请告诉我。!
private float distance(double currentlatitude, double currentlongitude, double originLat, double originLon) {

    float[] results = new float[1];
    Location.distanceBetween(currentlatitude, currentlongitude, originLat, originLon, results);
    float distanceInMeters = results[0];

    return distanceInMeters;
}
Location startPoint=new Location("locationA");
startPoint.setLatitude(17.372102);
startPoint.setLongitude(78.484196);

Location endPoint=new Location("locationA");
endPoint.setLatitude(17.375775);
endPoint.setLongitude(78.469218);

double distance=startPoint.distanceTo(endPoint);
computeDistanceBetween(LatLng from, LatLng to)
Double disInMeters = SphericalUtil.computeDistanceBetween(from,to);
Double disInKilometers = disInMeters / 1000;
// Double disOneDp = (double) Math.round(dis * 10) / 10;
listViewContent.add(String.valueOf(disInKilometers) + " Km");