我想计算包含经度和纬度的每个点之间的总距离,这些点都存储在本地数据库中,所以方案是我要计算从点a到b&b到c&c到d的距离使用Google Place API
将点(纬度和经度)存储到数据库中所有点都从数据库中获取。
答案 0 :(得分:0)
您可以计算两个位置点之间的距离,而无需使用places api
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)
我不确定我是否理解此问题的范围。您是否需要考虑地球的曲率?还是只有2个随机点?
如果是第一个:由于无法帮助您,我可以向您显示此链接:
https://www.movable-type.co.uk/scripts/latlong.html
如果是后者:
假设1是纬度,2是经度,那么a1是点A的纬度。
在平面中计算2个点就像这样:
距离= from pymongo import MongoClient
import gridfs
db = MongoClient('mongodb://localhost:27017/').myDB
fs = gridfs.GridFS( db )
fileID = fs.put( open(('Test.pdf') ))
out = fs.get(fileID)
因此,要计算从D到E的距离,例如:sqrt((a1−b1)+(a2−b2))
答案 2 :(得分:0)
class DistanceCalculator
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "M") + " Miles\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "K") + " Kilometers\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, "N") + " Nautical Miles\n");
}
private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
double theta = lon1 - lon2;
double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist);
dist = Math.toDegrees(dist);
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
} else if (unit == "N") {
dist = dist * 0.8684;
}
return (dist);
}
}
}
答案 3 :(得分:0)
float[] distanceWidth = new float[2];
Location.distanceBetween(
startLatitude,
startLongitude,
endLatitude,
endLongitude,
distanceWidth);
//You will get float[] of results after Location.distanceBetween().
float distance = distanceHeight[0];
float distanceInKm = distance/1000;