openlayer添加面图层和geojson数据

关键是styles和空间坐标系的转换!!!!

var styles=new ol.style.Style({
    fill: new ol.style.Fill({
        color: 'green'
    }),
    stroke: new ol.style.Stroke({
        color: 'red',
        width: 2
    }),
    image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
            color: 'red'
        })
    })
});

var polygon = (new ol.geom.Polygon([[[120.97, 23.1],[115.97, 15.1],[118.97, 13.1],[120.97, 20.1],[120.97, 23.1]]])).transform('EPSG:4326', map.getView().getProjection());
console.log(map.getView().getProjection());
var proper={"type":"火点"}
var geoMarker = new ol.Feature({

    geometry: polygon//(new ol.geom.Point([120.97, 23.1])).transform('EPSG:4326', map.getView().getProjection())

});

geoMarker.setProperties(proper,true)

var source=new ol.source.Vector({
    features: (new ol.format.GeoJSON({featureProjection: 'EPSG:3857'})).readFeatures(geojsonObject)
});

var vectorLayer = new ol.layer.Vector({
    source: source,
    style:styles,
    opacity:0.5//styles['route']
});
map.addLayer(vectorLayer);

转载自:https://blog.csdn.net/qq_21048515/article/details/81708158

You may also like...