给定移动设备在短时间内的实时位置数据,我如何才能沿着这条路获得更远的纬度/经度对,比如 2 英里,甚至更好,5 分钟的车程?
我看到我可以使用 Google 的 Roads API 捕捉到给定纬度/经度对的道路 https://developers.google.com/maps/documentation/roads/snap ,但这只会让我走到那一步。
这将在 Android 应用中。
最佳答案
给定一条表示为 List<LatLng>
的路线, 此函数计算路线上的点 distance
距离 origin
米在该路线上(使用 Google Maps API Utility Library 进行一些计算):
private LatLng extrapolate(List<LatLng> path, LatLng origin, float distance) {
LatLng extrapolated = null;
if (!PolyUtil.isLocationOnPath(origin, path, false, 1)) { // If the location is not on path non geodesic, 1 meter tolerance
return null;
}
float accDistance = 0f;
boolean foundStart = false;
List<LatLng> segment = new ArrayList<>();
for (int i = 0; i < path.size() - 1; i++) {
LatLng segmentStart = path.get(i);
LatLng segmentEnd = path.get(i + 1);
segment.clear();
segment.add(segmentStart);
segment.add(segmentEnd);
double currentDistance = 0d;
if (!foundStart) {
if (PolyUtil.isLocationOnPath(origin, segment, false, 1)) {
foundStart = true;
currentDistance = SphericalUtil.computeDistanceBetween(origin, segmentEnd);
if (currentDistance > distance) {
double heading = SphericalUtil.computeHeading(origin, segmentEnd);
extrapolated = SphericalUtil.computeOffset(origin, distance - accDistance, heading);
break;
}
}
} else {
currentDistance = SphericalUtil.computeDistanceBetween(segmentStart, segmentEnd);
if (currentDistance + accDistance > distance) {
double heading = SphericalUtil.computeHeading(segmentStart, segmentEnd);
extrapolated = SphericalUtil.computeOffset(segmentStart, distance - accDistance, heading);
break;
}
}
accDistance += currentDistance;
}
return extrapolated;
}
这里有一些测试:
List<LatLng> route = new ArrayList<>();
route.add(new LatLng(40, 4));
route.add(new LatLng(40.1, 4));
route.add(new LatLng(40.1, 4.1));
route.add(new LatLng(40.2, 4.1));
route.add(new LatLng(40.2, 4.2));
route.add(new LatLng(40.3, 4.2));
route.add(new LatLng(40.3, 4.3));
LatLng origin;
LatLng extrapolated;
origin = new LatLng(40.1, 4.05);
extrapolated = extrapolate(route, origin, 30000);
Log.e("Extrapolated1", "" + extrapolated); // lat/lng: (40.25517043951189,4.2)
origin = new LatLng(40.05, 4);
extrapolated = extrapolate(route, origin, 100);
Log.e("Extrapolated2", "" + extrapolated); // lat/lng: (40.05089932033549,4.0)
origin = new LatLng(40.05, 4);
extrapolated = extrapolate(route, origin, 50000);
Log.e("Extrapolated3", "" + extrapolated); // lat/lng: (40.300010207449226,4.261348349980259)
origin = new LatLng(40.05, 4);
extrapolated = extrapolate(route, origin, 100000);
Log.e("Extrapolated4", "" + extrapolated); // null (result out of the route)
关于android - 我怎样才能沿着道路推断得更远?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38497130/