Code Monkey home page Code Monkey logo

drawnlayer's Introduction

Based on the code for Crayon Map

drawnLayer

A canvas tile layer using OpenStreetMap data and Mapzen's vector tiles, to help you make colorful, custom-rendered maps. The coordinates of GeoJSON shapes are converted to relative pixel integers so you can draw them directly on canvas tiles.

Runs on Google Maps API and Leaflet maps.

Library includes GitHub's open source polyfill for fetch. Minified versions created by jsmin.

Suppose you wanted to draw shapes where OpenStreetMap returned landuse with kind=residential

var map = L.map('map');

// pass these values
L.drawnLayer('MAPZEN_KEY', 'landuse', function(geojson, canvasctx) {
  for (var f = 0; f < geojson.features.length; f++) {
    var feature = geojson.features[f];
    if (feature.properties.kind === 'residential') {
      CustomDrawFunction(feature.geometry, canvasctx);
    }
  }
}).addTo(map);

// your custom renderer
function CustomDrawFunction(geometry, canvasctx) {
  // only draw outer ring of polygons
  var coords = (geometry.type === 'Polygon') ? geometry.coordinates[0] : geometry.coordinates;

  // draw shape
  canvasctx.beginPath();
  canvasctx.moveTo(coords[0].x, coords[0].y);
  for (var pt = 1; pt < coords.length; pt++) {
    canvasctx.lineTo(coords[pt].x, coords[pt].y);
  }
  canvasctx.closePath();

  // fill in the shape
  canvasctx.fillStyle = 'darkgreen';
  canvasctx.fill();

  // if you just wanted to draw an outline
  canvasctx.strokeStyle = 'darkgreen';
  canvasctx.stroke();
}
var map = new google.maps.Map(document.getElementById('map'), { ... });

// pass these values
var dl = new DrawnLayer('MAPZEN_KEY', 'TILE_LAYERS', function(geojson, canvasctx) {
  geojson.features.map(function(feature) {
    if (feature.properties.kind === 'residential') {
      CustomDrawFunction(feature.geometry, canvasctx);
    }
  });
});
map.overlayMapTypes.insertAt(1, dl);

function CustomDrawFunction(geometry, canvasctx) {
  ... (see Leaflet example)
}

License

Open source, MIT license

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.