How to display Leaflet markers near the 180° meridian?

前端 未结 4 791
小蘑菇
小蘑菇 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:30

    Another approach is to leverage the Leaflet.RepeatedMarkers plugin, which will display a copy of each marker per 360 degrees of longitude:

    Applying this to markers near the antimeridian works as well, e.g.:

    var myRepeatingMarkers = L.gridLayer.repeatedMarkers().addTo(map);
    
    L.polyline([[-85,180],[85,180]]).addTo(map);
    L.polyline([[-85,-180],[85,-180]]).addTo(map);
    
    myRepeatingMarkers.addMarker(L.marker([0,140]));
    myRepeatingMarkers.addMarker(L.marker([0,150]));
    myRepeatingMarkers.addMarker(L.marker([0,160]));
    myRepeatingMarkers.addMarker(L.marker([0,170]));
    myRepeatingMarkers.addMarker(L.marker([0,180]));
    myRepeatingMarkers.addMarker(L.marker([0,-170]));
    myRepeatingMarkers.addMarker(L.marker([0,-160]));
    myRepeatingMarkers.addMarker(L.marker([0,-150]));
    myRepeatingMarkers.addMarker(L.marker([0,-140]));
    

    will display something like:

    Check out the live example for using Leaflet.repeatedMarkers with markers near the antimeridian.

    0 讨论(0)
  • 2020-12-04 00:36

    A similar issue has been encountered and reported on the Leaflet Github

    The solution is to increase the longitude of your markers if their initial longitude is below 0.

    var totalMarkers = markerPositions.length;
    for(var i = 0; i<totalMarkers; i++){
        var mData = markerPositions[i];
        if (mData.lon < 0) {
            mData.lon += 360;
        }
        L.marker([mData.lat, mData.lon]).addTo(map);
    }
    
    0 讨论(0)
  • 2020-12-04 00:36

    Thank you all for the help. Actually, I am not using your suggested codes but I got me the idea to fix it.

    This is my solution, hope it helpful for others looking for same solution: Screenshot that was solved

    Always display the icon and its copy on the map (on longitude range you want, in my case, it is from 0 to 360)

    makeMarkers: (item) ->
      markers = []
      markers.push(item.makeMarker())
    
      copy_marker = item.makeMarker()
      copy_marker.setLatLng( new L.LatLng(copy_marker._latlng.lat, copy_marker._latlng.lng + 360) )
      markers.push(copy_marker)
    
      markers
    

    In Item class:

    makeMarker: ->
      LeafletIcon = L.Icon.extend(
        options: {
          iconSize:     [25, 25],
          iconAnchor:   [10, 10],
          popupAnchor:  [0, 0]
        },
      )
      icon = new LeafletIcon( {iconUrl: this.iconUrl} )
    
      marker = L.marker([this.latitude, this.longitude], {icon: icon, zIndexOffset: 10})
      marker.id = this.id
    
      marker
    

    Thank you again.

    0 讨论(0)
  • 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.

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