本示例演示加载 HERE 地图瓦片。
HERE Map Tile API used with ol/source/XYZ
.
当使用他们瓦片 API 时,请确认遵守 HERE Service Terms
import 'ol/ol.css';
import Map from 'ol/Map';
import TileLayer from 'ol/layer/Tile';
import View from 'ol/View';
import XYZ from 'ol/source/XYZ';
const apiKey = 'Your HERE Maps API key from https://developer.here.com/';
const hereLayers = [
{
base: 'base',
type: 'maptile',
scheme: 'normal.day',
apiKey: apiKey,
},
{
base: 'base',
type: 'maptile',
scheme: 'normal.day.transit',
apiKey: apiKey,
},
{
base: 'base',
type: 'maptile',
scheme: 'pedestrian.day',
apiKey: apiKey,
},
{
base: 'aerial',
type: 'maptile',
scheme: 'terrain.day',
apiKey: apiKey,
},
{
base: 'aerial',
type: 'maptile',
scheme: 'satellite.day',
apiKey: apiKey,
},
{
base: 'aerial',
type: 'maptile',
scheme: 'hybrid.day',
apiKey: apiKey,
},
];
const urlTpl =
'https://{1-4}.{base}.maps.ls.hereapi.com' +
'/{type}/2.1/maptile/newest/{scheme}/{z}/{x}/{y}/256/png' +
'?apiKey={apiKey}';
const layers = [];
let i, ii;
for (i = 0, ii = hereLayers.length; i < ii; ++i) {
const layerDesc = hereLayers[i];
layers.push(
new TileLayer({
visible: false,
preload: Infinity,
source: new XYZ({
url: createUrl(urlTpl, layerDesc),
attributions:
'Map Tiles © ' +
new Date().getFullYear() +
' ' +
'<a href="https://developer.here.com/" target="_blank">HERE</a>',
}),
})
);
}
const map = new Map({
layers: layers,
target: 'map',
view: new View({
center: [921371.9389, 6358337.7609],
zoom: 10,
}),
});
function createUrl(tpl, layerDesc) {
return tpl
.replace('{base}', layerDesc.base)
.replace('{type}', layerDesc.type)
.replace('{scheme}', layerDesc.scheme)
.replace('{apiKey}', layerDesc.apiKey);
}
const select = document.getElementById('layer-select');
function onChange() {
const scheme = select.value;
for (let i = 0, ii = layers.length; i < ii; ++i) {
layers[i].setVisible(hereLayers[i].scheme === scheme);
}
}
select.addEventListener('change', onChange);
onChange();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HERE Map Tile API</title>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="./resources/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL,TextDecoder,Number.isInteger"></script>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<select id="layer-select">
<option value="normal.day" selected>Normal Day</option>
<option value="normal.day.transit">Normal Day Transit</option>
<option value="pedestrian.day">Pedestrian Day</option>
<option value="terrain.day">Terrain Day</option>
<option value="satellite.day">Satellite Day</option>
<option value="hybrid.day">Hybrid Day</option>
</select>
<script src="main.js"></script>
</body>
</html>
{
"name": "here-maps",
"dependencies": {
"ol": "7.1.0"
},
"devDependencies": {
"parcel": "^2.0.0-beta.1"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --public-url . index.html"
}
}