OpenLayers 3 Beginner’s Guide

Working with the TileVector source

Open sample in a new windows

<!doctype html>
    <title>Vector Examples</title>
    <link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
    <link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
    <div id="map" class="map"></div>
    <script src="../assets/ol3/js/ol.js"></script>
    <script src=""></script>
    var tiledRaster = new ol.layer.Tile({
        source: new ol.source.OSM()

      // some styles for the vector layer
      var fill = new{
        color: 'rgba(0,0,0,0.2)'

      var stroke = new{
        color: 'rgba(0,0,0,0.4)'

      var circle = new{
        radius: 6,
        fill: fill,
        stroke: stroke

      var vectorStyle = new{
        fill: fill,
        stroke: stroke,
        image: circle

      // this time, we'll load vector tiles from openstreetmap
      // in the topojson format
      var tiledSource = 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}{z}/{x}/{y}.topojson'

      var tiledVector = new ol.layer.Vector({
        source: tiledSource,
        style: vectorStyle

      var center = ol.proj.transform([-72.6, 41.7], 'EPSG:4326', 'EPSG:3857');
      var view = new ol.View({
        center: center,
        zoom: 12
      var map = new ol.Map({
        target: 'map',
        layers: [tiledRaster, tiledVector],
        view: view