openlayer5在天地圖上畫線
原創
花舞月咏潭
2019-07-05 10:40
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
}
</style>
<script src="https://openlayers.org/en/v5.3.0/build/ol.js" type="text/javascript">
</script>
<title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var lineCoords_org=[[120, 40],[116,35]];
var lineGeom=new ol.geom.LineString(lineCoords_org);
lineGeom.transform("EPSG:4326", "EPSG:3857");
var feature2 = new ol.Feature({
geometry: lineGeom
});
var vLayer2 = new ol.layer.Vector({
source: new ol.source.Vector({features:[feature2]})
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'https://t0.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=2ce94f67e58faa24beb7cb8a09780552'
})
}),
new ol.layer.Tile({
source: new ol.source.XYZ({
url:'https://t0.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=2ce94f67e58faa24beb7cb8a09780552'
})
}),
vLayer2
],
view: new ol.View({
center: ol.proj.fromLonLat([112.73, 38.42]),
zoom: 4
})
});
</script>
</body>
</html>