mapbox - 如何设置要素图层的弹出选项

mapbox - how to set the popup options for a feature layer

我正在使用 mapbox.js 生成包含标记和弹出窗口的图层。我在一定的时间间隔内以编程方式缩放到该标记并显示弹出窗口。但是,我想禁用弹出窗口的 "closeOnClick" 功能,但是如果我在创建 featureLayer 后设置它,它没有任何效果。任何人都知道如何正确地做到这一点?这是我的代码:

eventMarkerLayer = L.mapbox.featureLayer({ //add the event marker
  type: 'Feature',
  geometry: {
      type: 'Point',
      coordinates: [eventPt.lng,eventPt.lat]
  },
  properties: {
    title:eventScenario.text,
    'marker-color': eventColor
  },
  options: {
    popupOptions: {
      closeOnClick: false //doesn't work
    }
  }
}).addTo(map);

eventMarkerLayer.options.popupOptions.closeOnClick = false; //doesn't work either

eventMarkerLayer.openPopup();

默认弹出窗口行为只允许您一次打开一项功能,因此您必须使用 L.popup():

手动添加弹出窗口
eventMarkerLayer = L.mapbox.featureLayer({ //add the event marker
  type: 'Feature',
  geometry: {
      type: 'Point',
      coordinates: [eventPt.lng,eventPt.lat]
  },
  properties: {
    popupContent: eventScenario.text, // note -- change "title" to another name
    'marker-color': eventColor
  }
}).addTo(map);

eventMarkerLayer.eachLayer(function(layer) {
  var popup = L.popup({
      closeOnClick: false, // keeps popups open
      offset: L.point(0, -25) // offset so popup shows up above marker
    })
  .setLatLng([layer.feature.geometry.coordinates[1], layer.feature.geometry.coordinates[0]]) // L.popup takes [lat, lng]
  .setContent(layer.feature.properties.popupContent); // add content from feature
  map.addLayer(popup); // add to map
});