百度离线瓦片地图
openlayers官网
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>openlayers(版本6)百度离线瓦片地图</title>
<style>
body, #map{
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.6.1/css/ol.css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.6.1/build/ol.js"></script>
</head>
<body>
<div id="map"></div>
</body>
<script>
const projection = 'EPSG:3857'
let resolutions = []
for (let i = 0; i <= 18; i++) {
resolutions[i] = Math.pow(2, 18 - i)
}
const baiduMapLayer = new ol.layer.Tile({
source: new ol.source.TileImage({
projection,
tileGrid: new ol.tilegrid.TileGrid({
origin: [0, 0],
resolutions
}),
tileUrlFunction: tileCoord => {
const z = tileCoord[0]
let x = tileCoord[1]
let y = tileCoord[2] + 1
if (x < 0) {
x = -x
}
if (y < 0) {
y = -y
}
return `../assets/openlayers/tiles/${z}/${x}/${y}.jpg`
}
})
})
const map = new ol.Map({
target: 'map',
layers: [
baiduMapLayer
],
view: new ol.View({
zoom: 9,
maxZoom: 16,
minZoom: 8,
projection,
center: ol.proj.transform([109.956924, 40.620351], 'EPSG:4326', 'EPSG:3857')
})
})
</script>
</html>
在线瓦片地图
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>openlayers(版本6)在线瓦片地图</title>
<style>
body, #map{
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
</head>
<body>
<button onclick="setSatellite()">卫星地图</button>
<button onclick="setStreet()">街道地图</button>
<div id="map"></div>
</body>
<script>
const satellite = new ol.layer.Tile({
visible: false,
source: new ol.source.XYZ({
attributions: '',
url: 'https://webst0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}&scl=1<ype=10'
})
})
const street = new ol.layer.Tile({
visible: true,
source: new ol.source.XYZ({
attributions: '',
url: 'https://webst0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
})
})
const map = new ol.Map({
target: 'map',
layers: [
satellite,
street
],
view: new ol.View({
zoom: 9,
maxZoom: 16,
minZoom: 8,
projection: 'EPSG:4326',
center: [109.956924, 40.620351],
})
})
function setSatellite() {
street.setVisible(false)
satellite.setVisible(true)
}
function setStreet() {
street.setVisible(true)
satellite.setVisible(false)
}
</script>
</html>