openlayer中添加Geojson矢量数据
1.创建矢量图层源,用于加载矢量feature
2.使用ol提供的GeoJSON 类初始化对象,读取你的geojson对象中的feature列表
3.将遍历出来的feature列表遍历设置样式,并添加到矢量图层源中
geojson:new GeoJSON(),
/** * 创建矢量图层源 */ buildFeatureSource(source) { this[source] = new Vector({ source: new SourceVector(), wrapX: false }); this.map.addLayer(this[source]); //地图中捕捉鼠标点击事件 this.map.un('click',this.getFeatureEvent); this.map.on('click',this.getFeatureEvent); },
/** * 设置多边形的样式 */ setPolygonStyle(strokeColor, fillColor,width) { let fill,stroke; if (fillColor) { fill = new Fill({color: fillColor}); } if(strokeColor){ stroke = new Stroke({ color: strokeColor, width }) } return new Style({ fill, stroke, }) },
/** * 加载GEOJSON 到ol中 * @param geojsonObject */ addFeatureByGeojson(geojsonOb