How to display Leaflet markers near the 180° meridian?

前端 未结 4 805
小蘑菇
小蘑菇 2020-12-04 00:07

I am using Leaflet 1.0.0-rc.2+e02b5c9. I know the default is rendering all markers, polylines ... from longitude -180 to 180 as screen shot here:

However, I

4条回答
  •  没有蜡笔的小新
    2020-12-04 00:39

    Just make sure that the longitudes of your markers are in the range 0..360 instead of in the range -180..180. See a working example.

    i.e. instead of

    L.marker([0,170]).addTo(map);
    L.marker([0,-180]).addTo(map);
    L.marker([0,-170]).addTo(map);
    

    Do something like

    L.marker([0,170]).addTo(map);
    L.marker([0,180]).addTo(map);
    L.marker([0,190]).addTo(map);
    

    In other words, if a longitude is smaller than zero, add 360 to it. You might want to use L.Util.wrapNum(lng, [0,360], true) instead, if you plan to filter all your longitudes at once.

提交回复
热议问题