Atlaslog
Atlaslog / Examples / OpenLayers / Localized Openstreetmap
OpenLayers · Runnable Example

Localized Openstreetmap

Runnable OpenLayers example: Localized Openstreetmap. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-localized-openstreetmap.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-localized-openstreetmap.html
<!--
  示例:自定义 OSM 源 — OpenLayers localized-openstreetmap
  核心逻辑:ol.source.XYZ 传 url、attributions、crossOrigin 三项覆盖默认值,版权自绘
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>自定义 OSM 源</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
  .credit{position:absolute;right:8px;bottom:6px;background:rgba(255,255,255,0.86);
       padding:2px 8px;border-radius:3px;font-size:11px;color:#58606e}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="tiles" checked> 自定义瓦片源</label>
  <span id="info">—</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';

  if (typeof ol === 'undefined') {
    document.getElementById('map').innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  /* 显式声明地址模板、版权与跨域策略,便于换成自有瓦片服务 */
  var customOsm = new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', 
    attributions: 'Tiles © Esri · 自定义源',
    attributionsCollapsible: false,
    crossOrigin: 'anonymous',
    maxZoom: 19,
    /* 通过 tileUrlFunction 显式拼地址,等价于覆盖 url 模板 */
    tileUrlFunction: function (coord) {
      var z = coord[0], x = coord[1], y = coord[2];
      if (z < 0 || x < 0 || y < 0) return undefined;
      return 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/' + z + '/' + y + '/' + x;
    }
  });

  var customLayer = new ol.layer.Tile({ source: customOsm });

  var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }),
      customLayer
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.391, 39.907]),
      zoom: 12,
      minZoom: 2,
      maxZoom: 19
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  /* 版权提示自己画,不依赖地图控件 */
  var credit = document.createElement('div');
  credit.className = 'credit';
  credit.textContent = 'Tiles © Esri · 自定义源';
  map.getViewport().appendChild(credit);

  document.getElementById('tiles').addEventListener('change', function () {
    customLayer.setVisible(this.checked);
    document.getElementById('info').textContent = this.checked
      ? '叠加自定义 OSM 源(地址模板 + 版权声明)'
      : '仅显示默认 OSM 源';
  });

  document.getElementById('info').textContent = '叠加自定义 OSM 源(地址模板 + 版权声明)';
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More OpenLayers examples

OpenLayers · EXAMPLEMagnify OpenLayers · EXAMPLEMapbox Layer OpenLayers · EXAMPLEMapbox Style