openlayers 地图加载geojson绘制边界,点,线,面

openlayers 地图加载geojson绘制边界,点,线,面

  • 方法
 // 绘制点 / 线 / 面
    drawContent(geo) {
      if (this.routeLayer) {
        this.map.removeLayer(this.routeLayer);
      }

      let features = [];
      geo.features.forEach((g) => {
        let lineData = g.geometry;
        let routeFeature = "";
        if (lineData.type === "MultiPolygon") {
          routeFeature = new Feature({
            geometry: new MultiPolygon(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 0.5)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        } else if (lineData.type === "Polygon") {
          routeFeature = new Feature({
            geometry: new Polygon(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 0.5)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        } else if (lineData.type === "MultiLineString") {
          routeFeature = new Feature({
            geometry: new MultiLineString(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 1)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        } else if (lineData.type === "LineString") {
          routeFeature = new Feature({
            geometry: new LineString(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 1)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        }

        features.push(routeFeature);

        const center = this.getLast(lineData.coordinates);
        this.map.getView().setCenter(center);
      });
      // 设置图层
      this.routeLayer = new VectorLayer({
        source: new VectorSource({
          features: features,
        }),
      });
      // 添加图层
      this.map.addLayer(this.routeLayer);
    },
    // 获取到第一个坐标点,设置成地图中心
    getLast(list) {
      const isArray = list[0] instanceof Array;
      if (!isArray) {
        return list;
      } else {
        return this.getLast(list[0]);
      }
    },
  • geojson示例数据
    获取中国行政区geojson: http://datav.aliyun.com/portal/school/atlas/area_selector
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"adcode":370321,"name":"xx县","center":[118.101556,36.959773],"centroid":[118.026774,36.992528],"childrenNum":0,"level":"district","acroutes":[100000,370000,370300],"parent":{"adcode":370300}},"geometry":{"type":"MultiPolygon","coordinates":[[[[117.940613,36.891605],[117.940224,36.899505],[117.941006,36.899192],[117.940613,36.891605]]]]}}]}

### 回答1: 要加载GeoJSON数据,可以使用OpenLayers的Vector图层。首先,需要创建一个Vector图层对象,然后使用OpenLayers的Format.GeoJSON类来解析GeoJSON数据,并将其添加到Vector图层中。最后,将Vector图层添加到地图中即可。 以下是一个示例代码: ``` var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'path/to/your/geojson/file.geojson', format: new ol.format.GeoJSON() }) }); map.addLayer(vectorLayer); ``` 其中,`url`属性指定GeoJSON文件的路径,`format`属性指定使用OpenLayersGeoJSON解析器来解析数据。最后,将Vector图层添加到地图中即可。 注意:在使用OpenLayers加载GeoJSON数据时,需要确保GeoJSON文件的格式正确,否则可能会导致加载失败。 ### 回答2: Openlayers是一种开源地图框架,支持多种地图底图和图层。其中,加载GeoJSONOpenlayers的一项重要功能。 要加载GeoJSON,首先要创建一个新图层。新图层需要绑定一个数据源,可以是本地的一个GeoJSON文件,也可以是在线GeoJSON链接。在创建图层之后,使用Openlayersol.source.Vector创建一个新的矢量数据源,并将其绑定到新图层。 然后,使用Openlayersol.format.GeoJSON将数据转换为矢量要素,并将其添加到矢量数据源中。最后,将新图层添加到Openlayers地图中。 下是一个加载GeoJSON的代码示例: ```javascript // 创建一个新的地图对象 var map = new ol.Map({ target: 'map', // 地图显示的容器 layers: [ // 地图图层 new ol.layer.Tile({ // 街道地图 source: new ol.source.OSM() }) ], view: new ol.View({ // 地图视图 center: ol.proj.fromLonLat([119.306239, 26.080407]), // 地图中心 zoom: 14 // 地图缩放级别 }) }); // 创建一个新的矢量数据源 var vectorSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), // 数据源格式 url: '/path/to/geojson/file.geojson' // GeoJSON文件路径 }); // 创建一个新的矢量图层 var vectorLayer = new ol.layer.Vector({ source: vectorSource // 图层数据源 }); // 将图层添加到地图中 map.addLayer(vectorLayer); ``` 在上的代码中,我们创建了一个地图对象,并在地图图层中添加了一个OSM街道地图。然后,我们创建了一个新的矢量数据源,并将其绑定到一个新图层中。最后,我们将图层添加到地图中。 值得注意的是,在GeoJSON文件路径中使用了绝对路径。为了方便测试,我们可以将文件放在服务器的根目录下,然后使用绝对路径访问。在实际项目中,我们可能需要使用相对路径或者动态生成路径。 总之,Openlayers提供了简单易用的API,使得加载GeoJSON变得非常容易。在实际应用中,我们可以将GeoJSON数据和地图交互来打造各种应用,例如展示数据统计、可视化、分析等。 ### 回答3: OpenLayers是一种用于Web GIS开发的JavaScript框架,它支持多种数据格式的加载和显示,包括GeoJSONGeoJSON是一个用于地理数据交换的标准数据格式,它可以轻松地在WebGIS应用中实现地图数据的可视化。 在OpenLayers加载GeoJSON数据,需要使用ol.source.Vector类,该类是OpenLayers中用于加载和显示矢量数据的核心类。下是一个加载GeoJSON数据的简单示例: ```javascript var vectorSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: 'path/to/file.geojson' }); var vectorLayer = new ol.layer.Vector({ source: vectorSource }); var map = new ol.Map({ layers: [vectorLayer], target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); ``` 在这个示例中,我们首先创建了一个vectorSource对象,其中包含format和url两个属性,分别表示GeoJSON数据的格式和数据的路径。接着,我们使用vectorSource创建一个vectorLayer对象,并将其添加到地图上。最后,我们创建一个map对象,并将vectorLayer添加到图层数组中,实现了GeoJSON数据的加载和显示。 除了使用url加载GeoJSON数据外,还可以直接使用JavaScript对象或字符串加载数据。这种方式需要将数据转换为GeoJSON格式,然后使用vectorSource.addFeatures(features)方法将数据添加到vectorSource中。 总之,OpenLayers提供了简单而又强大的方法来加载和显示GeoJSON数据,使得开发WebGIS应用变得更加容易和高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值