OpenLayers 3加載矢量地圖源
一、矢量地圖
矢量圖使用直線和曲線來描述圖形,這些圖形的元素是一些點、線、矩形、多邊形、圓和弧線等等,它們都是通過數學公式計算獲得的。由于矢量圖形可通過公式計算獲得,是以矢量圖形檔案體積一般較小。矢量圖形最大的優點是無論放大、縮小或旋轉等不會失真。在地圖中存在着大量的應用,是地圖資料中非常重要的組成部分。
為了便于存儲,傳遞,使用,矢量地圖會按照一定的格式來表達,比如常見的
GeoJSON
,
TopoJSON
,
GML
,
KML
,
ShapeFile
等等。 除了最後一個
ShapeFile
,其他幾個格式的矢量地圖OpenLayers 3都支援。
二、使用GeoJson格式加載矢量地圖
1、項目結構
2、map.geojson
3、map.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :加載矢量地圖</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
</head>
<body>
<div id='map' style='width: 1000px;height: 800px;margin: auto'></div>
<script>
/**
* 建立地圖
*/
new ol.Map({
// 設定地圖圖層
layers: [
//建立一個使用Open Street Map地圖源的圖層
new ol.layer.Tile({
source: new ol.source.OSM()
}),
//加載一個geojson的矢量地圖
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'geojson/map.geojson', // 地圖來源
format: new ol.format.GeoJSON() // 解析矢量地圖的格式化類
})
})
],
// 設定顯示地圖的視圖
view: new ol.View({
center: [104,30], // 設定地圖顯示中心于經度104度,緯度30度處
zoom: 10, // 設定地圖顯示層級為10
projection: 'EPSG:4326' //設定投影
}),
// 讓id為map的div作為地圖的容器
target: 'map'
})
</script>
</body>
</html>
4、運作結果
三、擷取矢量地圖上的所有Feature,并設定樣式
1、map2.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :擷取矢量地圖上的所有Feature,并設定樣式</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
</head>
<body>
<div id='map' style='width: 800px;height:500px;margin: auto'></div>
<br>
<div style='width: 800px;margin: auto'>
<button type="button" onclick = 'updateStyle()' >修改Feature樣式</button>
</div>
<script>
/**
* 建立地圖
*/
var map = new ol.Map({
// 設定地圖圖層
layers: [
//建立一個使用Open Street Map地圖源的圖層
new ol.layer.Tile({
source: new ol.source.OSM()
}),
],
// 設定顯示地圖的視圖
view: new ol.View({
center: [104,30], // 設定地圖顯示中心于經度104度,緯度30度處
zoom: 10, // 設定地圖顯示層級為10
projection: 'EPSG:4326' //設定投影
}),
// 讓id為map的div作為地圖的容器
target: 'map'
});
//建立一個矢量地圖源圖層,并設定樣式
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'geojson/map.geojson', // 地圖來源
format: new ol.format.GeoJSON() // 解析矢量地圖的格式化類
}),
// 設定樣式,顔色為綠色,線條粗細為1個像素
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
size: 1
})
})
});
map.addLayer(vectorLayer);
/**
* 擷取矢量圖層上所有的Feature,并設定樣式
*/
function updateStyle(){
//建立樣式,顔色為紅色,線條粗細為3個像素
var featureStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
size: 3
})
})
//擷取矢量圖層上所有的Feature
var features = vectorLayer.getSource().getFeatures()
//周遊所有的Feature,并為每個Feature設定樣式
for (var i = 0;i<features.length;i++){
features[i].setStyle(featureStyle)
}
}
</script>
</body>
</html>
2、運作結果
4、矢量地圖坐标系轉換
矢量地圖用的是
EPSG:4326
,我們可以通過OpenLayers 3内置了地圖格式解析器,将坐标轉換為
EPSG:3857
1、map3.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :矢量地圖坐标系轉換</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
<script src="jquery-3.6.0.js"></script>
</head>
<body>
<div id='map' style='width: 1000px;height: 800px;margin: auto'></div>
<script>
/**
* 建立地圖
*/
var map = new ol.Map({
// 設定地圖圖層
layers: [
//建立一個使用Open Street Map地圖源的圖層
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
// 設定顯示地圖的視圖
view: new ol.View({
center: ol.proj.fromLonLat([104,30]), // 設定地圖顯示中心于經度104度,緯度30度處
zoom: 10, // 設定地圖顯示層級為10
}),
// 讓id為map的div作為地圖的容器
target: 'map'
});
// 加載矢量地圖
function addGeoJSON(data) {
var layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(data, { // 用readFeatures方法可以自定義坐标系
dataProjection: 'EPSG:4326', // 設定JSON資料使用的坐标系
featureProjection: 'EPSG:3857' // 設定目前地圖使用的feature的坐标系
})
})
});
map.addLayer(layer);
};
$.ajax({
url: 'geojson/map.geojson',
success: function(data, status) {
// 成功擷取到資料内容後,調用方法将矢量地圖添加到地圖
addGeoJSON(data);
}
});
</script>
</body>
</html>