简体   繁体   English

Openlayers 4.3.1没有显示我的GeoJSON层

[英]Openlayers 4.3.1 is not showing my GeoJSON layer

Please i want to display a layer from GeoJSON file with openlayers 4.3.1 but the result (the vectorLayer variable) show a blank page. 请我想显示来自带有openlayers 4.3.1的GeoJSON文件的图层,但是结果(vectorLayer变量)显示为空白页。 What am i missing? 我想念什么?

The GeoJSON file is here https://gist.githubusercontent.com/abdounasser202/5d830738ad29e6395743530545bd322b/raw/0c34d9a1ced1879432318b6860c1c21e8e88ef04/quartiers_yaounde.geojson GeoJSON文件位于https://gist.githubusercontent.com/abdounasser202/5d830738ad29e6395743530545bd322b/raw/0c34d9a1ced187943231818b6860c1c21e8e88ef04/quartiers_yaounde.geojson

Here is my JS code 这是我的JS代码

    var styles = {
   'Polygon': new ol.style.Style({
     stroke: new ol.style.Stroke({
       color: 'grey',
       width: 1
     }),
     fill: new ol.style.Fill({
       color: 'rgba(0, 0, 255, 0.1)'
     })
   }),
  };

  var styleFunction = function(feature) {
   return styles[feature.getGeometry().getType()];
  };

  var geojsonObject = 'https://gist.githubusercontent.com/abdounasser202/5d830738ad29e6395743530545bd322b/raw/0c34d9a1ced1879432318b6860c1c21e8e88ef04/quartiers_yaounde.geojson';
  console.log("geodata >>> ", geojsonObject);

  var vectorSource = new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        url: geojsonObject
    });

  var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: styleFunction
  });

  var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

  // var extent_degree = [11.4095, 3.71349, 11.5747, 3.9692];

  var map = new ol.Map({
   layers: [vectorLayer],
   target: 'map',
   view: new ol.View({
      // projection: 'EPSG:4326',
      center: ol.proj.fromLonLat([11.5021, 3.8480]),
      zoom: 6
   })
  });

  // ol.proj.get('EPSG:4326').setExtent(extent_degree)

  var units = map.getView().getProjection().getUnits();
  console.log("units >>> ", units);

  var projections = map.getView().getProjection();
  console.log("projections >>> ", projections);

According your code, you are styling Polygons, but no other geometry types. 根据您的代码,您正在设置多边形的样式,但没有其他几何类型的样式。 So you will see only Polygons on your map. 因此,您将在地图上仅看到多边形。 Other geometry types will be invisible since they are not styled. 其他几何类型将不可见,因为它们没有样式。 The GeoJSON file does not contain Polygons, so indeed you won't see anything. GeoJSON文件不包含Polygons,因此您实际上看不到任何东西。

As far as I can see, the GeoJSON file contains only MultiPolygons. 据我所知,GeoJSON文件仅包含MultiPolygons。 To make them visible, add a style for 'MultiPolygon' in the variable styles, or just change the string 'Polygon' in your code to 'MultiPolygon'. 为了使它们可见,请在变量样式中为“ MultiPolygon”添加样式,或者只是将代码中的字符串“ Polygon”更改为“ MultiPolygon”。

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

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