javascript - 打开街道 map ,并回调 OpenLayer

标签 javascript openlayers

我有一张来自 olms 的 map ,我正在尝试在其上叠加一个 4x4 网格。我希望悬停的正方形改变颜色。 This code有点工作,但它不一致,有时容易出错。我如何使它更一致?

这是我的鼠标移动回调:

var displayFeatureInfo = function(pixel) {
  var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
    return feature;
  });

  if (feature) {
    document.body.style.cursor = 'pointer';
  } else {
    document.body.style.cursor = 'default';
  }

  if (feature !== highlight) {
    if (highlight) {
      featureOverlay.getSource().removeFeature(highlight);
    }
    if (feature) {
      featureOverlay.getSource().addFeature(feature);
    }
    highlight = feature;
  }
}

这是我设置它的地方:

olms('map', 'https://maps.tilehosting.com/styles/darkmatter/style.json?key=' + apiKey).then(function(mp) {
  map = mp;
  map.setView(view);
  map.addLayer(polyLayer);
  featureOverlay = new VectorLayer({
    source: new VectorSource(),
    map: map,
    style: function(feature) {
      return highlightStyle;
    }
  });
  map.on('pointermove', function(evt) {
    if (evt.dragging) {
      return;
    }
    var pixel = map.getEventPixel(evt.originalEvent);
    displayFeatureInfo(pixel);
  });
});

最佳答案

因为您有多个矢量图层,所以您需要在 forEachFeatureAtPixel 中使用过滤器

  var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
    return feature;
  },{
    layerFilter: function(candidate) {
      return candidate === polyLayer
    }
  });

关于javascript - 打开街道 map ,并回调 OpenLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58126065/

相关文章:

java - 使用 webdriver 检测远程计算机上的操作系统的包装类?

javascript - 有没有办法在有限范围内对命名空间进行原型(prototype)设计?

OpenLayers-HTML DIV叠加层

openlayers - 将 openlayers 与本地 basemap 一起使用?

javascript - Amcharts 仅显示有关调整大小事件的数据

javascript - 在 Tinymce 弹出编辑器中插入动态列表框选项

javascript - OpenLayers 测量 map 渲染时间

png - 快速写出png

javascript - 替换文件中的 Javascript 对象

javascript - 用于绘制 MGRS 的叠加层