openlayers读取KML并显示在地图上

在ol里面读取KML文件时,如果style总是不对,需要设置一个重要的属性 extractStyles: false 

不多说了,上代码示例

 var projection = ol.proj.get('EPSG:3857');
        var map = new ol.Map({
            target: 'map',
            view: new ol.View({
                center: ol.proj.transform([104.06, 30.67], 'EPSG:4326', 'EPSG:3857'),
                zoom: 7,
                projection: projection,
            })
        });
        var layer = new ol.layer.Tile({
            title: "基础地图",
            source: new ol.source.TileArcGISRest({
                url: 'http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer'
            })
        });
        map.addLayer(layer);
 
var styles = new ol.style.Style({
            fill: new ol.style.Fill({
                color: 'rgba(245, 0, 255, 0.5)'
            }),
            stroke: new ol.style.Stroke({
                color: '#ffcc33',
                width: 2
            })
        });  
var vectorSource2 = new ol.source.Vector({
            url: '../data/chengdu.kml',
            format: new ol.format.KML({
                dataProjection: 'EPSG:4326',
                featureProjection: 'EPSG:3857',
                extractStyles: false //至关重要
            }),
            projection: 'EPSG:3857'
        });
        var kmlLayer = new ol.layer.Vector({
            source: vectorSource2,
            style: styles
        });
        map.addLayer(kmlLayer);

技术交流请留言

转载自:https://blog.csdn.net/GIS_Lyn/article/details/84787431

You may also like...

退出移动版