Reference: https://gist.github.com/amortka/2c0386cb9bd93fed47882815af66f1b8
https://mapzen.com/projects/vector-tiles/
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
margin: 0;
overflow: hidden;
}
.map {
position: relative;
overflow: hidden;
background-color: rgba(52, 73, 94, 1.00);
}
.layer {
position: absolute;
will-change: transform;
}
.tile {
position: absolute;
width: 256px;
height: 256px;
}
.tile path {
fill: none;
stroke: none;
stroke-linejoin: round;
stroke-linecap: round;
}
.tile .path { stroke: #1b465f; }
.tile .water-layer, .tile .river { fill: none; stroke: #9DD9D2; stroke-width: 1.5px; }
.tile .water, .tile .ocean { fill: #9DD9D2; }
.tile .riverbank, .tile .lake { fill: #9DD9D2; }
.tile .water_boundary, .tile .ocean_boundary, .tile .riverbank_boundary, .tile .lake_boundary { fill: none; stroke: #93cbc4; stroke-width: 0.5px; }
.tile .major_road { stroke: #00ffd9; stroke-width: 1px; }
.tile .minor_road { stroke: rgba(0, 255, 217, 0.32); stroke-width: 0.5px; }
.tile .highway { stroke: #f39; stroke-width: 1.5px; }
.tile .buildings-layer { stroke: #987284; stroke-width: 0.15px; }
.tile .park, .tile .nature_reserve, .tile .wood, .tile .protected_land { fill: #88D18A; stroke: #88D18A; stroke-width: 0.5px; }
.tile .pier { fill: #fff; stroke: #fff; stroke-width: 0.5px; }
.tile .rail { stroke: #77ddee; stroke-width: 0.5px; }
.info {
position: absolute;
bottom: 10px;
left: 10px;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-tile.v0.0.min.js"></script>
<script>
var pi = Math.PI,
tau = 2 * pi;
var width = window.innerWidth;//Math.min(1920, window.innerWidth),
height = window.innerHeight;//Math.min(1080, window.innerHeight);
// Initialize the projection to fit the world in a 1×1 square centered at the origin.
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
var path = d3.geoPath()
.projection(projection);
var tile = d3.tile()
.size([width, height]);
var zoom = d3.zoom()
.scaleExtent([1 << (12 + 8), 1 << (16 + 8)])
.on("zoom", zoomed);
var map = d3.select("body").append("div")
.attr("class", "map")
.style("width", width + "px")
.style("height", height + "px")
.on("mousemove", mousemoved);
var layer = map.append("div")
.attr("class", "layer");
var info = map.append("div")
.attr("class", "info");
// Compute the projected initial center.
var center = projection([17.034849, 51.097010]);
// Apply a zoom transform equivalent to projection.{scale,translate,center}.
map .call(zoom)
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << (14 + 8))
.translate(-center[0], -center[1]));
function zoomed() {
var transform = d3.event.transform;
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
projection
.scale(transform.k / tau)
.translate([transform.x, transform.y]);
var image = layer
.style("transform", stringify(tiles.scale, tiles.translate))
.selectAll(".tile")
.data(tiles, function(d) { return d; });
image.exit()
.each(function(d) { this._xhr.abort(); })
.remove();
image.enter().append("svg")
.attr("class", "tile")
.style("left", function(d) { return d[0] * 256 + "px"; })
.style("top", function(d) { return d[1] * 256 + "px"; })
.each(function(d) { this._xhr = render(d, this); });
}
function render(d, node) {
//https://tile.mapzen.com/mapzen/vector/v1/all/14/8966/5475.mvt?api_key=vector-tiles-LM25tq4
return d3.json("https://tile.mapzen.com/mapzen/vector/v1/all/" + d[2] + "/" + d[0] + "/" + d[1] + ".json?api_key=vector-tiles-LM25tq4", function(error, json) {
if (error) throw error;
var k = Math.pow(2, d[2]) * 256; // size of the world in pixels
if (!json) {
return;
}
var geoData = d3.merge([
json.roads.features,
json.water.features
]).sort(function(a, b) { return a.properties.sort_key - b.properties.sort_key; })
d3.select(node).selectAll("path")
.data(geoData.filter(f => {
return !f.properties.label_placement;
}))
.enter().append("path")
.attr("class", function(d) { var kind = d.properties.kind || ''; if(d.properties.boundary){kind += '_boundary';} return kind; })
.attr('data-props', function(d) { return JSON.stringify(d.properties);})
.attr("d", d3.geoPath()
.projection(d3.geoMercator()
.scale(k / tau)
.translate([k / 2 - d[0] * 256, k / 2 - d[1] * 256])
.precision(0)));
});
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "matrix3d(" + [k, 0, 0, 0, 0, k, 0, 0, 0, 0, k, 0, r(translate[0] * scale), r(translate[1] * scale), 0, 1 ] + ")";
}
function mousemoved() {
info.text(formatLocation(projection.invert(d3.mouse(this)), d3.zoomTransform(this).k));
}
function formatLocation(p, k) {
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
}
</script>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3-tile.v0.0.min.js