有没有办法使用反应传单库使传单弹出响应?

2024-03-06

我一直在研究并且我知道传单 https://leafletjs.com/有插件https://github.com/yafred/leaflet-responsive-popup https://github.com/yafred/leaflet-responsive-popup但我需要一个针对我正在使用的库的解决方法反应传单 https://www.npmjs.com/package/react-leaflet.

The 反应传单 https://www.npmjs.com/package/react-leaflet有很多第三方插件,但我没有看到任何适合我的插件。 如果有人知道如何或已经做过类似的事情,那么如果您能分享它会很酷。我很难受这个问题。

Thanks.


安装库,导入js,css获取地图引用,然后渲染标记:

import R from "leaflet-responsive-popup";
import "leaflet-responsive-popup/leaflet.responsive.popup.css";
...
  const position = [51.505, -0.09];
  const mapRef = useRef();

  const icon = L.icon({
    iconUrl: "https://unpkg.com/[email protected] /cdn-cgi/l/email-protection/dist/images/marker-icon.png",
    shadowUrl: "https://unpkg.com/[email protected] /cdn-cgi/l/email-protection/dist/images/marker-shadow.png"
  });

  useEffect(() => {
    const map = mapRef.current.leafletElement;
    const marker = L.marker([51.5, -0.09], { icon });
    const popup = R.responsivePopup({
      hasTip: true,
      autoPan: true,
      offset: [15, 20]
    }).setContent("A pretty CSS3 responsive popup.<br> Easily customizable.");

    marker.addTo(map).bindPopup(popup);
  }, []);

  return (
    <Map center={position} ref={mapRef} zoom={13} style={{ height: "100vh" }}>
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />
    </Map>
  );

Edit要使插件独立于外部包装组件,您可以拥有一个 ResponsivePopup 包装文件:

const ResponsivePopup = () => {
  const { map } = useLeaflet();
  console.log(map);

  useEffect(() => {
    const marker = L.marker([51.5, -0.09], { icon });
    const popup = R.responsivePopup({
      hasTip: true,
      autoPan: true,
      offset: [15, 20]
    }).setContent("A pretty CSS3 responsive popup.<br> Easily customizable.");

    marker.addTo(map).bindPopup(popup);
  }, []);
  return null;
};

这次您将使用react-leaflet库提供的useLeaflet钩子获取地图引用,然后您的操作与第一个解决方案类似。 这次你的地图或应用程序将变成这样:

 <Map center={position} zoom={13} style={{ height: "100vh" }}>
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />
      // here use your custom wrapper for responsive popup
      <ResponsivePopup />
    </Map>

Demo https://codesandbox.io/s/using-leaflet-responsive-popup-library-in-react-leaflet-w0909

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法使用反应传单库使传单弹出响应? 的相关文章

随机推荐