Цайвар, бараан, таны өгөгдөл
Саарал цайвар ба бараан загвар, дээр нь таны GeoJSON давхарга. Загвар солиход давхарга буцаж орно. Энд Голомт банкны төвийн салбарууд.
Код
html
<!doctype html>
<html lang="mn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Light and dark with your data</title>
<link href="https://dock.mn/sdk/v1/dock.css" rel="stylesheet">
<script src="https://dock.mn/sdk/v1/dock.js"></script>
<style>
body { margin: 0; font: 14px system-ui, sans-serif; }
#map { position: absolute; inset: 0; }
.panel { position: absolute; top: 10px; left: 10px; z-index: 1; max-width: 300px; padding: 8px 10px;
background: #fff; border-radius: 6px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }
</style>
</head>
<body>
<div id="map"></div>
<div class="panel"><div id="looks"></div><div id="out">Салбарууд ачаалж байна...</div></div>
<script>
const client = new dock.Client({ key: 'dk_...' });
const map = dock.createMap(client, { container: 'map', center: [106.9165, 47.8880], zoom: 14, look: 'light' });
// Your data: here a bank's branches as dock finds them, as one GeoJSON source and two layers.
let mine = { type: 'FeatureCollection', features: [] };
const addMine = () => {
if (map.getSource('mine')) return;
map.addSource('mine', { type: 'geojson', data: mine });
map.addLayer({ id: 'mine-dots', type: 'circle', source: 'mine',
paint: { 'circle-radius': 7, 'circle-color': '#e8590c', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
map.addLayer({ id: 'mine-names', type: 'symbol', source: 'mine',
layout: { 'text-field': ['get', 'name'], 'text-font': ['Noto Sans Regular'], 'text-size': 12, 'text-offset': [0, 1.2], 'text-anchor': 'top' },
paint: { 'text-color': '#e8590c', 'text-halo-color': '#fff', 'text-halo-width': 1.5 } });
};
// A new look is a new style, which drops your layers: load it whole (no diff) and put them back.
map.on('style.load', addMine);
for (const [look, word] of [['light', 'Цайвар'], ['dark', 'Бараан']]) {
const b = document.createElement('button');
b.textContent = word;
b.onclick = () => map.setStyle(dock.dockMapStyle(client, { look }), { diff: false });
document.getElementById('looks').append(b);
}
// The search ranks by name first, so keep the branches within 10 km of the centre.
const centre = new dock.maplibregl.LngLat(106.9165, 47.8880);
client.geocode('Голомт банк', { limit: 20, proximity: centre.toArray() }).then(({ results }) => {
const near = results.filter((p) => centre.distanceTo(new dock.maplibregl.LngLat(...p.point)) < 10000);
mine = { type: 'FeatureCollection', features: near.map((p) => ({ type: 'Feature',
properties: { name: p.name_mn ?? '' }, geometry: { type: 'Point', coordinates: p.point } })) };
map.getSource('mine')?.setData(mine);
if (near.length) map.fitBounds(near.reduce((b, p) => b.extend(p.point), new dock.maplibregl.LngLatBounds()),
{ padding: 60, maxZoom: 16, duration: 0 });
document.getElementById('out').textContent = `Голомт банкны ${near.length} салбар, таны давхарга дээр`;
});
</script>
</body>
</html>