Reputation: 77
I'm looking for a solution that calculates the center between several latitude-longitude coordinates (for example, to simply center a map to the center of a google-maps polygon).I m storing latlang coordinates into arraylist.
Upvotes: 6
Views: 2111
Reputation: 14918
this one worked for me
LatLng getCenterPointInPolygon(Polygon polygon) {
LatLngBounds.Builder latLngBounds = LatLngBounds.builder();
for (LatLng latLng : polygon.getPoints()) {
latLngBounds.include(latLng);
}
return latLngBounds.build().getCenter();
}
Upvotes: 2
Reputation: 38223
Use the LatLngBounds.Builder
.
Once you create an instance keep adding LatLng
s to it. Finally build()
it and call getCenter()
on the resulting LatLngBounds
object.
You can only add positions to the builder, it cannot subtract.
Upvotes: 8
Reputation: 2203
Basic 3D Calculus. Build a function that receives a point and outputs the sum of the distances to each position on the map. Then, use calculus to find the point where the function reaches its minimum. That is the center.
Upvotes: 0