如何在经纬度两点之间推进X距离(米)?
How to advance X distance (meters) between two points of latitude and longitude?
我在 Java 中模拟具有不同停靠点的折线,我知道这两点之间的距离(以米为单位),问题是,我必须以一定速度从 A 点到 B 点每秒 1 到 3 米,我需要每 15 分钟左右获取一次当前坐标,我该怎么做?
点之间的方式是直线,所有这些都是模拟的,不是在地图上发生的,我只需要每隔 X 次打印此信息,有帮助吗?
示例:
我有坐标:
LAT: 51.504870000000004 LNG: -0.21533000000000002
我必须以这样的速度去:
LAT: 51.50475 LNG: -0.21571
所以,我必须模拟我以 3 米秒的速度从 A 到 B,并且我需要知道我在这两点之间移动时的位置 (lat/lng)
还有一个question差不多,不同的是[=28=我做不到],是Java的应用。
我不是很清楚这个问题,但如果你想在给定距离后做某事,你可以使用这种方法
LatLng startPoint=new LatLng(51.504870000000004,-0.21533000000000002);
LatLng endPoin=new LatLng(51.50475,-0.21571);
Location loc1 = new Location("");
loc1.setLatitude(startPoint.latitude);
loc1.setLongitude(startPoint.longitude);
Location loc2 = new Location("");
loc2.setLatitude(endPoint.latitude);
loc2.setLongitude(endPoint.longitude);
float distance = loc1.distanceTo(loc2);
//instead of if you can use for loop
if (distance <= 100) {
//do something, like get the coordinates
} else {
//do something
}
所以你知道 latA, lngA, latB, lngB
。根据你的问题,我假设你知道速度,它是常数,v = 3 m/s。你可以得到开始时间LocalDateTime tA = LocalDateTime.now();
你想知道你在某个时刻的坐标tX
.
为此,我将引入 coefLat
和 coefLng
系数,用于将坐标转换为米和米。他们使用地球的平均半径并将度数转换为弧度:
double coefLat = 180 / Math.PI / 6371000;
double coefLng = coefLat / Math.cos(Math.PI * (latA + latB) / 360);
然后计算 Lat 和 Lng 轴的距离以及以米为单位的完整距离:
double distLat = (latB - latA) / coefLat;
double distLng = (lngB - lngA) / coefLng;
double dist = Math.sqrt(distLat * distLat + distLng * distLng);
double fullTime = dist / v; // Full time needed to pass from A to B in seconds
移动一段时间后,求出duration,得到当前坐标:
LocalDateTime tX = LocalDateTime.now(); // get current time
long dT = Duration.between(tA, tX).getSeconds(); // in seconds
double ratio = dT / fullTime;
double latX = latA + coefLat * ratio * distLat;
double lngX = lngA + coefLng * ratio * distLng;
另请参阅this答案
完整代码:
public class GetCurrentCoords {
public static void main(String[] args) {
LocalDateTime tA = LocalDateTime.now();
double latA = 51.504870000000004;
double lngA = -0.21533000000000002;
double latB = 51.50475;
double lngB = -0.21571;
double coefLat = 180 / Math.PI / 6371000;
double coefLng = coefLat / Math.cos(Math.PI * (latA + latB) / 360);
double distLat = (latB - latA) / coefLat; // meters
double distLng = (lngB - lngA) / coefLng; // meters
double dist = Math.sqrt(distLat * distLat + distLng * distLng);
System.out.println("distLat = " + distLat + "m; distLng = " + distLng + "m; full dist from A to B = " + dist + "m");
double v = 3;
double fullTime = dist / v; // seconds
System.out.println("full time from A to B = " + fullTime + "s");
// let's move for 4 seconds
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException ex) {
Logger.getLogger(GetCurrentCoords.class.getName()).log(Level.SEVERE, null, ex);
}
LocalDateTime tX = LocalDateTime.now();
long dT = Duration.between(tA, tX).getSeconds();
double ratio = dT / fullTime;
double latX = latA + coefLat * ratio * distLat;
double lngX = lngA + coefLng * ratio * distLng;
System.out.println("Moving " + dT + " seconds; latX = " + latX + "; lngX = " + lngX);
}
}
我在 Java 中模拟具有不同停靠点的折线,我知道这两点之间的距离(以米为单位),问题是,我必须以一定速度从 A 点到 B 点每秒 1 到 3 米,我需要每 15 分钟左右获取一次当前坐标,我该怎么做?
点之间的方式是直线,所有这些都是模拟的,不是在地图上发生的,我只需要每隔 X 次打印此信息,有帮助吗?
示例:
我有坐标:
LAT: 51.504870000000004 LNG: -0.21533000000000002
我必须以这样的速度去:
LAT: 51.50475 LNG: -0.21571
所以,我必须模拟我以 3 米秒的速度从 A 到 B,并且我需要知道我在这两点之间移动时的位置 (lat/lng)
还有一个question差不多,不同的是[=28=我做不到],是Java的应用。
我不是很清楚这个问题,但如果你想在给定距离后做某事,你可以使用这种方法
LatLng startPoint=new LatLng(51.504870000000004,-0.21533000000000002);
LatLng endPoin=new LatLng(51.50475,-0.21571);
Location loc1 = new Location("");
loc1.setLatitude(startPoint.latitude);
loc1.setLongitude(startPoint.longitude);
Location loc2 = new Location("");
loc2.setLatitude(endPoint.latitude);
loc2.setLongitude(endPoint.longitude);
float distance = loc1.distanceTo(loc2);
//instead of if you can use for loop
if (distance <= 100) {
//do something, like get the coordinates
} else {
//do something
}
所以你知道 latA, lngA, latB, lngB
。根据你的问题,我假设你知道速度,它是常数,v = 3 m/s。你可以得到开始时间LocalDateTime tA = LocalDateTime.now();
你想知道你在某个时刻的坐标tX
.
为此,我将引入 coefLat
和 coefLng
系数,用于将坐标转换为米和米。他们使用地球的平均半径并将度数转换为弧度:
double coefLat = 180 / Math.PI / 6371000;
double coefLng = coefLat / Math.cos(Math.PI * (latA + latB) / 360);
然后计算 Lat 和 Lng 轴的距离以及以米为单位的完整距离:
double distLat = (latB - latA) / coefLat;
double distLng = (lngB - lngA) / coefLng;
double dist = Math.sqrt(distLat * distLat + distLng * distLng);
double fullTime = dist / v; // Full time needed to pass from A to B in seconds
移动一段时间后,求出duration,得到当前坐标:
LocalDateTime tX = LocalDateTime.now(); // get current time
long dT = Duration.between(tA, tX).getSeconds(); // in seconds
double ratio = dT / fullTime;
double latX = latA + coefLat * ratio * distLat;
double lngX = lngA + coefLng * ratio * distLng;
另请参阅this答案
完整代码:
public class GetCurrentCoords {
public static void main(String[] args) {
LocalDateTime tA = LocalDateTime.now();
double latA = 51.504870000000004;
double lngA = -0.21533000000000002;
double latB = 51.50475;
double lngB = -0.21571;
double coefLat = 180 / Math.PI / 6371000;
double coefLng = coefLat / Math.cos(Math.PI * (latA + latB) / 360);
double distLat = (latB - latA) / coefLat; // meters
double distLng = (lngB - lngA) / coefLng; // meters
double dist = Math.sqrt(distLat * distLat + distLng * distLng);
System.out.println("distLat = " + distLat + "m; distLng = " + distLng + "m; full dist from A to B = " + dist + "m");
double v = 3;
double fullTime = dist / v; // seconds
System.out.println("full time from A to B = " + fullTime + "s");
// let's move for 4 seconds
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException ex) {
Logger.getLogger(GetCurrentCoords.class.getName()).log(Level.SEVERE, null, ex);
}
LocalDateTime tX = LocalDateTime.now();
long dT = Duration.between(tA, tX).getSeconds();
double ratio = dT / fullTime;
double latX = latA + coefLat * ratio * distLat;
double lngX = lngA + coefLng * ratio * distLng;
System.out.println("Moving " + dT + " seconds; latX = " + latX + "; lngX = " + lngX);
}
}