user3454590
user3454590

Reputation: 77

calculate center point of multiple latlang in android google map?

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

Answers (3)

saigopi.me
saigopi.me

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

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

Use the LatLngBounds.Builder.

Once you create an instance keep adding LatLngs 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

SalmonKiller
SalmonKiller

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

Related Questions