登录天地图的官方网站,创建一浏览器应用,获取其中的键值,
(https://www.tianditu.gov.cn/)
创建一index.html文件并编辑,将如下代码复制到其中,保存,双击打开index.html浏览器访问
<!doctype html>
<html lang="en">
<head>
<link href="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol.css" rel="stylesheet">
<style>
.map {
height: 800px;
width: 100%;
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol.js"></script>
<title>OpenLayers 3 example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({url: "http://t3.tianditu.com/DataServer?T=img_w&tk=8ab56038a0e4a31f936efd99726a1573&x={x}&y={y}&l={z}"})
}),
new ol.layer.Tile({
source: new ol.source.XYZ({url: "http://t1.tianditu.com/DataServer?T=cia_w&tk=8ab56038a0e4a31f936efd99726a1573&x={x}&y={y}&l={z}"}),
}),
],
view: new ol.View({
projection: ol.proj.get("EPSG:4326"),
center:[104.07, 30.7], //初始化地图中心点
zoom: 5
})
});
</script>
</body>
</html>
运行结果如下: