我的项目中有GoogleMap。它设置为缩放级别21.我想绘制一条长度为5米且具有特定标题的线。我用了这段代码:
private LatLng drawHeadingOnMap(LatLng centre, double radius, double heading)
{
double EARTH_RADIUS = 6378100.0;
// Convert to radians.
double lat = Math.toRadians(centre.latitude );
double lon = Math.toRadians(centre.longitude);
// y
double latPoint = lat + (radius / EARTH_RADIUS) * Math.sin(Math.toRadians(heading));
// x
double lonPoint = lon + (radius / EARTH_RADIUS) * Math.cos( Math.sin(Math.toRadians(heading)) / Math.cos(lat));
LatLng point =new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI);
return point;
}
我通过以下方式运行:
LatLng ll = drawHeadingOnMap(origin, 5, 90);
LatLng lll = drawHeadingOnMap(origin, 5, 0);
googleMap.addPolyline(new PolylineOptions().add(Mabda).add(ll).color(Color.BLUE).width(3));
googleMap.addPolyline(new PolylineOptions().add(Mabda).add(lll).color(Color.BLUE).width(3));
非常好地画0度!!但其他人错了。例如,这张照片显示在上面的代码中:
当我想画90度时,它会像这张照片一样画出来! 90后,它回到0度(当我写drawHeadingOnMap(原点,5,180)时,它绘制0度!)。我该如何解决?我很困惑!!! ...
更新: 我尝试了原产地=(12,12)...... 我得到了这个结果:
ll.Latitude = 12.000898320495335
ll.Longitude = 12.00046835742835
lll.latitude = 12.0
lll.longitude = 12.000898320495335
ll是(12,12)在90度方向上移动1米的结果。 lll是在0度方向上移动(12,12)1米的结果。 该方法适用于0度......
答案 0 :(得分:1)
如果你有一个中心点(10,20),并且想要找到半径为5的20度的另一个点(x,y),你可以进行以下数学运算:
x = 10 + 5 * Math.sin(Math.toRadians(20));
y = 20 + 5 * Math.cos(Math.toRadians(20));
不确定为什么你为你的lonPoint做了Math.cos( Math.sin(Math.toRadians(heading)) / Math.cos(lat))
。
答案 1 :(得分:0)
为了理解精确的数学,我建议阅读this nice blog post。
如果您只需要一个有效的实现,请使用此功能(从this link采用):
/**
* @param loc location to transale (creates a copy)
* @param distance in meters
* @param heading in degrees, where 0 is NORTH, clockwise
* @return new location
*/
public static LatLng translate(LatLng loc, double distance, double heading){
double EARTH_RADIUS = 6378100.0;
heading = Math.toRadians(heading);
distance = distance/EARTH_RADIUS;
// http://williams.best.vwh.net/avform.htm#LL
double fromLat = Math.toRadians(loc.latitude);
double fromLng = Math.toRadians(loc.longitude);
double cosDistance = Math.cos(distance);
double sinDistance = Math.sin(distance);
double sinFromLat = Math.sin(fromLat);
double cosFromLat = Math.cos(fromLat);
double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
double dLng = Math.atan2(
sinDistance * cosFromLat * Math.sin(heading),
cosDistance - sinFromLat * sinLat);
return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
}