简体   繁体   English

如何使用 NUXT.js 为我的传单地图设置自定义标记图标

[英]How can I set the custom marker icon for my leaflet map with NUXT.js

I am trying to change marker icon for separate marker on my OpenStreetMap.我正在尝试更改 OpenStreetMap 上单独标记的标记图标。

  mapIconsReinit(L) {
    delete L.Icon.Default.prototype._getIconUrl;

    L.Icon.Default.imagePath = ''
    L.Icon.Default.mergeOptions({
      iconRetinaUrl: require('@/assets/img/map_markers/default/marker-icon-2x.png'),
      iconUrl: require('@/assets/img/map_markers/default/marker-icon.png'),
      shadowUrl: require('@/assets/img/map_markers/default/marker-shadow.png'),
    });
  },

  getMarkerIcon(L, color) {
    return L.divIcon({
      iconRetinaUrl: require('@/assets/img/map_markers/marker-icon-2x-' + color + '.png'),
      iconUrl: require('@/assets/img/map_markers/marker-icon-' + color + '.png'),
      shadowUrl: require('@/assets/img/map_markers/marker-shadow.png'),
      iconSize: [25, 41],
      iconAnchor: [12, 41],
      popupAnchor: [1, -34],
      shadowSize: [41, 41]
    })
  }

First function works fine with paths like '@/...' , but the 2nd one - no.第一个函数适用于像'@/...'这样'@/...'路径,但第二个函数 - 不。

Default marker works fine:默认标记工作正常:

L.marker([marker.lat, marker.lng]).addTo(_context.map)

but if I try to use custom marker:但如果我尝试使用自定义标记:

L.marker([marker.lat, marker.lng], {icon: this.getMarkerIcon(L, "red")}).addTo(_context.map)

I see a white square我看到一个白色方块

在此处输入图片说明

You instantiate a Leaflet DivIcon whereas you pass options applicable for a Leaflet Icon.您实例化一个 Leaflet DivIcon,同时传递适用于 Leaflet Icon 的选项。

Use L.icon instead of L.divIcon in that case.在这种情况下使用L.icon而不是L.divIcon

The Icon expects the iconUrl (and other *Url) option to place the corresponding image on the map. Icon 需要 iconUrl(和其他 *Url)选项将相应的图像放置在地图上。

The DivIcon does not place an image but a bare HTML div element, so that you can fill it with arbitrary HTML content. DivIcon 不放置图像,而是放置一个裸 HTML div 元素,因此您可以用任意 HTML 内容填充它。 By default it is styled as a white square with black border.默认情况下,它的样式为带有黑色边框的白色方块。

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

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