【问题标题】:Using fontawesome with leaflet markers and geojson使用带有传单标记和 geojson 的 fontawesome
【发布时间】:2019-03-04 23:34:21
【问题描述】:

发现这个问题很难解决。我想在我的传单地图上使用 fontawesome 标记,它使用 GeoJson 数据来表示不同标记的 lon/lat。

var Icon = L.icon({
    html: '<i class="fas fa-map-pin"></i>',
    iconSize: [20, 20]
  });

    var mymap = L.map('mapid').setView([-37.735018, 144.894947], 13);

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
            '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        id: 'mapbox.streets'
    }).addTo(mymap);

    L.geoJSON(myGeojsonData).addTo(mymap);

    var circle = L.circle([-37.735018, 144.894947], {
    color: 'red',
    fillColor: '#f03',
    fillOpacity: 0.5,
    radius: 50
}).addTo(mymap);

geoJson 示例

var myGeojsonData =
{
  "features": [
    {
      "geometry": {
        "coordinates": [
          144.829434,
          -37.825233
        ],
        "type": "Point"
      },
      "properties": {
        "Area": "Combined Entry MVT on Grieve Pde, West Gate Fwy North Ramps, Grieve Pde Byp Start EB between Grieve ",
        "IDnumber": "2541EL_P0"
      },
      "type": "Feature"
    },
    {
      "geometry": {
        "coordinates": [
          144.829502,
          -37.825234
        ],
        "type": "Point"
      },
      "properties": {
        "Area": "Combined Entry MVT on Grieve Pde, West Gate Fwy North Ramps, Grieve Pde Byp Start EB between Grieve ",
        "IDnumber": "2541EL_P1"
      },
      "type": "Feature"
    },
    {
      "geometry": {
        "coordinates": [
          144.881846,
          -37.732708
        ],
        "type": "Point"
      },

我想在 geoJson 给出的所有单独点上添加 var Icon 信息作为标记。

我知道如何用一个点来做到这一点,但如果有多个,它会变得非常混乱......

【问题讨论】:

    标签: javascript leaflet font-awesome


    【解决方案1】:

    我猜你的“myGeojsonData”对象是一个“多点”特征类型,所以我在你的“红圈”附近创建了几个点。我将您的 Icon 更改为 DivIcon 并通过在 L.geoJSON 调用上使用 pointToLayer 回调连接了标记逻辑。

    var myGeojsonData = {
        "type": "Feature",
        "geometry": {
            "type": "MultiPoint",
            "coordinates": [
                [144.894947,-37.72],
                [144.894947,-37.70]
            ]
        }
    };
    
    var myIcon = L.divIcon({
        html: '<i class="fas fa-map-pin"></i>',
        iconSize: [20, 20],
        className: 'dummy' // We don't want to use the default class
    });
    
    var mymap = L.map('mapid').setView([-37.735018, 144.894947], 13);
    
    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
            '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        id: 'mapbox.streets'
    }).addTo(mymap);
    
    L.geoJSON(myGeojsonData, {
        pointToLayer: function(getJsonPoint, latlng) {
            return L.marker(latlng, { icon: myIcon });
        }
    }).addTo(mymap);
    
    var circle = L.circle([-37.735018, 144.894947], {
        color: 'red',
        fillColor: '#f03',
        fillOpacity: 0.5,
        radius: 50
    }).addTo(mymap);
    

    结果:

    使用如下所示的 GeoJSON 数据:

    var myGeojsonData = {
        "features": [
            {
                "geometry": {
                    "coordinates": [
                        144.829434,
                        -37.825233
                    ],
                    "type": "Point"
                },
                "properties": {
                    "Area": "Combined Entry MVT on Grieve Pde, West Gate Fwy North Ramps, Grieve Pde Byp Start EB between Grieve ",
                    "IDnumber": "2541EL_P0"
                },
                "type": "Feature"
            },
            {
                "geometry": {
                    "coordinates": [
                        144.829502,
                        -37.825234
                    ],
                    "type": "Point"
                },
                "properties": {
                    "Area": "Combined Entry MVT on Grieve Pde, West Gate Fwy North Ramps, Grieve Pde Byp Start EB between Grieve ",
                    "IDnumber": "2541EL_P1"
                },
                "type": "Feature"
            }
        ]
    };
    

    这两点看起来真的很接近:

    对于弹出窗口,我不知道它最初的样子,因为我没有在您的原始代码中看到任何“bindpopup”类型调用,并且测试您提供的原始代码(没有我的更改)我没有获取任何弹出窗口。您可以像这样添加弹出窗口:

    L.geoJSON(myGeojsonData, {
        pointToLayer: function (getJsonPoint, latlng) {
            return L.marker(latlng, { icon: myIcon });
        }
    }).bindPopup(function(layer) {
        return 'ID #: ' + layer.feature.properties.IDnumber + '<br />Area: ' + layer.feature.properties.Area;
    }).addTo(mymap);
    

    我没有添加任何样式,所以它们看起来很丑。还有更多用于定位和样式化弹出窗口的设置。

    【讨论】:

    • 很好的解决方案 - 我使用不同的 GeoJson 格式,是否可以使用我的示例提供解决方案?
    • 我无法渲染第三个点,可能是因为我的编辑器删除了一些尾随文本,但您的对象应该可以与上面列出的其他更改一起使用。
    • 唯一的问题是它没有显示之前的弹出消息
    • 您的回答完全准确,非常感谢您,一旦您弄清楚为什么它不再弹出 geojson 中保存的信息的原因,我会勾选它。
    • 我今天没时间了。祝你好运!我认为您真的很接近最终解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多