我想在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));
}
答案 0 :(得分:1)
答案 1 :(得分:1)
这是我的质心代码:
public class PolygonCentroid {
private List<GeoPoint> points;
private int pointsSize;
public PolygonCentroid(List<GeoPoint> points) {
this.points = points;
this.pointsSize = points.size();
}
protected double polygonArea() {
double area = 0;
for (int i = 0, j; i < pointsSize; i++) {
j = (i + 1) % pointsSize;
area += points.get(i).getLongitude() * points.get(j).getLatitude();
area -= points.get(i).getLatitude() * points.get(j).getLongitude();
}
area /= 2.0;
return area;
}
public GeoPoint centroid() {
double cx = 0, cy = 0;
double factor;
for (int i = 0, j; i < pointsSize; i++) {
j = (i + 1) % pointsSize;
factor = (points.get(i).getLongitude() * points.get(j).getLatitude() - points.get(j).getLongitude() * points.get(i).getLatitude());
cx += (points.get(i).getLongitude() + points.get(j).getLongitude()) * factor;
cy += (points.get(i).getLatitude() + points.get(j).getLatitude()) * factor;
}
double A = polygonArea();
factor = 1.0 / (6.0 * A);
cx *= factor;
cy *= factor;
return new GeoPoint(cy, cx);
}
}
另外,请注意质心可以在多边形之外: