简体   繁体   English

GEOjson未显示在我的地图openlayers中

[英]GEOjson not displaying in my map openlayers

I want to display geojson data in my map openLayers but my data didn't show and the map doesn't even display. 我想在地图openLayers中显示geojson数据,但是我的数据没有显示,地图甚至都没有显示。 I work with openLayers 5. I have an api (in node.js) which allows to extract the data in my database. 我使用openLayers5。我有一个api(位于node.js中),可用来提取数据库中的数据。 I have a file (script.js) which allows to display the map, recover the data sent by the api and display the data on the map. 我有一个文件(script.js),该文件可显示地图,恢复api发送的数据并在地图上显示数据。

In script.js : 在script.js中:

I create a new vectorLayer which contain the style of the geojson : 我创建一个新的vectorLayer,其中包含geojson的样式:

 var foncier2 = new VectorLayer({
source:source,
style: function (feature, res) {
  property = feature.get("nature");
  return new  Style({
    stroke: new Stroke({
      color: [40, 40, 40, 1],
      width: 0.3
    }),
    fill: new Fill({
      color: couleur(property)
    })
  })
},});

I request on my API to recover the data with the help of a callback : 我要求我的API在回调的帮助下恢复数据:

`
function loadJSON(callback) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
            console.log(this.responseText);
            callback(this.responseText);
          }
        };
        xhr.open("GET", "adressIP:port/endpoint", true);
        // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(null);
      }`

Then I create a function which transform the data into json then into GEOjson : 然后我创建一个函数,将数据转换为json,然后转换为GEOjson:

   var myData = JSON.parse(data);

var geojsonObject = {
 "type": "FeatureCollection",
 "crs": { "type": "name", "properties": { "name": "EPSG:4326" }},
 "features": [
   {
     "type": "Feature",
     "geometry": {
       "type": "Polygon",
       "coordinates":myData[2].geom
     }
   }
 ]};

Finally i display the data : 最后我显示数据:

foncier2.getSource().clear();
foncier2.getSource().addFeatures(foncier2.getSource().getFormat().readFeatures(geojsonObject, {
  dataProjection: 'EPSG:4326',
  defaultFeatureProjection: 'EPSG:3857',
}));
foncier2.getSource().refresh();
foncier2.getSource().changed();
map.getView().fit(foncier2.getSource().getExtent());

But
nothing displays on my map and I don't have error in my console log. 我的地图上没有任何显示,并且控制台日志中没有错误。

Thank you for helping me 感谢你们对我的帮助

PS : ( I managed to recover the data,it looks like that and the coordinates ) PS:(我设法恢复数据,看起来坐标

You are setting coordinates to the geometry (which your screenshot shows is multipolygon) instead of the geometry's coordinates and not updating the type. 您正在将坐标设置为几何图形(屏幕快照显示为多多边形),而不是几何图形的坐标,并且不更新类型。 Try this: 尝试这个:

var geojsonObject = {
 "type": "FeatureCollection",
 "crs": { "type": "name", "properties": { "name": "EPSG:4326" }},
 "features": [
   {
     "type": "Feature",
     "geometry": {
       "type": JSON.parse(myData[2].geom).type,
       "coordinates": JSON.parse(myData[2].geom).coordinates
     }
   }
 ]};

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

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