Skip to content

数据源 ​

​ 添加图层时,需要提供source,令渲染器能够使用数据去绘图。图层数据源包括type和url,数据源类型包括:

  1. geojson
  2. video
  3. image
  4. canvas

​ 本章将为您介绍数据源相关操作:添加、获取、修改和移除。

添加数据源 ​


​ 通过addSource方法。以geojson为例,添加数据源可以通过两种方式:直接使用url或者geojson类型数据

javascript
map.addSource('yourSourceId', {
    type: 'geojson',
    data: 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_ports.geojson'
});
javascript
map.addSource('yourSourceId', {
   type: 'geojson',
   data: {
       "type": "FeatureCollection",
       "features": [{
           "type": "Feature",
           "properties": {},
           "geometry": {
               "type": "Point",
               "coordinates": [114.138947, 22.373061]
           }
       }]
   }
});

获取数据源 ​


javascript
map.getSource('yourSourceId')

修改数据源 ​


javascript
map.getSource('yourSourceId').setData({
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": { "name": "Null Island" },
      "geometry": {
          "type": "Point",
          "coordinates": [ 0, 0 ]
      }
  }]
});

移除数据源 ​


javascript
map.removeSource('yourSourceId');

V3.2-202604301400