OpenLayers 教程
对于数据量不是很大的场景,可以直接从数据库读取数据,通过创建点线面要素(Feature),然后添加到矢量图层(vectorLayer)中,在地图上展示。添加点线面的方法可以封装一下,方便使用。
GIS数据最好符合标准,这里介绍 WKT WKB GeoJson 格式数据。
Openlayers 创建矢量图层,添加点线面要素
<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>Feature Layer</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: [116, 39],
zoom: 4,
maxZoom: 18,
minZoom: 1,
})
});
var layer = initVectorLayer();
addFeatures();
function addFeatures() {
var features = [];
features.push(getFeatureByWKT("POINT(116.17983834030585 39.98298600752048)"));
var geojson = {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[116.09129344901807,39.976463050783],[116.12802898368604,39.986934394777144],[116.14845668754346,39.970454902589644],[116.14365016898877,39.952945442140425],[116.11069118461377,39.95037052148613],[116.09129344901807,39.976463050783]]]},"properties":null};
features.push(getFeatureByGeoJson(geojson));
features.push(getFeatureByWKB("0102000020E6100000040000004AB6DE424F095D4024548C542D0144404AB6DE42E10D5D4024548CD46D0444404AB6DE022D115D4024548CD4DBFF43404AB6DE42DB135D4024548CD46D044440"));
layer.getSource().addFeatures(features);
var extent = layer.getSource().getExtent();
map.getView().fit(extent, {
duration: 1,
callback: null,
});
}
function initVectorLayer() {
let source = new ol.source.Vector();
let customVectorLayer = new ol.layer.Vector({
source: source,
zIndex: 2,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 5,
lineDash: [3, 5]
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.3)',
}),
image: new ol.style.Circle({
radius: 9,
fill: new ol.style.Fill({
color: 'red',
})
})
}),
});
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 getFeatureByGeoJson(geojson, sourceCode, targetCode) {
let view = map.getView();
if (!geojson) {
return null;
}
let feature;
if ((typeof geojson) == 'string') {
while (geojson.indexOf('null') != -1) {
geojson = geojson.replace("null", "");
}
}
feature = (new ol.format.GeoJSON()).readFeature(geojson, {
dataProjection: sourceCode || view.getProjection(),
featureProjection: targetCode || view.getProjection()
});
return feature;
}
function getFeatureByWKB(coordinate, sourceCode, targetCode) {
try {
let view = map.getView();
if (!coordinate) {
return null;
}
let format = new ol.format.WKB();
let feature;
if (coordinate.indexOf('010') == 0) {
let confirmEnding = function (str, target) {
var start = str.length - target.length;
var arr = str.substr(start, target.length);
if (arr == target) {
return true;
}
return false;
}
if (confirmEnding(coordinate, '40')) {
feature = (format).readFeature(coordinate, {
dataProjection: sourceCode || view.getProjection(),
featureProjection: targetCode || view.getProjection()
});
}
}
return feature;
} catch (e) {
console.log(e);
return null;
}
}
</script>
</body>
</html>
在线示例
地图添加各种数据格式点线面:Openlayers feature
|