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?
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.
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.
(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.
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:
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.
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.
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.
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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With