【问题标题】:Adding geoJSON feature attributes to mapbox popup将 geoJSON 要素属性添加到地图框弹出窗口
【发布时间】:2015-03-12 19:00:10
【问题描述】:

我希望为地图上的每个书店标记添加一个带有 geoJSON 属性的弹出窗口。我在 marker.bindpopup 方法中使用了“layer.feature.properties.name”,但得到了“未定义”的返回。

L.mapbox.accessToken = 'jk.eyJ1IjsdkjfhskjdfhksdskdjflWNCJ9.Ov2O5PtskdljfsdR0lq3Q';
var map = L.mapbox.map('map', 'example.kks3kec4')
    .setView([38.633, -90.319],12);

//add cafe, books store, and university geoJSON layers with styling
var bookStore = L.mapbox.featureLayer()
    .loadURL('book_store84.geojson')
    //wait for the layer to be "on", or "loaded", to use a function that will setIcon with an L.icon object
    .on('ready', function(layer) {
        this.eachLayer(function(marker) {
            marker.setIcon(L.mapbox.marker.icon({
                'marker-color': '#BA1A1A',
                'marker-symbol': "library",
                'description': "book store"
            }));
            //when layer.feature is within the popup, it returns "undefined"
            marker.bindPopup("<p>" + layer.feature.properties.name + "</p>");
        });
    })
    .addTo(map);

【问题讨论】:

    标签: javascript gis leaflet mapbox geojson


    【解决方案1】:

    您正在使用layer 变量:

    marker.bindPopup("<p>" + layer.feature.properties.name + "</p>");
    

    layer 变量不包含feature 对象。您正在循环包含的层,将它们分配给marker 变量,这些层具有feature 对象,所以您应该这样做:

    marker.bindPopup("<p>" + marker.feature.properties.name + "</p>");
    

    【讨论】:

    • 非常感谢!这正是我所需要的,而且是一个如此简单的修复。希望我早点问。
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多