使用传单获取 geojson Continent/Country/State 的中心

Get center of geojson Continent/Country/State with leaflet

有没有办法找到 countries/continents 的中心,它由多个多边形对象组成(例如美国、加拿大,除了主要陆地部分外还有一个岛屿)?我试图使用传单的 Layer.getCenter() 甚至 Layer.getBounds().getCenter(),但这些方法肯定不适用于此类国家和 return 一个 "no-go" 结果。

例子可以看下面link(leaflet playground)。只需滚动并单击美国或加拿大,然后查看 circleMarker 出现的位置。然后尝试点击较小的实体,比如美国南部的一些岛屿,他们应该把这个标记精确地放在它们的中心:

http://playground-leaflet.rhcloud.com/voyi/1/edit?html,output

Leaflet 只能计算特征边界框的中心。在其他库的一些帮助下(例如多边形的 Turf.js) you can calculate the centroid 或 (multipolygon).

无论如何,您会感到困惑,因为有时质心不在多边形(或多多边形)内:


(来源:postgis.net

(图 1. 根据 PostGIS's ST_Centroid documentation 的多边形质心)

所以我猜你真正想问的是:

Given a polygon or multipolygon, how can I calculate a center (or center-like point) which is guaranteed to be within the polygon and looks visually in the center of it, in order to add symbolizers to that point with Leaflet?

为此,答案是 "pole of inaccessibility" as implemented by polylabel:«距多边形轮廓最远的内部点»。