Skip to content Skip to sidebar Skip to footer

React Leaflet Side-by-side

I wanted to show two tile layers side by side, like the plugin side-by-side for leaflet (https://github.com/digidem/leaflet-side-by-side). However, I am not sure how to do this wi

Solution 1:

Just create a component and use the native leaflet code inside a useEffect after importing the plugin.

import"leaflet-side-by-side";
...

const Map = () => {
  useEffect(() => {
    constmap = L.map("map").setView([51.505, -0.09], 13);

    const osmLayer = L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
      attribution:
        '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    var stamenLayer = L.tileLayer(
      "https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png",
      {
        attribution:
          'Map tiles by <a href="http://stamen.com">Stamen Design</a>, ' +
          '<a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; ' +
          "Map data {attribution.OpenStreetMap}",
        minZoom: 1,
        maxZoom: 16
      }
    ).addTo(map);

    L.control.sideBySide(stamenLayer, osmLayer).addTo(map);
  }, []);

  return <div id="map" />;
};

Demo

Post a Comment for "React Leaflet Side-by-side"