
source 是 Layer 的重要组成部分,表示图层的来源,也就是服务地址。
除了在构造函数中指定外,还可以使用 layer.setSource(source) 稍后指定。
上面介绍的都是可以实例化的类,还有一部分基类,不能被实例化,只负责被继承,有:
矢量图层的数据源
包含四个事件, addfeature , changefeature , clear , removefeature 。
addfeature ,当一个要素添加到 source 中触发。
changefeature ,当要素变化时触发。
clear ,当 source 的 clear 方法调用时候触发。
removefeature ,当要素移除时候触发。
接受的参数:
features 方法
假如有一个包含空间数据的字符串,geojsonObject,是GeoJSON字符串格式,那么可以用来初始化一个图层。
url + format 方法
如果有一个文件作为数据源,那么可以配置 url 属性来加载数据:
这两种方法中都会指定数据来源格式, 矢量数据源支持的格式包含很多:gml、EsriJSON、geojson、gpx、igc、kml、osmxml、ows、polyline、topojson、wfs、wkt、wms capabilities(兼容 wms 的格式)、 wms getfeatureinfo、 wmts capabilities、xlink、xsd等格式。这些格式都有readFeatures 、readFeature 和readGeometry 方法用于读取数据。
提供被切分为切片的图片地图数据
可配置的选项
与 vector 一样的选项就不介绍了,介绍与 vector 特有的选项:
接受的事件
提供单一的图片地图。
可以配置的选项
触发的事件
source 是 layer 中必须(required)的选项,定义了地图数据的来源,与数据有关的函数,如addfeature、getfeature等函数都定义在 source 中,而且数据源支持多种格式。
获取GeoJSON数据,可以通过http请求获取,也可以自己定义json数据。
注意格式
var geojsonObject = { 'type': 'FeatureCollection', 'crs': { 'type': 'name', 'properties': { 'name': 'EPSG:3857'}
}, 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [0, 0]
}
}, { 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': [[4e6, -2e6], [8e6, 2e6]]
}
}, { 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': [[4e6, 2e6], [8e6, -2e6]]
}
}, { 'type': 'Feature', 'geometry': { 'type': 'Polygon', 'coordinates': [[[-5e6, -1e6], [-4e6, 1e6], [-3e6, -1e6]]]
}
}, { 'type': 'Feature', 'geometry': { 'type': 'MultiLineString', 'coordinates': [ [[-1e6, -7.5e5], [-1e6, 7.5e5]], [[1e6, -7.5e5], [1e6, 7.5e5]], [[-7.5e5, -1e6], [7.5e5, -1e6]], [[-7.5e5, 1e6], [7.5e5, 1e6]]
]
}
}, { 'type': 'Feature', 'geometry': { 'type': 'MultiPolygon', 'coordinates': [ [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6], [-3e6, 6e6]]], [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6], [0, 6e6]]], [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6], [3e6, 6e6]]]
]
}
}, { 'type': 'Feature', 'geometry': { 'type': 'GeometryCollection', 'geometries': [{ 'type': 'LineString', 'coordinates': [[-5e6, -5e6], [0, -5e6]]
}, { 'type': 'Point', 'coordinates': [4e6, -5e6]
}, { 'type': 'Polygon', 'coordinates': [[[1e6, -6e6], [2e6, -4e6], [3e6, -6e6]]]
}]
}
}]
}
创建矢量图层Source
var vectorSource = new ol.source.Vector({features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
})
创建图层并绑定Source
var vectorLayer = new ol.layer.Vector({source: vectorSource,
style: ''
})
添加到map中即可
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)