简体   繁体   English

重新定位后Javascript OpenLayers填充不透明度增加

[英]Javascript OpenLayers fill opacity increases after repositioning

I have created a test project where i'm trying to learn how to work with OpenLayers.我创建了一个测试项目,我试图在其中学习如何使用 OpenLayers。 I try to do a get request to a remote geoservice.我尝试向远程地理服务发出 get 请求。 Every time I reposition the filled polygons on my map get there opacity increased.每次我在地图上重新定位填充的多边形时,不透明度都会增加。 So I think the polygones are put on top of each other.所以我认为多边形是相互叠加的。 Here you can view the URL that I from where i get my JSON data.在这里,您可以查看我从中获取 JSON 数据的 URL。 This is done over a Node JS server on localhost beceause the original link doesn't support CORS.这是通过本地主机上的 Node JS 服务器完成的,因为原始链接不支持 CORS。

var text =
      "http://localhost:3030/map/https://geoservices.landbouwvlaanderen.be/PUBLIC/wfs?service=WFS&request=GetFeature&version=1.1.0&typename=PUBLIC:LBGEBRPERC2019&srsname=EPSG:3857&outputFormat=application/json&count=1000&bbox=" +
      extent.join(",") +
      ",EPSG:3857";

This is the link to my codesandbox project.这是我的 codeandbox 项目的链接。 https://codesandbox.io/embed/vector-wfs-7thkt?fontsize=14&hidenavigation=1&theme=dark https://codesandbox.io/embed/vector-wfs-7thkt?fontsize=14&hidenavigation=1&theme=dark

If I change the webservice URL the problem is gone and everything behaves normaly.如果我更改网络服务 URL,问题就消失了,一切都正常。 But the point is to get it to work with this URL.但关键是让它与这个 URL 一起工作。 If you are experiencing problems with CORS using the standard URL, you can install a CORS extension in Chrome from this link: https://chrome.google.com/webstore/detail/moesif-orign-cors-changer/digfbfaphojjndkpccljibejjbppifbc?hl=nl如果您在使用标准 URL 时遇到 CORS 问题,可以通过以下链接在 Chrome 中安装 CORS 扩展程序: https : //chrome.google.com/webstore/detail/moesif-orign-cors-changer/digfbfaphojjndkpccljibejjbppifbc?hl= nl

var text =
      "https://geoservices.landbouwvlaanderen.be/PUBLIC/wfs?service=WFS&request=GetFeature&version=1.1.0&typename=PUBLIC:LBGEBRPERC2019&srsname=EPSG:3857&outputFormat=application/json&count=1000&bbox=" +
      extent.join(",") +
      ",EPSG:3857";
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import GeoJSON from "ol/format/GeoJSON";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import { bbox as bboxStrategy } from "ol/loadingstrategy";
import BingMaps from "ol/source/BingMaps";
import VectorSource from "ol/source/Vector";
import { Style, Fill, Stroke } from "ol/style";
import Select from "ol/interaction/Select";

var raster = new TileLayer({
  source: new BingMaps({
    imagerySet: "Aerial",
    key: "AsVf4lj-tiANM5N4_P56DC_oQQM9fjb0lMosBxFtgovzGEgcMnQuqYpeKpX-1KL2"
  })
});

var vectorSource = new VectorSource({
  format: new GeoJSON(),
  url: function (extent) {
    var text =
      "http://localhost:3030/map/https://geoservices.landbouwvlaanderen.be/PUBLIC/wfs?service=WFS&request=GetFeature&version=1.1.0&typename=PUBLIC:LBGEBRPERC2019&srsname=EPSG:3857&outputFormat=application/json&count=1000&bbox=" +
      extent.join(",") +
      ",EPSG:3857";

    console.log(text);
    return text;
  },
  strategy: bboxStrategy
});

var vector = new VectorLayer({
  source: vectorSource,
  style: new Style({
    fill: new Fill({
      color: "rgba(255,0,0,0.3)"
    }),
    stroke: new Stroke({
      color: "rgba(255,0,0,0.3)"
    })
  })
});

var map = new Map({
  layers: [raster, vector],
  target: document.getElementById("map"),
  view: new View({
    center: [589973.4805179046, 6575521.818939352],
    maxZoom: 19,
    zoom: 15
  })
});
// ref to currently selected interaction

var changeInteraction = function () {
  var select = new Select();
  map.removeInteraction(select);
  map.addInteraction(select);
};

/**
 * onchange callback on the select element.
 */
changeInteraction();

The service is returning a different feature id for the same feature at each call, so the bbox strategy won't work correctly and the features are being repeated该服务在每次调用时为同一功能返回不同的功能 ID,因此 bbox 策略将无法正常工作并且功能正在重复

在此处输入图片说明

It looks like the OBJ_ID property uniquely identifies features so you would need a custom loader to set the feature id to OBJ_ID, eg看起来 OBJ_ID 属性唯一地标识了功能,因此您需要一个自定义加载程序来将功能 ID 设置为 OBJ_ID,例如

     var features = vectorSource.getFormat().readFeatures(xhr.responseText));
     features.forEach(function(feature) {
       feature.setId(feature.get('OBJ_ID'));
     });
     vectorSource.addFeatures(features);

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

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