简体   繁体   English

OpenLayers:如何在地图div的流体CSS渲染后重新对齐鼠标坐标和矢量层

[英]OpenLayers: How to re-align mouse coordinates and vector layers after fluid css rendering of map div

My OpenLayers application includes several vector layers that interact with mouse movements. 我的OpenLayers应用程序包括几个与鼠标移动交互的矢量层。 Everything was working as expected until I made one change to my css file. 一切都按预期进行,直到我对css文件进行了一次更改。 In my html, I defined a div: 在我的html中,我定义了一个div:

<div id="main_map" class="map"></div>

My original/working css file includes: 我的原始/工作CSS文件包括:

#main_map {
 position: absolute;
 left: 200px;
 top: 0px;
 height: 600px;
 width: 1000px;
}

To give the map div a fluid width, I changed the css file to: 为了给map div赋予流体宽度,我将css文件更改为:

#main_map {
 position: absolute;
 left: 200px;
 top: 0px;
 height: 600px;
 width: calc(100% - 487px);
}

Now when the map displays, there is some misalignment between the vector layers and the mouse coordinates, so my mouse-hover functions do not work correctly. 现在,当地图显示时,矢量层和鼠标坐标之间会出现一些未对齐的情况,因此我的鼠标悬停功能无法正常工作。 If I change the size of the browser window just slightly, a redraw or refresh of the map div is invoked which makes this misalignment problem disappear. 如果我稍稍更改浏览器窗口的大小,则将调用map div的重绘或刷新,这将使此未对齐问题消失。 I visibly notice a slight shifting of vector elements on the map upon slightly resizing the browser window. 我明显注意到,在稍微调整浏览器窗口的大小后,地图上矢量元素略有偏移。

The following are my OpenLayer code snippets they may be relevant to this problem...first establish a map: 以下是我的OpenLayer代码段,它们可能与此问题有关...首先建立一个映射:

  var map = new ol.Map({
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ],
    target: 'main_map',
    controls: ol.control.defaults({
      attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
        collapsible: true
      })
    })
  });

Next, I construct several vector layers (details not shown here) and group a few of them as: 接下来,我构造了几个矢量层(细节未在此处显示)并将其中的一些分组为:

  var vector_layer_all = new ol.layer.Group({ layers: [vector_layer,vector_layer2] });

Next, I set my zoom extent based on selected vector layer content: 接下来,我根据选定的矢量图层内容设置缩放范围:

  var extent = ol.extent.createEmpty();
  vector_layer_all.getLayers().forEach(function(layer) {
    ol.extent.extend(extent, layer.getSource().getExtent());
  });
  map.getView().fit(extent, map.getSize());

Finally, I display some text data in a text box div named map_location_info based on the position of the mouse over some vector layers on the map: 最后,根据鼠标在地图上某些矢量层上的位置,在名为map_location_info的文本框div中显示一些文本数据:

  var displayFeatureInfo = function(pixel) {
    var features = [];
    map.forEachFeatureAtPixel(pixel, function(feature,layer) {
      features.push(feature);
    }, null, function(layer) {
      return (layer === vector_layer | layer === vector_layer2 | layer === vector_layer3);
    });
    if (features.length > 0) {
      var info = [];
      var i, ii;
      for (i = 0, ii = features.length; i < ii; ++i) {
        info.push(features[i].get('Name'));
        info.push(features[i].get('Status'));
      }
      document.getElementById('map_location_info').innerHTML = info.join(', ') || '(unknown)';
    } else {
      document.getElementById('map_location_info').innerHTML = '&nbsp;';
    }
  };
  map.on('pointermove', function(evt) {
    if (evt.dragging) {
      return;
    }
    var pixel = map.getEventPixel(evt.originalEvent);
    displayFeatureInfo(pixel);
  });

Somehow I need to force a redraw or refresh to some OpenLayers component just after the initial rendering of the map and vector layers. 在刚渲染完地图和矢量层之后,我需要以某种方式强制重新绘制或刷新某些OpenLayers组件。 At what point in the code/process should I redraw/refresh and which OpenLayers component should this refresh/redraw be applied to? 我应该在代码/过程中的什么时候重绘/刷新,以及应该将此刷新/重绘应用于哪个OpenLayers组件? Any suggestions would be appreciated. 任何建议,将不胜感激。

Edit: In thinking about this further...the OpenLayers script is positioned inside the hmtl content section of a JQuery tab. 编辑:在进一步考虑... OpenLayers脚本位于JQuery选项卡的hmtl内容部分内。 The problem might be coming from the way JQuery renders its tab content. 问题可能出在JQuery呈现其选项卡内容的方式上。

I was able to resolve the issue by moving the OpenLayers script block from inside a JQueryUI tab content space to the $(document).ready(function() { }); 通过将OpenLayers脚本块从JQueryUI选项卡内容空间内部移至$(document).ready(function() { });我能够解决此问题$(document).ready(function() { }); script block at the bottom of my html template (and below a bunch of script using the DataTables library within that script block). 脚本块位于我的html模板的底部(以及在该脚本块中使用DataTables库的一堆脚本下面)。 So, all that remains in the JQueryUI tab content spaces are divs with no inline scripts. 因此,JQueryUI选项卡内容空间中保留的所有内容都是没有内联脚本的div。 I did not attempt to trace out the css propagation so I really don't know exactly what was causing my problem. 我没有尝试找出CSS传播,所以我真的不知道是什么导致了我的问题。 With several high level js libraries (JQuery, JQueryUI, DataTables, OpenLayers - which includes Google Closure and more) playing together in the same space I need to be careful about the order in which things are getting done. 在同一个空间中同时使用几个高级js库(JQuery,JQueryUI,DataTables,OpenLayers-包括Google Closure等),我需要注意事情的完成顺序。 So, my problem was not an OpenLayers problem, but rather a code organization and ordering problem. 因此,我的问题不是OpenLayers问题,而是代码组织和订购问题。 This problem/solution reminds me of some good advice I didn't take from my earlier days of learning javascript. 这个问题/解决方案使我想起了一些我从早期学习javascript时没有得到的好建议。

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

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