简体   繁体   English

使用forEachFeatureatPixel的Firefox非常慢

[英]Firefox very slow with forEachFeatureatPixel

Firefox and IE are operating REALLY slowly with OPENLAYERS 3 when using the forEachFeatureatPixel function. 使用forEachFeatureatPixel函数时,Firefox和IE在OPENLAYERS 3上的运行速度非常慢。 I'm trying to find a way to speed it up. 我正在尝试找到一种加快速度的方法。 Essentially, the application (found at www.penguinmap.com) has as popup that comes up when the user hovers over a point on the map. 本质上,该应用程序(可在www.penguinmap.com上找到)具有弹出窗口,当用户将鼠标悬停在地图上的某个点上时会弹出该弹出窗口。 But Firefox struggles with this feature. 但是Firefox在此功能方面苦苦挣扎。 I'm looking for help with the following code to speed it up: 我正在寻找以下代码的帮助以加快速度:

var selectMouseMove = new ol.interaction.Select({  
    condition: function (e) {
        return e.originalEvent.type == 'mousemove';
    },
    style: hoverStyle
})

// Change cursor 
var target = map.getTarget();
var jTarget = typeof target === "string" ? $("#" + target) : $(target);
// On Hover, change the mouse cursor and display the name of the site  
$(map.getViewport()).on('mousemove', function (e) {
    var pixel = map.getEventPixel(e.originalEvent);
    var feature = map.forEachFeatureAtPixel(pixel,
         function (feature, layer) {
    return feature;
});

if (feature) {
    map.addInteraction(selectMouseMove)
    jTarget.css("cursor", "pointer");
    var geometry = feature.getGeometry();
    var coord = geometry.getCoordinates();
    popup.setPosition(coord);
    $(element).popover({
        'placement': 'top',
        'html': true,
        'content': feature.get('site_name')
    });
    $(element).popover('show');
} else {
    map.removeInteraction(selectMouseMove)
   jTarget.css("cursor", "");
   $(element).popover('destroy');
  }
});
var element = document.getElementById('popup');

var popup = new ol.Overlay({
    element: element,
    positioning: 'bottom-center',
    stopEvent: false
});
map.addOverlay(popup);

I solved this problem for points only (I had the slow performance problem also on IE 11) with an own function. 我只用自己的功能解决了这个问题(仅在IE 11上也遇到了性能下降的问题)。

// Alternative FeatureAtPixel-Function wegen FF
// Sucht im Rechteck vom Punkt pixel aus in +/- x und +/- y
function FFIE_getFeatureAtPixelQuadrat(pixel, DiffCoord) {
  result = [];
  mousepixel = map.getCoordinateFromPixel(pixel);
  f = vectorSource.getFeatures();
  c = 0;
  for (var i in f) {
    if (f[i].point.flatCoordinates[0] > mousepixel[0] - DiffCoord && f[i].point.flatCoordinates[0] < mousepixel[0] + DiffCoord ) {
    if (f[i].point.flatCoordinates[1] > mousepixel[1] - DiffCoord && f[i].point.flatCoordinates[1] < mousepixel[1] + DiffCoord ) {
      c++;
      result.push(f[i]);
    }}
  }
  if (c > 0) {
    return result;
  }
}

The parameter DiffCoord I get on "moveend" event: 我在“ moveend”事件上得到的参数DiffCoord:

// Event, wenn Zoom neu gesetzt
map.on("moveend", function(evt) {
  // Berechnen flatCoordinates per Pixel
  var pixel = [0,0];
  startx = map.getCoordinateFromPixel(pixel)[0];
  pixel = [1,1];
  endx = map.getCoordinateFromPixel(pixel)[0];
  DiffCoord = (endx-startx) * 8; // 8 entspricht den +/- Pixeln in flatCoordinates
});

Now IE and FF seems to be equal fast than Chrome for the popup on mousemove. 现在,对于mousemove上的弹出窗口,IE和FF似乎比Chrome快。 This example is only for points because it searches the feature in a square around the origin of the point. 本示例仅用于点,因为它在点的原点周围的正方形中搜索要素。 I hope this helps? 我希望这有帮助?

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

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