60

It doesn't need to be 100% correct, it can be the center of the bounding rectangle.

| |
67

Algorithm:

Run through all the points in the polygon. For all the points find;

  • x1, the lowest x coordinate
  • y1, the lowest y coordinate
  • x2, the highest x coordinate
  • y2, the highest y coordinate

You now have the bounding rectangle, and can work out the center using:

center.x = x1 + ((x2 - x1) / 2);
center.y = y1 + ((y2 - y1) / 2);
| |
  • 7
    Well, i've found that in v2, I was able to do it just by calling polygon.getBounds().getCenter(), but I guess i'll have to do it your way. Thanks – ANd Jun 20 '10 at 22:41
  • I have no idea, that may work in v3 too. I havn't looked too closely at the Google Maps API. But this way does essentially that anyway. – Matthew Scharley Jun 20 '10 at 23:24
  • It looks like Google forgot the Polygon.getBounds() method. It is available for Circle and Rectangle, but not for the Polygon class. – Daniel Vassallo Jun 21 '10 at 5:29
  • 2
    This will fail horrifically if the polygon crosses the international date line (example: Russia's international boundary). – Canuck Jul 12 '16 at 20:00
  • As @Canuk stated, this will fail in certain circumstances. Ideally you want to use the mean of circular quantities, and not a arithmetic mean. en.wikipedia.org/wiki/Mean_of_circular_quantities – martin Sep 18 '16 at 22:09
202

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:

var bounds = new google.maps.LatLngBounds();
var i;

// The Bermuda Triangle
var polygonCoords = [
  new google.maps.LatLng(25.774252, -80.190262),
  new google.maps.LatLng(18.466465, -66.118292),
  new google.maps.LatLng(32.321384, -64.757370),
  new google.maps.LatLng(25.774252, -80.190262)
];

for (i = 0; i < polygonCoords.length; i++) {
  bounds.extend(polygonCoords[i]);
}

// The Center of the Bermuda Triangle - (25.3939245, -72.473816)
console.log(bounds.getCenter());
| |
  • 3
    Wrapped up nicely here: code.google.com/p/google-maps-extensions/source/browse/… – Steve Feb 19 '11 at 23:01
  • 4
    Why is this the better solution? To me it seems that both solutions should always get to the same answer. Is this faster? – Nicolas Jul 5 '13 at 20:55
  • 4
    @Nicolas: Both will get to the same solution, but this is simpler to implement, relies on Googles API's and is more obvious what it does at a glance thanks to being shorter in code (4 lines at worst). Either method should have negligible runtime, so speed isn't an issue. – Matthew Scharley Nov 3 '13 at 8:06
  • I getting the following Error with the above code. TypeError: a is undefined – ArunRaj Jan 17 '14 at 16:15
  • 5
    Also, this is a better solution as it will take into account the curvature of the Earth. The trivial algorithm I posted will be a reasonable approximation for small areas, but if you are trying to work out the geographical centre of a region for instance, then this will give a better answer. The world isn't rectangular, only maps are. – Matthew Scharley Jul 24 '14 at 9:23
49

you can extend the Polygon class with your own version of the missing function, let's call it my_getBounds() :

google.maps.Polygon.prototype.my_getBounds=function(){
    var bounds = new google.maps.LatLngBounds()
    this.getPath().forEach(function(element,index){bounds.extend(element)})
    return bounds
}

and than use it in code like this :

myPolygon.my_getBounds().getCenter()

... etc, it should be equivalent to the v2 behavior

| |
  • 2
    Found this posting when doing a google search for the same issue.. This suggestion worked out perfectly for me! thanks.. – Dennis Jan 9 '13 at 11:57
  • 3
    wow it's work like a charm!! the best solution I think, Thanks @furiozo – chespinoza Jan 25 '13 at 15:20
  • For some reason, this doesn't work for me. the my_getBounds() function is undefined when I try to call it on Polygon objects. – coredumperror Feb 2 '15 at 23:08
  • Upon further experimentation, this appears to be out-of-date compared to the newest iteration of Google Maps Javascript API v3. The Polygon class is now google.maps.Data.Polygon, and there's no getPath() method on the class. – coredumperror Feb 2 '15 at 23:20
  • This answer worked verbatim for me today (August 21, 2015) on google.maps.Polygon objects with no changes using the Google Maps API v3. – Daniel Nalbach Aug 21 '15 at 18:31
7

Here is a custom function I wrote. Feel free to use it.

function polygonCenter(poly) {
    const vertices = poly.getPath();

    // put all latitudes and longitudes in arrays
    const longitudes = new Array(vertices.length).map((_, i) => vertices.getAt(i).lng());
    const latitudes = new Array(vertices.length).map((_, i) => vertices.getAt(i).lat());

    // sort the arrays low to high
    latitudes.sort();
    longitudes.sort();

    // get the min and max of each
    const lowX = latitudes[0];
    const highX = latitudes[latitudes.length - 1];
    const lowy = longitudes[0];
    const highy = longitudes[latitudes.length - 1];

    // center of the polygon is the starting point plus the midpoint
    const centerX = lowX + ((highX - lowX) / 2);
    const centerY = lowy + ((highy - lowy) / 2);

    return (new google.maps.LatLng(centerX, centerY));
}
| |
4

Note that in the case of a concave polygon the center of the bounding rectangle might be completely outside the polygon. If your polygons might be concaved, I'd recommend using the center of the biggest inscribed circle as the "center" of the polygon. You can see a simple enough algorithm here (p. 4). If your task is to place a label on the polygon, this will also give the most aesthetically pleasing results (in which case I'd recommend using this method even if your polygons might not be concave).

| |
1

For somone seaching for an answer in dart/flutter you can achieve the same by using the code below.

 calculateCenter(List<LatLng> points) {
    var longitudes = points.map((i) => i.longitude).toList();
    var latitudes = points.map((i) => i.latitude).toList();

    latitudes.sort();
    longitudes.sort();

    var lowX = latitudes.first;
    var highX = latitudes.last;
    var lowy = longitudes.first;
    var highy = longitudes.last;

    var centerX = lowX + ((highX - lowX) / 2);
    var centerY = lowy + ((highy - lowy) / 2);

    return LatLng(centerX, centerY);
  }
| |
0

kotlin version of getting center of polygon. mPoints is array of latitude and longitude.

val bounds = LatLngBounds.Builder()

for(i in 0 until mPoints.size) {
    val point = LatLng(mPoints[i].latitude, mPoints[i].longitude)

    bounds.include(point)
}
mMap.moveCamera(CameraUpdateFactory.newLatLng(bounds.build().center))
| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.