From 6284c9d8b45b4df112326c23037a5c7c2e0b89d0 Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Sat, 18 Apr 2020 12:35:36 +0200 Subject: [PATCH] Center markers etc. on block Before, markers were set to the bottom left corner of a block. Now, it is nicely centered even on high zoom levels. --- htdocs/mapscript.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/htdocs/mapscript.js b/htdocs/mapscript.js index e3689c6..83967ba 100644 --- a/htdocs/mapscript.js +++ b/htdocs/mapscript.js @@ -83,6 +83,29 @@ function load_svg(name, url, active=1) { xhttp_ps.send(); } +function json_center_on_block(json) { + for (var i = 0; i < json.length; i++) { + if (json[i].geometry && json[i].geometry.coordinates) { + switch (json[i].geometry.type) { + case "Point": + json[i].geometry.coordinates[0] += 0.5; + json[i].geometry.coordinates[1] += 0.5; + break; + case "LineString": + for (var j = 0; j < json[i].geometry.coordinates.length; j++) { + json[i].geometry.coordinates[j][0] += 0.5; + json[i].geometry.coordinates[j][1] += 0.5; + } + break; + default: + console.log("centering: Type " + json[i].geometry.type + " not yet implemented."); + break; + } + } + } + return json; +} + function load_tiles(name, id) { var satellite = L.tileLayer('https://notsyncing.net/maps.linux-forks.de/tiles/?id={id}&z={z}&x={x}&y={y}', { maxZoom: 14 /*8*/, @@ -136,7 +159,8 @@ function load_geojson(name, url, iconname, iconcolor, active=1, style={}) { }; break; } - var geojson = L.geoJSON(JSON.parse(xhttp_ps.responseText), { + var json = json_center_on_block(JSON.parse(xhttp_ps.responseText)); + var geojson = L.geoJSON(json, { style: style, onEachFeature: onEachFeature, pointToLayer: pointToLayer