PolygonUtil.js 687 B

12345678910111213141516171819202122232425
  1. /*
  2. * L.PolygonUtil contains different utility functions for Polygons.
  3. */
  4. L.PolygonUtil = {
  5. // Ported from the OpenLayers implementation. See https://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/Geometry/LinearRing.js#L270
  6. geodesicArea: function (latLngs) {
  7. var pointsCount = latLngs.length,
  8. area = 0.0,
  9. d2r = L.LatLng.DEG_TO_RAD,
  10. p1, p2;
  11. if (pointsCount > 2) {
  12. for (var i = 0; i < pointsCount; i++) {
  13. p1 = latLngs[i];
  14. p2 = latLngs[(i + 1) % pointsCount];
  15. area += ((p2.lng - p1.lng) * d2r) *
  16. (2 + Math.sin(p1.lat * d2r) + Math.sin(p2.lat * d2r));
  17. }
  18. area = area * 6378137.0 * 6378137.0 / 2.0;
  19. }
  20. return Math.abs(area);
  21. }
  22. };