【问题标题】:OpenLayers 5 - Click is a little bit displacedOpenLayers 5 - 点击有点移位
【发布时间】:2018-07-27 07:57:09
【问题描述】:

我担心地图中的一些问题。 click-Event 工作正常,但它有点向右偏移,这意味着如果我点击一个特征的左边一点(可能是 6 像素或其他东西),它会选择该特征,但我实际上没有点击在上面。这意味着,如果有 2 个要素彼此相邻并且我单击左侧边界附近,它会单击右侧而不是左侧。 感觉就像地图注意到了向右一点点的点击。有人也有这个问题吗?

我教 OL 处理 EPSG:25832,因为我们普遍使用它,所以我认为是问题所在。在一遍又一遍地切换所有 EPSG 之后,我发现问题就在那里,与使用哪个 EPSG 无关。

我听说过 hitTolerance,所以将其设置为 0,只是为了测试。

这里是我的地图的创建:

map = new Map({
    controls: defaultControls().extend([
    new FullScreen()
]),
  target: 'map-container',
  layers: [
    new TileLayer({
      source: new XYZSource({
        url: 'http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg',
        projection: 'EPSG:3857'
      })
    })
  ],
  interactions: defaultInteractions({ doubleClickZoom: false }), //deaktiviert Zoom bei Doppelklick
  view: new View({
    center: [630114.604, 5629466.731],
    projection: 'EPSG:25832',
    //extent: transformExtent([4, 46, 16, 56], 'EPSG:4326', projectionName),
    zoom: 2,
    minZoom: 2
  }),
  controls: defaultControls({ "attribution": false }),
});

这里是 Select-Interaction 的添加(问题在于每次交互,这只是我添加它们的示例):

function addSelectInteraction() {
var select = new Select({
source: edit
});
select.on('select', function(evt) {
    for(var i=0; i<evt.selected.length; i++) {
        if(document.getElementById("checkbox" + evt.selected[i].getId()).checked == true){
            console.log("Deselect " + evt.selected[i].getId());
            deselectFeature(evt.selected[i]);
        } else {
            console.log("Select " + evt.selected[i].getId());
            selectFeature(evt.selected[i]);
        }
        select.getFeatures().clear();
    }
});
select.set("name", "select");
addTool(select);

}

selectFeature(feature) 只是设置该特征的样式,所以在这里没有影响。 以及在这里我如何教OL如何处理EPSG.25832(使用proj4):

var projectionName = 'EPSG:25832';
proj4.defs(projectionName, '+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs');
register(proj4);

也许有人也遇到过这个问题?

【问题讨论】:

    标签: click gis openlayers feature-selection openlayers-5


    【解决方案1】:

    我自己解决了这个问题。

    我在我的 JavaScript 代码中向我的 html 添加了一些东西,而地图并不知道这一点。添加完所有内容后,我只需要 map.updateSize() 就可以了。

    【讨论】:

    • 如果您单击切换按钮,我有一个可选的视觉元素会显示在地图上方,这有助于我解决问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2023-03-24
    • 2020-08-04
    相关资源
    最近更新 更多