javascript - 使用传单获取 geojson 大陆/国家/州的中心

标签 javascript leaflet geojson markers

有没有办法找到由多个多边形对象组成的国家/大陆的中心(例如美国、加拿大,除了主要陆地部分外还有一个岛屿)?我尝试使用 leaflet 的 Layer.getCenter() 甚至 Layer.getBounds().getCenter(),但这些方法肯定不适用于此类国家并返回“不通过”的结果。

示例可以通过以下链接(传单 Playground )看到。只需滚动并单击美国或加拿大,然后查看 circleMarker 将出现在何处。然后尝试点击较小的实体,比如美国南部的一些岛屿,他们会把这个标记精确地放在它们的中心:

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

最佳答案

Leaflet 只能计算特征边界框的中心。在其他库(例如 Turf.js)的帮助下,您可以计算出 centroid。的多边形或(多边形)。

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


(来源: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"polylabel 实现: «距多边形轮廓最远的内部点»。

关于javascript - 使用传单获取 geojson 大陆/国家/州的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42108975/

相关文章:

javascript - 以编程方式在 UIWebView 中显示键盘

javascript - 从图层集合中搜索/检索原始容器传单图层的最佳方法

javascript - 为什么我的 d3 map 上的悬停功能无法正常工作?

javascript - 在 LeafLet map 上显示 jQuery 动态 GeoJSON 内容

node.js - 在 Cloudant 中使用 MapReduce 获取地理空间索引的结果

javascript - 视频帧卡住在下一帧 HTML5 视频

javascript - 如何根据文本长度使文本最适合?

javascript - 删除 div 之间的垂直空白,尤其是在遇到断点之后

javascript - 传单绘制删除按钮删除 "clear all"操作

javascript - 设计通过 javascript、MapBox 中的 URL 加载的 GeoJSON 多边形