简体   繁体   English

打开图层检查功能是否在视口中

[英]Open layer check if feature is in viewport

My purpose is to display only features that have coordinate inside my map viewport (map area currently displayed).我的目的是仅显示在我的 map 视口(当前显示的地图区域)内具有坐标的要素。

I get the extent of the viewport by doing:我通过执行以下操作来获得视口的范围:

var mapExtent = this.map.getView().calculateExtent(this.map.getSize());
mapExtent = ol.proj.transformExtent(mapExtent, 'EPSG:3857', 'EPSG:4326');

and after, in a loop where I am reading the element of one store,然后,在我正在读取一家商店的元素的循环中,

var point = ol.proj.fromLonLat([
      element.get('longitude'),
      element.get('latitude')
]);

elementPoint = new ol.geom.Point(point);
var feature = new ol.Feature(elementPoint);
var coordsFeatures = feature.getGeometry().getCoordinates();

and after, just to quickly see if the point is inside my viewport, I use just a console log:之后,为了快速查看该点是否在我的视口内,我只使用控制台日志:

console.log(ol.extent.containsXY(mapExtent, coordsFeatures[0],coordsFeatures[1]));
/*if(!ol.extent.containsXY(mapExtent, coordsFeatures[0],coordsFeatures[1])){
     return true;
}*/

But I don't know why, the result is only false, even if for sure there are points inside the map area currently displayed.但我不知道为什么,结果只是假的,即使在当前显示的map区域内肯定有点。

Whart I am doing wrong?我做错了什么?

You are mixing projections.您正在混合预测。

For the extent, you change from 3857 to 4326.对于范围,您从 3857 更改为 4326。

For the point, by applying ol.proj.fromLonLat() , you change from 4326 to 3857 (the default )对于这一点,通过应用ol.proj.fromLonLat() ,您可以从 4326 更改为 3857 (默认值

Just use 3857 or 4326 for both只需使用 3857 或 4326 即可

So I will post the correct code in case someone else may need it.所以我会发布正确的代码以防其他人需要它。 As mentioned by @GH (thank you again) my problem was the mixing projections, so I changed my code in this way:正如@GH(再次感谢)所提到的,我的问题是混合投影,所以我以这种方式更改了我的代码:

instead of using the coordinate, I get the extent of the feature:而不是使用坐标,我得到了特征的范围:

var extentFeature = feature.getGeometry().getExtent();

and after i applied to it the same transformExtent used for the map:在我应用了与 map 相同的 transformExtent 之后:

extentFeature = ol.proj.transformExtent(extentFeature, 'EPSG:3857', 'EPSG:4326');

if(!ol.extent.containsExtent(mapExtent, extentFeature)){
   return true;
}

and now it works: :D现在它可以工作了::D

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

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