简体   繁体   English

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

[英]Adding geoJSON feature attributes to mapbox popup

I am looking to add a popup with geoJSON attributes to each book store marker on my map.我希望为地图上的每个书店标记添加一个带有 geoJSON 属性的弹出窗口。 I have used "layer.feature.properties.name" within the marker.bindpopup method, but am getting a return of "undefined".我在marker.bindpopup 方法中使用了“layer.feature.properties.name”,但返回“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);

You're using the layer variable:您正在使用layer变量:

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

The layer variable does not contain the feature object. layer变量不包含feature对象。 You are looping over the contained layers assigning them to the marker variable, those have the feature object, so you should that:您正在遍历包含的图层,将它们分配给marker变量,这些图层具有feature对象,因此您应该:

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

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

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