我想在android中获取一个形状的中心。形状在地图上由手绘制。我拥有所有的坐标,但平均值比我想象的要复杂。我想在平均经度和纬度上绘制一个标记。谷歌地图中经纬度点集群的中心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));
}
您所描述的方法应该可行,但可能在经度接近国际日期的情况下,或者位置靠近极点的情况下。也许你应该分享你的代码。 – JerryM