Skip to content

Instantly share code, notes, and snippets.

@egardner
Forked from anonymous/index.html
Last active December 11, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egardner/cd13c0cd1c36ed047dce to your computer and use it in GitHub Desktop.
Save egardner/cd13c0cd1c36ed047dce to your computer and use it in GitHub Desktop.
A basic working example of using a more OOP pattern to wrap Leaflet's API. source http://jsbin.com/yudenugugu
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
#map {
height: 800px;
}
</style>
</head>
<body>
<div id="map">
</div>
<script id="jsbin-javascript">
// MyMap Class
//
//
//
// Properties
// -------------------------------------------------------------------------------------------
// All new MyMap objects get these upon instantiation
function MyMap() {
this.map = {}; // stash a leaflet map object here once we initialize
this.zoomLevel = 9;
this.center = [34.050, -118.250];
this.tiles = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
this.attr = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
// this.bind('init');
// this.bind('addTiles');
// this.bind('addMarker');
this.init();
this.addTiles();
// this.addMarker([34.050, -118.250]);
}
// Methods
// -------------------------------------------------------------------------------------------
MyMap.prototype = {
// an example of a "bind" function...
bind: function(method) {
var fn = this[method],
self = this;
this[method] = function() {
return fn.apply(self, arguments);
};
return this;
},
addTiles: function() {
L.tileLayer(this.tiles, {attribution: this.attr}).addTo(this.map);
},
addMarker: function(latlng) {
// expect latlng to be an array
L.marker(latlng).addTo(this.map).bindPopup('A pretty CSS3 popup.<br> Easily customizable.');
},
init: function() {
this.map = L.map('map').setView(this.center, this.zoomLevel);
}
};
var map = new MyMap();
// This method can be called publicly:
map.addMarker([34.050, -118.250]);
</script>
<script id="jsbin-source-css" type="text/css">#map {
height: 800px;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">// MyMap Class
//
//
//
// Properties
// -------------------------------------------------------------------------------------------
// All new MyMap objects get these upon instantiation
function MyMap() {
this.map = {}; // stash a leaflet map object here once we initialize
this.zoomLevel = 9;
this.center = [34.050, -118.250];
this.tiles = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
this.attr = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
// this.bind('init');
// this.bind('addTiles');
// this.bind('addMarker');
this.init();
this.addTiles();
// this.addMarker([34.050, -118.250]);
}
// Methods
// -------------------------------------------------------------------------------------------
MyMap.prototype = {
// an example of a "bind" function...
bind: function(method) {
var fn = this[method],
self = this;
this[method] = function() {
return fn.apply(self, arguments);
};
return this;
},
addTiles: function() {
L.tileLayer(this.tiles, {attribution: this.attr}).addTo(this.map);
},
addMarker: function(latlng) {
// expect latlng to be an array
L.marker(latlng).addTo(this.map).bindPopup('A pretty CSS3 popup.<br> Easily customizable.');
},
init: function() {
this.map = L.map('map').setView(this.center, this.zoomLevel);
}
};
var map = new MyMap();
// This method can be called publicly:
map.addMarker([34.050, -118.250]);
</script></body>
</html>
#map {
height: 800px;
}
// MyMap Class
//
//
//
// Properties
// -------------------------------------------------------------------------------------------
// All new MyMap objects get these upon instantiation
function MyMap() {
this.map = {}; // stash a leaflet map object here once we initialize
this.zoomLevel = 9;
this.center = [34.050, -118.250];
this.tiles = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
this.attr = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
// this.bind('init');
// this.bind('addTiles');
// this.bind('addMarker');
this.init();
this.addTiles();
// this.addMarker([34.050, -118.250]);
}
// Methods
// -------------------------------------------------------------------------------------------
MyMap.prototype = {
// an example of a "bind" function...
bind: function(method) {
var fn = this[method],
self = this;
this[method] = function() {
return fn.apply(self, arguments);
};
return this;
},
addTiles: function() {
L.tileLayer(this.tiles, {attribution: this.attr}).addTo(this.map);
},
addMarker: function(latlng) {
// expect latlng to be an array
L.marker(latlng).addTo(this.map).bindPopup('A pretty CSS3 popup.<br> Easily customizable.');
},
init: function() {
this.map = L.map('map').setView(this.center, this.zoomLevel);
}
};
var map = new MyMap();
// This method can be called publicly:
map.addMarker([34.050, -118.250]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment