【发布时间】:2014-11-06 09:33:29
【问题描述】:
我有一张地图,我在上面画了一个多边形。绘制初始多边形后,我将地图旋转一圈,一次旋转 1 度,在大约 5 秒内完成一整圈。
问题是我希望多边形保持在屏幕上的相同位置,而不是地图上。如果我在旋转地图时对多边形不做任何事情,那么多边形也会旋转。这不是所需的行为,我希望多边形在地图旋转时留在屏幕上的同一位置。
我已尝试使用以下方法来获取新的多点,但无法正常显示。我一直使用 0 和 135 的方位角作为测试,结果如下所示。
方法:
//degrees = counter-clockwise from north
private List<LatLng> getPolygonPoints(LatLng startPoint, int bearing) {
LatLng firstPoint = getPolyPoint(startPoint, 100, getPolyPointDifference(90, bearing));
LatLng fourthPoint = getPolyPoint(startPoint, 100, getPolyPointDifference(270, bearing));
LatLng secondPoint = getPolyPoint(firstPoint, 300, getPolyPointDifference(10, bearing));
LatLng thirdPoint = getPolyPoint(fourthPoint, 300, getPolyPointDifference(350, bearing));
return Arrays.asList(
startPoint,
firstPoint,
secondPoint,
thirdPoint,
fourthPoint,
startPoint
);
}
private int getPolyPointDifference(int fromNorth, int bearing) {
int[] compass = new int[360];
int currentBearing = 360 - bearing;
if (currentBearing == 360) {
currentBearing = 0;
}
for (int i = 0; i < 360; i++) {
compass[i] = currentBearing;
currentBearing++;
if (currentBearing >= 360) {
currentBearing = 0;
}
}
Log.i(LOG_TAG, "fromNorth: "+fromNorth+" return: "+compass[fromNorth]);
return compass[fromNorth];
}
//distance = meters
//degrees = counter-clockwise from north
private LatLng getPolyPoint(LatLng fromLocation, int distance, int degrees) {
double radians = (degrees * Math.PI) / 180;
double distanceX = distance * Math.sin(radians);
double distanceY = distance * Math.cos(radians);
double deltaLatitude = distanceY / 110540;
double deltaLongitude = distanceX / (111320 * Math.cos(fromLocation.latitude));
double pointLatitude = fromLocation.latitude + deltaLatitude;
double pointLongitude = fromLocation.longitude + deltaLongitude;
return new LatLng(pointLatitude, pointLongitude);
}
输出:
轴承 = 0
- 从北:90 返回:90
- 从北:270 返回:270
- 从北:10 返回:10
- 从北:350 返回:350
轴承 = 135
- 从北:90 返回:315
- 从北:270 返回:135
- 从北:10 返回:235
- 从北:350 返回:215
结果:
【问题讨论】:
标签: android google-maps polygon