简体   繁体   English

如何在角度传单地图上将 GeoJson 要素属性显示为标签?

[英]How to show GeoJson feature properties as labels on the angular leaflet map?

I'm trying to show GeoJson feature properties as labels on the leaflet map.我正在尝试将 GeoJson 要素属性显示为传单地图上的标签。 here's my code:这是我的代码:

private createGeoJsonLayer(cords) {
return L.geoJSON(
  cords as any,
  {
    onEachFeature: (feature, layer) => {
      layer.bindTooltip(feature.properties.country_name).openTooltip();
    },
  });

the problem with this code is that it depends on the mouse hover to show the tooltip and the tooltip does not open when the page loads, how can I fix this?这段代码的问题是它依赖于鼠标悬停来显示工具提示,而当页面加载时工具提示不会打开,我该如何解决这个问题? user expects to see all the country names as labels when the page loads.用户希望在页面加载时看到所有国家名称作为标签。

and if there is a better way than tooltip to show labels on the map, I'll be happy to hear that.如果有比工具提示更好的方法在地图上显示标签,我会很高兴听到。

I'm trying to improve my own code around labels like you.我正在尝试围绕像您这样的标签改进我自己的代码。 This is a piece of my code, similar to yours, that I'm working on over a previous ajax call.这是我的一段代码,类似于您的代码,我正在处理之前的 ajax 调用。

In this case the tooltip always open when the page loads.在这种情况下,工具提示总是在页面加载时打开。 The Leaflet documentation explains about parameters like "permanent: true": Leaflet 文档解释了诸如“permanent: true”之类的参数:

Whether to open the tooltip permanently or only on mouseover.是永久打开工具提示还是仅在鼠标悬停时打开。

So"true" is necessary in your case.所以在你的情况下“真”是必要的。

It is working, and could be helpful for you.它正在工作,可能对您有所帮助。

var calzada = new L.geoJson(data, {
                    onEachFeature: function(feature, layer) {
                      layer.bindTooltip(feature.properties.nam.toString(), 
                         {permanent: true, className: 'label'}
                        ).addTo(map);
                    }
                  })

"nam" is the property I am labeling and "label" is my CSS style “nam”是我标记的属性,“label”是我的 CSS 样式

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

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