webpack中使用leaflet 时图标加载报错问题?

发布于 2022-09-11 15:43:54 字数 1249 浏览 11 评论 0

1 在webpack中使用leaflet添加marker 时,使用默认marker报错
图片描述

转码后的base64图标 后会多出来默认 marker 名称

webpack.config.js 中的相关配置

resolve: {
      alias: {
          '@':path.resolve(__dirname, './src'),
          leaflet_css: __dirname + "/node_modules/leaflet/dist/leaflet.css",
          leaflet_marker: __dirname + "/node_modules/leaflet/dist/images/marker-icon.png",
          leaflet_marker_2x: __dirname + "/node_modules/leaflet/dist/images/marker-icon-2x.png",
          leaflet_marker_shadow: __dirname + "/node_modules/leaflet/dist/images/marker-shadow.png",
      }
  },

代码部分

import "leaflet_css"; 
import L from "leaflet";

(function(){
    var map = L.map('map-osm-leaflet').setView([51.505, -0.09], 13);

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

    L.marker([51.5, -0.09]).addTo(map)
        .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
        .openPopup();
})();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2022-09-18 15:43:54

对我也是遇见这种问题了 自己项目中的默认icon就不可以使用 同样的错误不知道为什么自己项目中会变成base64,但是在官网上就会变成cdn,

后来的解决方法是手动引入leaflet的图标

import icon from "leaflet/dist/images/marker-icon.png";
import iconShadow from "leaflet/dist/images/marker-shadow.png";

let DefaultIcon = L.icon({
            iconUrl: icon,
            shadowUrl: iconShadow
        });
L.Marker.prototype.options.icon = DefaultIcon;
new L.marker(//你marker的坐标位置).addTo(this.map); 
萌梦深 2022-09-18 15:43:54

目前是自己传入图标,不使用默认图标时可以正常使用

(function(){
     var myIcon = L.icon({
         iconUrl: marker_icon,
         iconSize: [20, 20],
         iconAnchor: [10, 20],
    });

    var map = L.map('map-osm-leaflet').setView([51.505, -0.09], 13);

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    L.marker([51.5, -0.09],{
        icon: myIcon
    }).addTo(map)
        
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文