0

I looked at this answer about getting the centroid of a polygon, and basically copied the code.

Because I'm trying to solve the same problem, which is getting the centroid of a polygon, and for the example given (bermuda triangle) it works wonders.

Now, i'm really having this weird bug or I really don't know what's going on but I have a similar code using the polygon i'm trying to get the centroid of.

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

    var polygonCoords = [
        new google.maps.LatLng(121.26814396414763 , 14.156734403334724 ),
        new google.maps.LatLng(121.2682025135474 , 14.156956159186617 ),
        new google.maps.LatLng(121.2673054424065 , 14.15716533202476 ),
        new google.maps.LatLng(121.26724861901177 , 14.156951585745787 ),
        new google.maps.LatLng(121.26814396414763  ,14.156734403334724 )
      ];

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

    console.log(bounds.getCenter());

Shape above is basically a rectangle a very small one. But output in console is :

R {k: 90, B: 14.156949867679714, toString: function, j: function, equals: function…}

The lat is 90 and long is 14.156949867679714, this is plain wrong it doesn't even exist, I don't know what i'm missing really. Thanks!

Community
  • 1
  • 1
muffin
  • 2,034
  • 10
  • 43
  • 79
  • btw, this polygon exists. I created it, that's why it's really weird for this code to be not working – muffin Jul 28 '14 at 02:09

1 Answers1

3

You have the latitude and longitude backwards (the maximum value for latitude is 90 degrees)

var polygonCoords = [
    new google.maps.LatLng(14.156734403334724,121.26814396414763),
    new google.maps.LatLng(14.156956159186617,121.2682025135474),
    new google.maps.LatLng(14.15716533202476,121.2673054424065),
    new google.maps.LatLng(14.156951585745787,121.26724861901177),
    new google.maps.LatLng(14.156734403334724,121.26814396414763)
  ];

working fiddle

center: latitude: 14.156949867679742, longitude: 121.26772556627952

geocodezip
  • 158,664
  • 13
  • 220
  • 245