【发布时间】:2016-04-21 20:57:28
【问题描述】:
我正在尝试在 android 中获取形状的中心。形状是在地图上手工绘制的。我有所有的坐标,但平均值比我想象的要复杂。我想在平均纬度和经度上绘制一个标记。
我尝试过分别将纬度和经度相加,然后除以点数。这并没有给出正确的答案。标记似乎总是落后于绘图人物。我也尝试过使用该实现,但它给出了相同的答案,之前的 SO 问题,Calculate the center point of multiple latitude/longitude coordinate pairs
我一直在使用的代码:
private void calculateFreeHandPolygonParameters(){
double xValues = 0;
double yValues = 0;
double zValues = 0;
int count = 0;
// linesForPolygon a list of the lines in the polygon
for(Polyline line : linesForPolygon){
for (LatLng point : line.getPoints()) {
xValues += Math.cos(Math.toRadians(point.latitude)) * Math.cos(Math.toRadians(point.longitude));
yValues += Math.cos(Math.toRadians(point.latitude)) * Math.sin(Math.toRadians(point.longitude));
zValues += Math.sin(Math.toRadians(point.latitude));
count++;
}
}
double meanX = xValues/count;
double meanY = yValues/count;
double meanZ = zValues/count;
double centralLongitude = Math.atan2(meanY, meanX);
double centralSquareRoot = Math.sqrt(Math.pow(meanX, 2) + Math.pow(meanX, 2) + Math.pow(meanX, 2));
double centralLatitude = Math.atan2(meanZ, centralSquareRoot);
double latitude = Math.toDegrees(centralLatitude);
double longitude = Math.toDegrees(centralLongitude);
Log.i("MAPS", "Freehand Parameters: x mean -> " + latitude + " y mean -> " + longitude);
testMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Polygon center")
.snippet("lat: " + latitude + " long: " + longitude));
}
【问题讨论】:
-
您描述的方法应该有效,除非经度靠近国际日期变更线,或者位置靠近两极。也许你应该分享你的代码。
标签: android google-maps coordinates