将geoJSON要素属性添加到mapbox弹出窗口

时间:2015-03-12 19:00:11

标签: javascript gis leaflet mapbox geojson

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

        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);

1 个答案:

答案 0 :(得分:1)

您正在使用layer变量:

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

layer变量不包含feature对象。您正在遍历包含的图层,将它们分配给marker变量,这些变量具有feature对象,因此您应该:

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