我正在开发Android
MapView
和开发基于地图的应用程序。我需要找到与特定的 X 距离Co-ordinates
。方向不是我的首要任务距离是我的首要任务可以说我需要从特定位置找到 100 米关于如何做到这一点的任何想法提前感谢您的阅读和回答。
问问题
2085 次
2 回答
8
为了计算在距原点给定距离的线上找到一个点,您需要有方位(或方向)以及距离。这是一个函数,它将获取起始位置、方位角和距离(深度)并返回目标位置(对于 Android):您可能希望将其从 KM 转换为 Meters 或其他任何值。
public static Location GetDestinationPoint(Location startLoc, float bearing, float depth)
{
Location newLocation = new Location("newLocation");
double radius = 6371.0; // earth's mean radius in km
double lat1 = Math.toRadians(startLoc.getLatitude());
double lng1 = Math.toRadians(startLoc.getLongitude());
double brng = Math.toRadians(bearing);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(depth/radius) + Math.cos(lat1)*Math.sin(depth/radius)*Math.cos(brng) );
double lng2 = lng1 + Math.atan2(Math.sin(brng)*Math.sin(depth/radius)*Math.cos(lat1), Math.cos(depth/radius)-Math.sin(lat1)*Math.sin(lat2));
lng2 = (lng2+Math.PI)%(2*Math.PI) - Math.PI;
// normalize to -180...+180
if (lat2 == 0 || lng2 == 0)
{
newLocation.setLatitude(0.0);
newLocation.setLongitude(0.0);
}
else
{
newLocation.setLatitude(Math.toDegrees(lat2));
newLocation.setLongitude(Math.toDegrees(lng2));
}
return newLocation;
};
于 2013-01-15T07:20:07.260 回答
1
只是为了将 javram 的答案变成米和弧度而不是度数。
/**
* Create a new location specified in meters and bearing from a previous location.
* @param startLoc from where
* @param bearing which direction, in radians from north
* @param distance meters from startLoc
* @return a new location
*/
public static Location createLocation(Location startLoc, double bearing, double distance) {
Location newLocation = new Location("newLocation");
double radius = 6371000.0; // earth's mean radius in m
double lat1 = Math.toRadians(startLoc.getLatitude());
double lng1 = Math.toRadians(startLoc.getLongitude());
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(distance / radius) + Math.cos(lat1) * Math.sin(distance / radius) * Math.cos(bearing));
double lng2 = lng1 + Math.atan2(Math.sin(bearing) * Math.sin(distance / radius) * Math.cos(lat1), Math.cos(distance / radius) - Math.sin(lat1) * Math.sin(lat2));
lng2 = (lng2 + Math.PI) % (2 * Math.PI) - Math.PI;
// normalize to -180...+180
if (lat2 == 0 || lng2 == 0) {
newLocation.setLatitude(0.0);
newLocation.setLongitude(0.0);
} else {
newLocation.setLatitude(Math.toDegrees(lat2));
newLocation.setLongitude(Math.toDegrees(lng2));
}
return newLocation;
}
于 2015-05-31T11:50:55.730 回答