简体   繁体   English

使用 Angular 精确定位显示 leaflet map 时出现问题

[英]Issue with displaying the leaflet map pinpoint using Angular

We are using the leaflet package v 1.7.1 in our angular app.我们在 angular 应用程序中使用 leaflet package v 1.7.1。

We are having an issue with when the use selects a point on the map, the pinpoint icon doesn't display, rather it displays the broken image icon.当用户在 map 上选择一个点时,我们遇到了问题,不显示精确图标,而是显示损坏的图像图标。

We have tried including the leaflet images in the assets folder by adding this to our angular.Json file我们尝试将 leaflet 图像添加到资产文件夹中,方法是将其添加到我们的 angular.Json 文件中

“Assets”: [

   “src/assets”,
   {
      “glob”: “**/*”,
      “input”: “./node_modules/leaflet/dist/images”,
      “output”: “./assets/“
   }
]

We have a reference to the leaflet css file in our global styles.scss file.我们在全局 styles.scss 文件中引用了 leaflet css 文件。

   @import "~leaflet/dist/leaflet.css";

In our component, we are doing the following在我们的组件中,我们正在执行以下操作

const iconRetinaUrl = './assets/marker-icon-2x.png';

const iconUrl = './assets/marker-icon.png';

const shadowUrl = './assets/marker-shadow.png';

const iconDefault = L.icon({

iconRetinaUrl,

iconUrl,

shadowUrl,

iconSize: [25, 41],

iconAnchor: [12, 41],

popupAnchor: [1, -34],

tooltipAnchor: [16, -28],

shadowSize: [41, 41]

});

L.Marker.prototype.options.icon = iconDefault;

Then when the use selects a point we are doing this:然后当用户选择一个点时,我们这样做:

var marker = L.marker([33.89268303132417, 35.50405740737915]).addTo(this.map);

We are not seeing the marker icon, we get a missing image icon.我们没有看到标记图标,我们得到一个丢失的图像图标。 If we inspect the element on the page it shows the source like this:如果我们检查页面上的元素,它会显示如下所示的源:

https://friendlyurl.somedomain.com/marker-icon.2b3e1faf89f94a483539.png")marker-icon.png

Why is angular changing the name of the file and referencing it in this manner?为什么 angular 会更改文件名并以这种方式引用它?

Why is angular changing the name of the file and referencing it in this manner?为什么 angular 会更改文件名并以这种方式引用它?

Because Angular build uses webpack with css loaders and asset files finger printing (for browser cache busting).因为 Angular 构建使用 webpack 和 css 加载程序和资产文件指纹(用于浏览器缓存破坏)。

All this interferes with Leaflet scheme to automatically detect its icon path and reconstruct it.这一切都干扰了 Leaflet 方案自动检测其图标路径并重建它。

That being said, your above workaround should have at least changed the URL of the image.话虽如此,您的上述解决方法至少应该更改了图像的 URL。

As for a solution, you might be interested in my leaflet-defaulticon-compatibility plugin:至于解决方案,您可能对我的leaflet-defaulticon-compatibility插件感兴趣:

Retrieve all Leaflet Default Icon options from CSS, in particular all icon images URL's, to improve compatibility with bundlers and frameworks that modify URL's in CSS.从 CSS 中检索所有 Leaflet 默认图标选项,特别是所有图标图像 URL,以提高与在 CSS 中修改 URL 的捆绑程序和框架的兼容性。

import 'leaflet/dist/leaflet.css';
import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.webpack.css'; // Re-uses images from ~leaflet package
import * as L from 'leaflet';
import 'leaflet-defaulticon-compatibility';

See issue Leaflet/Leaflet#4698 if you want the story and details, and other possible solutions.如果您想了解故事和详细信息以及其他可能的解决方案,请参阅问题 Leaflet/Leaflet#4698

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

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