Get center of geojson Continent/Country/State with leaflet

前端 未结 1 1063
耶瑟儿~
耶瑟儿~ 2021-01-25 20:44

Is there a way of finding a center for countries/continents which consist of more than one polygon object(ex. USA, Canada, which have an islands besides main land part)? I was t

1条回答
  •  执笔经年
    2021-01-25 21:15

    Leaflet can only calculate the center of the bounding box of a feature. With a bit of help from other libraries (such as Turf.js) you can calculate the centroid of the polygon or (multipolygon).

    In any case, you are getting confused because sometimes the centroid is not within the polygon (or multipolygon):


    (source: postgis.net)

    (Figure 1. Centroid of a polygon as per PostGIS's ST_Centroid documentation)

    So I guess that what you really wanted to ask is:

    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?

    And for that, the answer is "pole of inaccessibility" as implemented by polylabel: «the most distant internal point from the polygon outline».

    0 讨论(0)
提交回复
热议问题