简体   繁体   English

如何在 Angular 中显示传单标记?

[英]How to show Leaflet markers in Angular?

I am using leaflet and after a lot of research i still cannot get my marker to show on my map.我正在使用传单,经过大量研究后,我仍然无法让我的标记显示在我的地图上。

I tried all the solutions I could find out there.我尝试了所有可以在那里找到的解决方案。 Even the the angular workaround suggested by Leaflet.即使是 Leaflet 建议的角度解决方法。 For now I managed to get the marker img, with the right path to render into the html, tho it still doesn't show the image.现在我设法获得了标记 img,并使用了正确的路径来渲染到 html 中,但它仍然没有显示图像。 In other words, I get the right path, it finds the image, I can see all that in the console, but then says cannot load image, which is rather misleading because there is definitely nothing wrong with the image换句话说,我找到了正确的路径,它找到了图像,我可以在控制台中看到所有内容,但随后说无法加载图像,这是相当具有误导性的,因为图像绝对没有问题

below is my code:下面是我的代码:

let markerIcon = L.icon({ iconUrl: 'src/assests/icons/marker.png'});
L.marker([48.208, 16.373], {
      icon: markerIcon,
      riseOnHover: true
}).addTo(map);

below is the console of the rendered html in the browser with the correct path and everything:下面是浏览器中呈现的 html 的控制台,具有正确的路径和所有内容:

<img src="src/assests/icons/marker.png" class="leaflet-marker-icon leaflet-zoom-animated leaflet-interactive" alt="" tabindex="0" style="transform: translate3d(659px, 207px, 0px); z-index: 207;">

I just wanted that marker to show up!我只是想让那个标记出现! Without markers my whole project is useless.没有标记,我的整个项目就毫无用处。

You need to pass an icon object which will define the path for the marker image like this:您需要传递一个图标对象,它将定义标记图像的路径,如下所示:

icon = {
    icon: L.icon({
      iconSize: [ 25, 41 ],
      iconAnchor: [ 13, 0 ],
      // specify the path here
      iconUrl: './node_modules/leaflet/dist/images/marker-icon.png',
      shadowUrl: './node_modules/leaflet/dist/images/marker-shadow.png'
   })
};

and then const marker = L.marker([51.5, -0.09], this.icon).addTo(map);然后const marker = L.marker([51.5, -0.09], this.icon).addTo(map);

No need to include the marker's image on the assets folder because it is already included in the Leaflet package.无需在资产文件夹中包含标记的图像,因为它已包含在 Leaflet 包中。

Here is a working demo with a marker and a popup using latest angular version.这是一个使用最新 angular 版本的带有标记和弹出窗口的工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM