首页 > 解决方案 > 将 geojson 属性设为链接

问题描述

我有一个包含 geojson 的外部 js 脚本。在该 geojson 对象中有属性,其中一个属性是 url。我需要在弹出窗口中将该网址显示为可点击的链接。现在,我的弹出窗口中有 url,但它只是显示为字符串。

我试图将 geojson 属性存储到一个名为 assemblyWebsite 的新变量中,然后将 assemblyWebsite 输入到我的弹出窗口中。尽管如此,它仍然具有作为字符串的变量。考虑到 url 取决于用户点击的多边形,我无法弄清楚如何将该变量变成可点击的链接。有几个不同的 url,每个弹出窗口不能是一个 url。

function onEachFeature(feature, layer) {

    var assemblyWebsite = feature.properties.QnsPubAdvocateResults_URL

    layer.bindPopup('<h4>Assembly District</h4>' + ' ' + feature.properties.AssemDist + '</b><br />' + 'Website:' + ' ' + assemblyWebsite);
};

标签: javascriptleafletgeojson

解决方案


您可以使用 href 像链接一样显示。

var layerGroup = L.geoJSON(data, {
  onEachFeature: function (feature, layer) {
    layer.bindPopup('<h4>Assembly District</h4>' + ' ' + feature.properties.AssemDist + '</b><br />' + 'Website:' + ' ' + '<a href="'+ 
assemblyWebsite + '">Visit Website</a>' );
  }
}).addTo(map);

推荐阅读