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

时间:2015-09-18 15:35:13

标签: javascript leaflet mapbox

我正在使用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();

1 个答案:

答案 0 :(得分:2)

默认弹出行为只允许您一次打开一个功能,因此您必须使用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
});