我正在使用谷歌地图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 km,应该是29.4km
答案 0 :(得分:1)
以千米(km)为距离
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);}
答案 1 :(得分:0)
这里以米(m)为单位获取距离 尝试这些方法将帮助您找到基于半径的距离,如果您正在寻找远距离尝试使用谷歌地图API距离GOOGLE MAP MATRIX API , ROAD BASED DISTANCE AND TIME TUTORIAL。!
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;
}
这里以米(m)为距离
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);
答案 2 :(得分:0)
我认为您实际上做错了。 据官方Google map utils documentation表示;
computeDistanceBetween(LatLng from, LatLng to)
以米为单位返回两个LatLng之间的距离。
结果以米为单位返回,因此您应该像这样将其转换为公里;
Double disInMeters = SphericalUtil.computeDistanceBetween(from,to);
Double disInKilometers = disInMeters / 1000;
// Double disOneDp = (double) Math.round(dis * 10) / 10;
listViewContent.add(String.valueOf(disInKilometers) + " Km");