leaflet - 使用只有很棒的字体的标记图标,没有周围的气球

标签 leaflet font-awesome markers

我的代码可以正常工作,但我只需要显示要显示的图标,而不是带有阴影的“气球”。

我尝试删除“markerColor...”,但这只是更改为默认的蓝色标记/气球。

如何只显示图标及其大小和颜色?

pointToLayer: function(feature, latlng) {
  var con = feature.properties.concept;

  var hub;

  // icons for XX, YY and ZZ 
  if (kon === 'XX') {
    hub = new L.marker(latlng, {icon: L.AwesomeMarkers.icon({icon: 'truck', prefix: 'fa', markerColor: 'cadetblue'}) });
  } else if (kon === 'YY') {
    hub = new L.marker(latlng, {icon: L.AwesomeMarkers.icon({icon: 'envelope', prefix: 'fa', markerColor: 'blue'}) });
  } else if (kon === 'ZZ') {
    hub = new L.marker(latlng, {icon: L.AwesomeMarkers.icon({icon: 'bicycle', prefix: 'fa', markerColor: 'darkblue'}) });
  } else {
    hub = new L.marker(latlng, {icon: L.AwesomeMarkers.icon({icon: 'envelope-o', prefix: 'fa', markerColor: 'red'}) });
  }
  return hub;
}

最佳答案

不幸的是,Leaflet.awesome-markers插件不提供您选择显示内部图标(来自 Font Awesome 或任何来源)周围的气球。

同样适用于 cloned version和其他变体,如 Leaflet.extra-markers插件。

但是您可以非常简单地使用传单 DivIcon 相反:

Represents a lightweight icon for markers that uses a simple <div> element instead of an image. Inherits from Icon but ignores the iconUrl and shadow options.

然后你只需填写 <div>带有 Font Awesome 图标的容器,与在普通页面中执行的方式相同,以及 Leaflet.awesome-markers 插件在幕后为您所做的事情:

L.marker(latlng, {
  icon: L.divIcon({
    html: '<i class="fa fa-truck" style="color: red"></i>',
    iconSize: [20, 20],
    className: 'myDivIcon'
  })
});

请注意,您还必须指定一些 CSS 来根据需要自定义它:

.myDivIcon {
  text-align: center; /* Horizontally center the text (icon) */
  line-height: 20px; /* Vertically center the text (icon) */
}

示例:

var map = L.map('map').setView([48.86, 2.35], 11);

L.marker([48.86, 2.35], {
  icon: L.divIcon({
    html: '<i class="fa fa-truck" style="color: red"></i>',
    iconSize: [20, 20],
    className: 'myDivIcon'
  })
}).addTo(map);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
.myDivIcon {
  text-align: center; /* Horizontally center the text (icon) */
  line-height: 20px; /* Vertically center the text (icon) */
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet-src.js" integrity="sha512-IkGU/uDhB9u9F8k+2OsA6XXoowIhOuQL1NTgNZHY1nkURnqEGlDZq3GsfmdJdKFe1k1zOc6YU2K7qY+hF9AodA==" crossorigin=""></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet.awesome-markers@2.0.4/dist/leaflet.awesome-markers.css" />
<script src="https://unpkg.com/leaflet.awesome-markers@2.0.4/dist/leaflet.awesome-markers.js"></script>
<link href="https://use.fontawesome.com/releases/v5.0.8/css/all.css" rel="stylesheet">

<div id="map" style="height: 180px"></div>

关于leaflet - 使用只有很棒的字体的标记图标,没有周围的气球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099987/

相关文章:

javascript - 使用 leaflet Js 创建 MultiPolyline

python - folium 中线条的动态样式

javascript - 是否可以将传单版本 1 与自定义 mapbox 样式一起使用?

html - 如何使 Font Awesome 图标以特定颜色发光

ios - 如何使用 mapbox ios 标记执行操作

javascript - 计算 MarkerCluster 中的元素数量

reactjs - 如何在geojson层中手动控制react-leaflet弹出窗口(通过props)?

html - 文本重叠字体很棒的图标 - 只发生在移动/平板设备上

html - 如何在选择下拉菜单上设置 Font Awesome 图标?