Code Monkey home page Code Monkey logo

cartodb-leaflet's Introduction

CartoDB library for LeafletJS

Use your own CartoDB tables in a leaflet map.

Adding the library

Use the library is really easy, and you will only need:

  • Your CartoDB user name
  • Choose a table to play with
  • Do a query
  • And make your table public

Params

The library accepts certain params to manage the cartodb layer:

Required

  • map_canvas: The DOM element id where the map is
  • map: The Google map object create before
  • username: Your CartoDB user name
  • table_name: Your CartoDB table name
  • query: A query to experiment with

Optional:

  • map_key: If your table is private, you'll need the map_key parameter
  • infowindow: If you want to add interactivity to the layer, showing the infowindow (If you want to show specific columns, it must be a query, adding it 'WHERE cartodb_id={{feature}}', later it will be replace by the clicked cartodb_id. cartodb_id and the_geom_webmercator are REQUIRED parameters)
  • tile_style: If you want to add other style to the layer
  • autobound: If you want to zoom in the area where the layer is positioned
  • debug: If you want to debug the library, set to true

Note: If you choose a CartoDB private table you'll need to authenticate for using API methods.

Example

First of all create your map:

var map = new L.Map('map_canvas');
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
    cloudmadeAttrib = 'Map data © 2011 OpenStreetMap contributors, Imagery © 2011 CloudMade',
    cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttrib});
		map.addLayer(cloudmade);

And then add the cartodb layer:

var cartodb_leaflet = new L.CartoDBLayer({
	map_canvas: 'map_canvas',
	map: map,
	user_name:'xavijam',
	table_name: 'test2',
	query: "SELECT * FROM test2",
    tile_style: "#test2{line-color:#719700;line-width:1;line-opacity:0.6;polygon-opacity:0.6;}",
    map_key: "6087bc5111352713a81a48491078f182a0541f6c",
	infowindow: "SELECT cartodb_id,the_geom_webmercator,description FROM test2 WHERE cartodb_id={{feature}}",
	auto_bound: true});

Functions

New funcionalities are coming, in the meantime you can use:

  • update: It needs a new query to work. Example: cartodb_leaflet.update('SELECT * FROM test WHERE cartodb_id>2');
  • destroy: Removes the cartodb layer from the map. Example: cartodb_leaflet.destroy();
  • hide: Hide the cartodb layer from the map (For now, hide and destroy are the same, but will be replace in the future).
  • show: Show again the cartodb layer in the map. Example: cartodb_leaflet.show();
  • isVisible: Returns if cartodb layer is visible or not. Example: cartodb_leaflet.isVisible(); -> true | false

live example

cartodb-leaflet's People

Contributors

xavijam avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.