アンドロイドで図形の中心を取得しようとしています。図形は地図上に手で描かれています。私はすべてのコーディネイトを持っていますが、平均値は思ったよりも厄介なものになっています。私は平均緯度と経度にマーカーをプロットしたいと思います。Googleマップの緯度と経度の点の中心点
私は緯度と経度を別々に合計してから、ポイント数で除算しようとしました。これは正しい答えを出すものではありません。マーカーは、常に描画図の後ろにあるようです。私はまた、実装を使用して試してみましたが、それは同じ答え、前の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