Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Created July 9, 2014 18:20
Show Gist options
  • Save wboykinm/cc509eb2763ca1ba9293 to your computer and use it in GitHub Desktop.
Save wboykinm/cc509eb2763ca1ba9293 to your computer and use it in GitHub Desktop.
OSM Vector Tiles with OpenLayers 3
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://ol3js.org/en/master/css/ol.css" type="text/css">
<style>
body {
padding: 0;
margin: 0;
overflow:hidden;
background: #A7A37E;
}
.map {
height: 100%;
width: 100%;
}
</style>
<script src="http://ol3js.org/en/master/build/ol.js" type="text/javascript"></script>
<title>OpenLayers 3 vector tiles</title>
</head>
<body>
<div id="map" class="map"></div>
<script>
var waterLayer = new ol.layer.Vector({
source: new ol.source.TileVector({
format: new ol.format.TopoJSON({
defaultProjection: 'EPSG:4326'
}),
projection: 'EPSG:3857',
tileGrid: new ol.tilegrid.XYZ({
maxZoom: 19
}),
url: 'http://{a-c}.tile.openstreetmap.us/' +
'vectiles-water-areas/{z}/{x}/{y}.topojson'
}),
style: new ol.style.Style({
fill: new ol.style.Fill({
color: '#046380'
})
})
});
var roadStyleCache = {};
var roadLayer = new ol.layer.Vector({
source: new ol.source.TileVector({
format: new ol.format.TopoJSON({
defaultProjection: 'EPSG:4326'
}),
projection: 'EPSG:3857',
tileGrid: new ol.tilegrid.XYZ({
maxZoom: 19
}),
url: 'http://{a-c}.tile.openstreetmap.us/' +
'vectiles-highroad/{z}/{x}/{y}.topojson'
}),
style: function(feature, resolution) {
var kind = feature.get('kind');
var railway = feature.get('railway');
var sort_key = feature.get('sort_key');
var styleKey = kind + '/' + railway + '/' + sort_key;
var styleArray = roadStyleCache[styleKey];
if (!styleArray) {
var color, width;
if (railway) {
color = '#E6E2AF';
width = 1;
} else {
color = {
'major_road': '#002F2F',
'minor_road': '#E6E2AF',
'highway': '#002F2F'
}[kind];
width = kind == 'highway' ? 1.5 : 1;
}
styleArray = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: color,
width: width
}),
zIndex: sort_key
})];
roadStyleCache[styleKey] = styleArray;
}
return styleArray;
}
});
var map = new ol.Map({
layers: [waterLayer, roadLayer],
renderer: 'canvas',
target: document.getElementById('map'),
view: new ol.View({
center: ol.proj.transform([3.22469,51.2093], 'EPSG:4326', 'EPSG:3857'),
maxZoom: 19,
zoom: 14
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment