Atlaslog
Atlaslog / Examples / OpenLayers / Projection and Scale
OpenLayers · Runnable Example

Projection and Scale

Runnable OpenLayers example: Projection and Scale. 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-projection-and-scale.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-projection-and-scale.html
<!--
  示例:投影与比例
  要点:view.getProjection() 读取投影元数据,resolution 乘 metersPerUnit 得实地米数
  依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
-->
<!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>投影与比例</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;color:#16181d}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:18px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar b{color:#16181d;font-weight:600;font-variant-numeric:tabular-nums}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <span>投影 <b id="code">—</b></span>
  <span>单位 <b id="unit">—</b></span>
  <span>缩放 <b id="zoom">—</b></span>
  <span>1 像素 = <b id="res">—</b> 米</span>
  <span>光标 <b id="ll">—</b></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 (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  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'}) })],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.391, 39.907]),
      zoom: 10
    }),
    controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
  });

  var view = map.getView();
  var proj = view.getProjection();

  document.getElementById('code').textContent = proj.getCode();
  document.getElementById('unit').textContent = proj.getUnits();
  var extent = proj.getExtent();
  if (extent) {
    /* 投影范围换算成公里,方便看清米制投影的范围 */
    var w = (extent[2] - extent[0]) / 1000;
    var h = (extent[3] - extent[1]) / 1000;
    document.getElementById('unit').textContent =
      proj.getUnits() + ' · 范围 ' + Math.round(w) + ' × ' + Math.round(h) + ' km';
  }

  function refresh() {
    var res = view.getResolution();
    var mpu = proj.getMetersPerUnit() || 1;
    document.getElementById('res').textContent = (res * mpu).toFixed(2);
    document.getElementById('zoom').textContent = view.getZoom().toFixed(2);
  }

  view.on('change:resolution', refresh);
  refresh();

  map.on('pointermove', function (evt) {
    if (evt.dragging) return;
    var ll = ol.proj.toLonLat(evt.coordinate);
    document.getElementById('ll').textContent =
      ll[0].toFixed(4) + ', ' + ll[1].toFixed(4);
  });

  /* 比例尺控件按当前投影的 metersPerUnit 计算,投影换成英尺制会同步变化 */
  map.addControl(new ol.control.ScaleLine({ units: 'metric' }));
})();
</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 · EXAMPLERaster OpenLayers · EXAMPLERectangle OpenLayers · EXAMPLERegion Growing