Kml Timezones
Runnable OpenLayers example: Kml Timezones. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:KML 时区多边形
要点:内联 KML 经 ol.format.KML 读取,按 ExtendedData 里的时区偏移量着色
依赖: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>KML 时区多边形</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:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>悬停读取:<b id="info">—</b></span>
<span id="legend"></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;
}
/* 内联 KML:四条南北向带状区域,用 ExtendedData 记录 UTC 偏移 */
var kml = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document>' +
'<Placemark><name>UTC+6 区</name><ExtendedData><Data name="offset"><value>6</value></Data></ExtendedData>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'88,18,0 104,18,0 104,52,0 88,52,0 88,18,0' +
'</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark>' +
'<Placemark><name>UTC+7 区</name><ExtendedData><Data name="offset"><value>7</value></Data></ExtendedData>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'104,18,0 112,18,0 112,52,0 104,52,0 104,18,0' +
'</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark>' +
'<Placemark><name>UTC+8 区</name><ExtendedData><Data name="offset"><value>8</value></Data></ExtendedData>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'112,18,0 124,18,0 124,52,0 112,52,0 112,18,0' +
'</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark>' +
'<Placemark><name>UTC+9 区</name><ExtendedData><Data name="offset"><value>9</value></Data></ExtendedData>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'124,18,0 140,18,0 140,52,0 124,52,0 124,18,0' +
'</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark>' +
'</Document></kml>';
var features = new ol.format.KML().readFeatures(kml, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
var palette = {
6: 'rgba(69,123,157,0.45)',
7: 'rgba(86,163,140,0.45)',
8: 'rgba(250,193,86,0.45)',
9: 'rgba(196,110,110,0.45)'
};
function styleFor(feature) {
var offset = Number(feature.get('offset'));
return new ol.style.Style({
fill: new ol.style.Fill({ color: palette[offset] || 'rgba(120,120,120,0.4)' }),
stroke: new ol.style.Stroke({ color: 'rgba(40,45,55,0.6)', width: 1 }),
text: new ol.style.Text({
text: 'UTC+' + offset,
font: '600 13px sans-serif',
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
})
});
}
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: styleFor
});
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'}) }), layer],
view: new ol.View({ center: ol.proj.fromLonLat([114, 34]), zoom: 3 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
map.on('pointermove', function (evt) {
var hit = null;
map.forEachFeatureAtPixel(evt.pixel, function (f) { hit = f; }, { hitTolerance: 2 });
document.getElementById('info').textContent = hit
? hit.get('name') + ' · 偏移 ' + hit.get('offset') + ' 小时'
: '—';
});
document.getElementById('legend').textContent =
Object.keys(palette).map(function (k) { return 'UTC+' + k; }).join(' · ');
})();
</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.