Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate the center of a polygon in Google Maps Android API v2?

I have drawn a polygon on google map using several Latitude,Longitude points.But now I need to place a marker at the center of the polygon for which I need the center coordinates.How do I calculate the center point.

Below is the code for adding polygons on map:

for (Warning w : warningsList) {
            // Instantiates a new Polygon object and adds points
            PolygonOptions rectOptions = new PolygonOptions();
            List<PolyPoints> pp = w.getPolyPoints();
            for (PolyPoints p : pp) {
                rectOptions.add(new LatLng(Double.valueOf(p.getLatitude()),
                        Double.valueOf(p.getLongitude())));
            }

                mMap.addPolygon(rectOptions.strokeColor(Color.GREEN)
                        .fillColor(Color.RED).strokeWidth(STROKE_WIDTH));

        }

I found similar question which has been answered but thats for JavaScript Api.Is their any way of using the same solution in my case?

like image 930
Ansh Avatar asked Aug 26 '13 09:08

Ansh


People also ask

How do I calculate the area of a polygon in Google Maps?

Right-click on the map at your starting point and choose the Measure distance option. Add points around the location's boundary. Once you close the shape by clicking on the starting point, the Google Maps area calculator will automatically process the area of your shape.

How do I extract a polygon from Google Maps?

In Google Earth, create the polygon that you wish to bring into RockWorks. Or, locate the polygon that currently exists in your Saved Places listing. Right-click on the item, and choose Copy from the pop-up menu. Or, right-click on the item, and choose Save Place As to save the locations in a KMZ file.

How the Google map application makes use of coordinate geometry in its functioning?

(Google uses the World Geodetic System WGS84 standard.) World coordinates, which reference a point on the map uniquely. Pixel coordinates, which reference a specific pixel on the map at a specific zoom level. Tile coordinates, which reference a specific tile on the map at a specific zoom level.

How to get the center of a polygon using Google Maps API?

Matthew's answer is a good solution. However, when using the Google Maps API v3, you might want to pass each point of the polygon to a LatLngBounds object through the extend () method, and then finally call the getCenter () method on the LatLngBounds object. Consider the following example:

What are polylines and circles in Android maps?

You've built an Android app containing a Google map, with polygons to represent areas on the map and polylines to represent routes or other connections between locations. You've also learned how to use the Maps SDK for Android . Learn about the Circle object. Circles are similar to polygons but have properties that reflect the shape of a circle.

How to add polygons to represent areas on the map?

Add polygons to represent areas on the map. A Polygon is a shape consisting of a series of coordinates in an ordered sequence, similar to a Polyline. The difference is that polygon defines a closed area with a fillable interior, while a polyline is open ended. Create a PolygonOptions object and add points to it.

How to display a Google Maps-based API on Android?

The API key for Google Maps-based APIs. Display a map, using the Maps SDK for Android. Add a <fragment> element to your activity's layout file, activity_maps.xml. This element defines a SupportMapFragment to act as a container for the map and to provide access to the GoogleMap object.


2 Answers

Below code I am using to find the center point of the polygon. its working for me too

private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){
        LatLng centerLatLng = null;
        Builder builder = new LatLngBounds.Builder(); 
        for(int i = 0 ; i < polygonPointsList.size() ; i++) 
        {          
            builder.include(polygonPointsList.get(i));
        }
        LatLngBounds bounds = builder.build();
        centerLatLng =  bounds.getCenter();

        return centerLatLng;
    }
like image 143
Thinesh Avatar answered Sep 23 '22 01:09

Thinesh


Below is the code which I am using now to find the center of polygon:-

public static double[] centroid(List<PolyPoints> points) {
        double[] centroid = { 0.0, 0.0 };

        for (int i = 0; i < points.size(); i++) {
            centroid[0] += points.get(i).getLatitude();
            centroid[1] += points.get(i).getLongitude();
        }

        int totalPoints = points.size();
        centroid[0] = centroid[0] / totalPoints;
        centroid[1] = centroid[1] / totalPoints;

        return centroid;
    }
like image 37
Ansh Avatar answered Sep 26 '22 01:09

Ansh