繁体   English   中英

如何在传单中定义其他tooltip()

[英]How to define an additional tooltip() in leaflet

我已经在使用一个tooltip()函数。

L.tooltip({
    permanent: true,
    direction: 'center',
    className: 'text'
})
.setContent(feature.properties.text)
.setLatLng(layer.getLatLng()).addTo(map);

现在,我需要创建一个新的工具提示。 例如,使用名称tooltip2()。

我怎样才能做到这一点?

您实际上可以继续使用此功能多次!

L.tooltip({
  permanent: true,
  direction: 'center',
  className: 'text'
})
  .setContent("Hello")
  .setLatLng([38.8, -77.1])
  .addTo(map);

L.tooltip({
  permanent: true,
  direction: 'center',
  className: 'text'
})
  .setContent("World")
  .setLatLng([38.7, -77.1])
  .addTo(map);

但是,由于您正在使用工具提示中的要素和图层,因此您似乎正在遍历geojson数据。 在这种情况下,您只需编写一次即可:

L.geoJSON(geojson, {
  onEachFeature: (feature, layer) => {
    L.tooltip({
      permanent: true,
      direction: 'center',
      className: 'text'
    })
      .setContent(feature.properties.name)
      .setLatLng(layer.getLatLng())
      .addTo(map);
  }
}).addTo(map);

如果您要命名工具提示或功能层以便稍后在代码中再次使用它们,则可以将其设置为: const featureLayer = L.geoJSON...const text = L.tooltip...

这是一个向地图添加弹出窗口,工具提示,要素图层并调整弹出窗口和工具提示css(包括方向三角形)的工作示例: https : //repl.it/repls/NeighboringConventionalPhp

暂无
暂无

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

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