Mapbox simplestyle blocks popupContent? Using pointToLayer a

Mapbox simplestyle blocks popupContent? Using pointToLayer and onEachFeature

本文关键字:Using pointToLayer popupContent simplestyle blocks Mapbox      更新时间:2023-09-26

我在Mapbox中使用这个函数与geoJson使用simplestyle的样式标记

var groupThree = new L.LayerGroup();
L.geoJson(layerThree, {
    pointToLayer: L.mapbox.marker.style,
    style: function (feature) {
        return feature.properties;
    }
}, {
    onEachFeature: onEachFeature
}).addTo(groupThree);

但是当我运行它时,当我点击标记时,我无法获得弹出窗口。下面是popupContent的函数:

var popupContent = "";
function onEachFeature(feature, layer) {
        if (feature.properties && feature.properties.popupContent) {
            popupContent = feature.properties.popupContent;
        }
        layer.bindPopup(popupContent);
    }

这是我的小提琴显示标记没有简单的风格,有工作的弹出框,和简单的风格,其弹出框不起作用的标记。

pointToLayer和onEachFeature干扰?我怎样才能让它工作?

一切正常,除了一个语法错误:

var groupTwo = new L.LayerGroup();
L.geoJson(layerTwo, {
  pointToLayer: L.mapbox.marker.style,
  style: function(feature) { return feature.properties; }
}, {onEachFeature: onEachFeature}).addTo(groupTwo);
 ^
 ^

在这里,您将onEachFeature作为L.geoJson的单独参数传递,但L.geoJson中没有第三个参数。所以基本上你创建了两个对象而不是一个。

固定:

var groupTwo = new L.LayerGroup();
L.geoJson(layerTwo, {
  pointToLayer: L.mapbox.marker.style,
  style: function(feature) { return feature.properties; },
  onEachFeature: onEachFeature
}).addTo(groupTwo);

groupThree的情况相同。

工作JSFiddle