Google Maps JavaScript API v3: how to get the center lat & lon of a polygon


How to get the center lat & lon of a polygon?

We can do it just by calling polygon.getBounds().getCenter()

// 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)
];

var triangle = new google.maps.Polygon({
                 paths: polygonCoords,
                 strokeColor: "#0000FF",
                 strokeOpacity: 0.8,
                 strokeWeight: 2,
                 fillColor: "#FF0000",
                 fillOpacity: 0.35
     });

alert(triangle.getBounds().getCenter());

Note: in Google Maps JavaScript API v2, Polygon had a getBounds() method, but that doesn’t exist for v3 Polygon. You can find that method in this article.

Recent search terms:

  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1841 -avsalDE7CoHnneZhVtmXMq0AwvnGJJBSuk0Az8MTQYLdHoKbRmJuVYsrARaWcpP 2b6f7cf7ce440e4e705f266d40f79c3814a08697&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1832 chHTaF-Xb9ttU9nQ8zBXsPlXnR46AkXL0AMEV4IzkciFLMHu2Ps0k7GS5iPamXHo cd879e8e8596c9512a140ff499e4ed86dfdaa0b1&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 3hEM0iKwwtln65sec6ZJWIvWiLncf1Luv7Dh5xevWh3BjHGSIAXYPTCHQ8tzGEzo 519dcdcb6989b034d8fbcd0da465188268205d3f&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1829 gOIp_qZMfrVuV-izcwjCcHcm8V6NmeM488YWrM9V6JJnk8eKtRMNcxvKrXHWG9gP 0b3a98c60209d03b38f45de1c89cf1225641eee8&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1826 Hx3OUzLxfTCCtALcWrTZVGdpFwjGKyGp29LdCpSql6ohSfJeBm-evu4C4Rk2fddV 39c2c441535ef5a94bc12641ad642ab052f84265&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1826 5ptWrfaMpU4V50iplgbSWzxRJjCrwMDQgm9nrn3Ox3qWd6Sq8ksLQaX4E_sF3bwQ bc8260623987d78433fe874ed0add439403a3d3c&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1825 6j1sqUjgoqX1k6rVha6yT4oUl3EulU9jQi7tVq0qppFdGEkdB7s2mVf00EcvEo4c f079e363c71890043383663662c5f36eb43be4d9&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1824 AfrY67DPUNFzlcxTMd2N-80GKtah4gykefpnpE5gTtLj3LnUOik_NAeMnEqRnoTH eb6714b6beee96cb19ab41eab6bd1f1fdb976368&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1823 0TSnge8IwLChYrtIMOz0hgNK4smWSA1-yfW-tpaxgFjDnBk6dcFWh7F3QSsj6EpM 8d80279ae17d2d42c4c1e61e0142ecc6893c9156&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1821 IE13GPKymhhamUo9k8Ck8GtTjCzpismGSk237olryd2EGad0_ao7KGttiZvfAvrU ad0bd2e02970f43c0d4a1346d2c839c0eddf5e0e&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply