openlayers常用操作
1.坐标转换
根据当前坐标系与目标坐标系进行转换
ol.proj.transform(coordinate, source, destination); //coordinate:数组;source:当前坐标编码;destination:目标坐标编码
从经纬度转化到指定坐标系
ol.proj.fromLonLat(coordinate, opt_projection)
从某坐标转经纬度
ol.proj.toLonLat(coordinate, opt_projection)
从源投影变换到目标投影
ol.proj.transformExtent(extent,source,destination)
2.常用样式
var style = new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'magenta', width: 2 }), fill: new ol.style.Fill({ color: 'magenta' }), image: new ol.style.Circle({ radius: 10, fill: null, stroke: new ol.style.Stroke({ color: 'magenta' }) }), text: new ol.style.Text({ font: '12px Calibri,sans-serif', text: text, fill: new ol.style.Fill({ color: '#000' }), stroke: new ol.style.Stroke({ color: '#f00', width: 3 }) }) })
3.分级设置样式
var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'https://openlayers.org/en/v4.1.1/examples/data/geojson/countries.geojson', format: new ol.format.GeoJSON() }), style: function(feature, resolution) { style.getText().setText(resolution < 5000 ? feature.get('name') : ''); return style; } });