OpenLayers 教程
Openlayers 对样式的控制是通过一个通用的样式对象 Style,图层(Layer)和图形要素(Feature)都可以设置 Style 对象,来展示想要的结果。
Style 可以很灵活的设置**点(图标)、线、面的样式,包括基础样式、标签文本(label)、标签背景和边框、渐变色**等。
本示例介绍点线面的 基础样式、标签和渐变色 等常用样式。
Openlayers style样式演示
<html lang="en">
<head>
<meta charSet="utf-8">
<link rel="stylesheet" href="http://openlayers.vip/examples/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
float: left;
}
</style>
<script src="http://openlayers.vip/examples/resources/ol.js"></script>
<script src="./tiandituLayers.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>OpenLayers style</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var map = new ol.Map({
target: 'map',
layers: [
getIMG_CLayer(),
getIBO_CLayer(),
getCIA_CLayer(),
],
view: new ol.View({
projection: "EPSG:4326",
center: [115.67724700667199, 37.73879478106912],
zoom: 6,
maxZoom: 18,
minZoom: 1,
})
});
var defaultStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'white',
width: 2,
}),
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.7)',
}),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: 'white',
})
})
})
var layer = initVectorLayer();
var features = [];
addFeatures();
function addFeatures() {
features.push(getFeatureByWKT("POINT(116.17983834030585 39.98298600752048)"));
features.push(getFeatureByWKT("LINESTRING(113.69619564084466 38.778729673116445,113.85000423459466 39.196210141866445,114.90469173459466 38.712811704366445,115.19033626584466 39.569745298116445,116.22305110959466 38.569989438741445,116.45376400021966 39.789471860616445,117.18984798459466 40.031171079366445)"));
features.push(getFeatureByWKT("POLYGON((115.13540462521966 37.877850766866445,116.31094173459466 39.042401548116445,117.23379329709466 38.130536313741445,117.10195735959466 37.295575376241445,115.64077571896966 36.976971860616445,115.26724056271966 37.174725766866445,115.13540462521966 37.877850766866445))"));
layer.getSource().addFeatures(features);
}
function initVectorLayer() {
let source = new ol.source.Vector();
let customVectorLayer = new ol.layer.Vector({
source: source,
zIndex: 2,
style: defaultStyle,
});
map.addLayer(customVectorLayer);
return customVectorLayer;
}
function getFeatureByWKT(wkt, sourceCode, targetCode) {
try {
let view = map.getView();
if (!wkt) {
return null;
}
let format = new ol.format.WKT();
let feature;
feature = format.readFeature(wkt, {
featureProjection: targetCode || view.getProjection(),
dataProjection: sourceCode || view.getProjection(),
});
return feature;
} catch (e) {
console.log(e);
return null;
}
}
function pointStyle() {
let point = new ol.style.Style({
image: new ol.style.Circle({
radius: 15,
stroke: new ol.style.Stroke({
color: 'red',
width: 3,
}),
scale: 1,
fill: new ol.style.Fill({
color: 'green',
})
})
});
let pointFeature = features[0];
pointFeature.setStyle(point);
}
function RegularStyle() {
let point = new ol.style.Style({
image: new ol.style.RegularShape({
points: 4,
radius: 15,
stroke: new ol.style.Stroke({
color: 'red',
width: 3,
}),
scale: 1,
rotation: 0.1,
rotateWithView: true,
fill: new ol.style.Fill({
color: 'green',
})
})
});
let pointFeature = features[0];
pointFeature.setStyle(point);
}
function markerStyle() {
let point = new ol.style.Style({
image: new ol.style.Icon({
crossOrigin: 'anonymous',
anchor: [0.5, 0],
offset: [0.2, 0],
anchorOrigin: 'bottom-right',
opacity: 0.8,
src: "http://api.tianditu.gov.cn/v4.0/image/marker-icon.png",
scale: 1,
color: 'red',
})
});
let pointFeature = features[0];
pointFeature.setStyle(point);
}
function lineStyle() {
let styleLine = [];
let steps = 10;
for (let i = 0; i < steps; i++) {
styleLine.push(
new ol.style.Style({
stroke: new ol.style.Stroke({
color: [0, 255, 255, 1 / (steps - i)],
width: (steps - i) * 2 - 1
}),
})
);
}
let lineFeature = features[1];
lineFeature.setStyle(styleLine);
}
function polygon() {
var polygonStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
width: 2,
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.3)',
}),
text: new ol.style.Text({
text: '面要素',
offsetX: 0,
offsetY: 0,
textAlign: 'center',
scale: 1,
placement: 'point',
font: 'normal bold 18px Arial,sans-serif',
textBaseline: 'middle',
fill: new ol.style.Fill({
color: 'red'
}),
})
});
let polygonFeature = features[2];
polygonFeature.setStyle(polygonStyle);
}
function label() {
var polygonStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
width: 2,
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.3)',
}),
text: new ol.style.Text({
text: '面要素完成样式',
offsetX: 0,
offsetY: 0,
textAlign: 'center',
scale: 1,
placement: 'point',
textBaseline: 'middle',
rotation: 0,
rotateWithView: false,
padding: [5, 5, 5, 5],
overflow: true,
font: 'normal bold 16px Arial,sans-serif',
fill: new ol.style.Fill({
color: 'rgba(51,51,51, 1)'
}),
stroke: new ol.style.Stroke({
color: 'rgba(0, 255, 255, 0.8)',
width: 2,
}),
backgroundFill: new ol.style.Fill({
color: 'rgba(252,254,255, 1)'
}),
backgroundStroke: new ol.style.Stroke({
color: 'rgba(0, 255, 255, 0.8)',
width: 1,
}),
})
});
let polygonFeature = features[2];
polygonFeature.setStyle(polygonStyle);
}
function restore() {
for (let i = 0; i < features.length; i++) {
features[i].setStyle(defaultStyle);
}
}
</script>
<button id="pointStyle" onClick="pointStyle()">圆点样式</button>
<button id="RegularStyle" onClick="RegularStyle()">规则图形</button>
<button id="markerStyle" onClick="markerStyle()">点(图标)样式</button>
<button id="lineStyle" onClick="lineStyle()">线样式(渐变色)</button>
<button id="polygon" onClick="polygon()">面样式(标注)</button>
<button id="label" onClick="label()">通用标注(背景)</button>
<button id="restore" onClick="restore()">还原所有样式</button>
</body>
</html>
在线示例
Openlayers Style 样式演示:Openlayers style
|