Skip to content
Advertisement

Find centerpoint of polygon in JavaScript

I have a “place” object from Google Maps which has a set of coordinates that represent a bounding box for a given location, say London. Each set of coordinates has a latitude and longitude.

I have written the below code to find the centerpoint, but I am not sure if it does actually produce the centerpoint. What if the polygon has 5 points instead of 4? Also, can this be done in a more efficient way, with less operations?

JavaScript

Example.

Advertisement

Answer

This should get the centroid of the area of any polygon

JavaScript

On jsfiddle

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement