Skip to content

Instantly share code, notes, and snippets.

@mourner
Created July 3, 2012 10:07
Show Gist options
  • Select an option

  • Save mourner/3038879 to your computer and use it in GitHub Desktop.

Select an option

Save mourner/3038879 to your computer and use it in GitHub Desktop.
Leaflet API simplification proposal
var map = new L.Map('map');
map.setView(new L.LatLng(51.505, -0.09), 13);
var cloudmade = new L.TileLayer('http://{s}.tile.cloudmade.com/.../{z}/{x}/{y}.png', {
attribution: '[...]'
});
map.addLayer(cloudmade);
var marker = new L.Marker(new L.LatLng(51.5, -0.09));
marker.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
map.addLayer(marker);
var circle = new L.Circle(new L.LatLng(51.508, -0.11), 500, {color: '#f03', opacity: 0.7});
circle.bindPopup("I am a circle.");
map.addLayer(circle);
var polygon = new L.Polygon([
new L.LatLng(51.509, -0.08),
new L.LatLng(51.503, -0.06),
new L.LatLng(51.51, -0.047)
]);
polygon.bindPopup("I am a polygon.");
map.addLayer(polygon);
var map = L.map('map').setView([51.505, -0.09], 13);
var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/.../{z}/{x}/{y}.png', {
attribution: '[...]'
}).addTo(map);
L.marker([51.5, -0.09])
.bindPopup("<b>Hello world!</b><br />I am a popup.")
.addTo(map)
.openPopup();
L.circle([51.508, -0.11], 500, {color: '#f03', opacity: 0.7})
.bindPopup("I am a circle.")
.addTo(map);
L.polygon([
[51.509, -0.08],
[51.503, -0.06],
[51.51, -0.047]])
.bindPopup("I am a polygon.")
.addTo(map);
@rlightner
Copy link
Copy Markdown

+1

@bartek
Copy link
Copy Markdown

bartek commented Jul 5, 2012

Love the chaining. +1 for the simplified API

@WolfieZero
Copy link
Copy Markdown

Simplified API is very sex; more jQuery like and less Google Maps.

+1 simplified

@mourner
Copy link
Copy Markdown
Author

mourner commented Jul 5, 2012

Implemented in the master branch with backwards compatibility — you can use both approaches in your code now!

@ZoltanVeres
Copy link
Copy Markdown

I think the new version is more readable.

@frewsxcv
Copy link
Copy Markdown

frewsxcv commented Jul 5, 2012

For example, on this line:

var map = L.map('map').setView([51.505, -0.09], 13);

Does that mean setView() returns the map object?

@mourner
Copy link
Copy Markdown
Author

mourner commented Jul 5, 2012

Yep, all method that don't explicitly return some value (like getCenter) return the object itself for chaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment