-
-
Save biapar/db23a95f6edd3bc62bc9 to your computer and use it in GitHub Desktop.
Leaflet.js Tips, Step 7 (filter buttons)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css" /> | |
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> | |
<link rel="stylesheet" href="assets/leaflet_awesome/leaflet_awesome_markers.css"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | |
<!--this is new--> | |
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script> | |
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.js'></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="assets/leaflet_awesome/leaflet_awesome_markers.js"></script> | |
<style type="text/css"> | |
#map { | |
width: 600px; | |
height: 400px; | |
} | |
/* This is new */ | |
button { | |
width: 100px; | |
} | |
</style> | |
</head> | |
<body> | |
<!-- This is new --> | |
<div class="btn-group"> | |
<button type="button" id="allbus" class="btn btn-success">All</button> | |
<button type="button" id="others" class="btn btn-primary">Others</button> | |
<button type="button" id="cafes" class="btn btn-danger">Cafes</button> | |
</div> | |
<div id="map"></div> | |
<script type="text/javascript"> | |
var mapboxTiles = L.tileLayer('https://{s}.tiles.mapbox.com/v3/examples.map-i87786ca/{z}/{x}/{y}.png', { | |
attribution: '<a href="http://www.mapbox.com/about/maps/" target="_blank">Terms & Feedback</a>' | |
}); | |
//possible colors 'red', 'darkred', 'orange', 'green', 'darkgreen', 'blue', 'purple', 'darkpuple', 'cadetblue' | |
var cafeIcon = L.AwesomeMarkers.icon({ | |
prefix: 'fa', //font awesome rather than bootstrap | |
markerColor: 'red', // see colors above | |
icon: 'coffee' //http://fortawesome.github.io/Font-Awesome/icons/ | |
}); | |
var map = L.map('map') | |
.addLayer(mapboxTiles) | |
.setView([42.444508, -76.499491], 12); | |
var promise = $.getJSON("businesses.json"); | |
promise.then(function(data) { | |
var allbusinesses = L.geoJson(data); | |
var cafes = L.geoJson(data, { | |
filter: function(feature, layer) { | |
return feature.properties.BusType == "Cafe"; | |
}, | |
pointToLayer: function(feature, latlng) { | |
return L.marker(latlng, { | |
icon: cafeIcon | |
}).on('mouseover', function() { | |
this.bindPopup(feature.properties.Name).openPopup(); | |
}); | |
} | |
}); | |
var others = L.geoJson(data, { | |
filter: function(feature, layer) { | |
return feature.properties.BusType != "Cafe"; | |
}, | |
pointToLayer: function(feature, latlng) { | |
return L.marker(latlng, { | |
}).on('mouseover', function() { | |
this.bindPopup(feature.properties.Name).openPopup(); | |
}); | |
} | |
}); | |
map.fitBounds(allbusinesses.getBounds(), { | |
padding: [50, 50] | |
}); | |
cafes.addTo(map) | |
others.addTo(map) | |
// The JavaScript below is new | |
$("#others").click(function() { | |
map.addLayer(others) | |
map.removeLayer(cafes) | |
}); | |
$("#cafes").click(function() { | |
map.addLayer(cafes) | |
map.removeLayer(others) | |
}); | |
$("#allbus").click(function() { | |
map.addLayer(cafes) | |
map.addLayer(others) | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: L. Voogdt | |
License: MIT | |
Version: 1.0 | |
*/ | |
/* Marker setup */ | |
.awesome-marker { | |
background: url('images/markers-soft.png') no-repeat 0 0; | |
width: 35px; | |
height: 46px; | |
position:absolute; | |
left:0; | |
top:0; | |
display: block; | |
text-align: center; | |
} | |
.awesome-marker-shadow { | |
background: url('images/markers-shadow.png') no-repeat 0 0; | |
width: 36px; | |
height: 16px; | |
} | |
/* Retina displays */ | |
@media (min--moz-device-pixel-ratio: 1.5),(-o-min-device-pixel-ratio: 3/2), | |
(-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5),(min-resolution: 1.5dppx) { | |
.awesome-marker { | |
background-image: url('images/[email protected]'); | |
background-size: 720px 46px; | |
} | |
.awesome-marker-shadow { | |
background-image: url('images/[email protected]'); | |
background-size: 35px 16px; | |
} | |
} | |
.awesome-marker i { | |
color: #333; | |
margin-top: 10px; | |
display: inline-block; | |
font-size: 14px; | |
} | |
.awesome-marker .icon-white { | |
color: #fff; | |
} | |
/* Colors */ | |
.awesome-marker-icon-red { | |
background-position: 0 0; | |
} | |
.awesome-marker-icon-darkred { | |
background-position: -180px 0; | |
} | |
.awesome-marker-icon-lightred { | |
background-position: -360px 0; | |
} | |
.awesome-marker-icon-orange { | |
background-position: -36px 0; | |
} | |
.awesome-marker-icon-beige { | |
background-position: -396px 0; | |
} | |
.awesome-marker-icon-green { | |
background-position: -72px 0; | |
} | |
.awesome-marker-icon-darkgreen { | |
background-position: -252px 0; | |
} | |
.awesome-marker-icon-lightgreen { | |
background-position: -432px 0; | |
} | |
.awesome-marker-icon-blue { | |
background-position: -108px 0; | |
} | |
.awesome-marker-icon-darkblue { | |
background-position: -216px 0; | |
} | |
.awesome-marker-icon-lightblue { | |
background-position: -468px 0; | |
} | |
.awesome-marker-icon-purple { | |
background-position: -144px 0; | |
} | |
.awesome-marker-icon-darkpurple { | |
background-position: -288px 0; | |
} | |
.awesome-marker-icon-pink { | |
background-position: -504px 0; | |
} | |
.awesome-marker-icon-cadetblue { | |
background-position: -324px 0; | |
} | |
.awesome-marker-icon-white { | |
background-position: -574px 0; | |
} | |
.awesome-marker-icon-gray { | |
background-position: -648px 0; | |
} | |
.awesome-marker-icon-lightgray { | |
background-position: -612px 0; | |
} | |
.awesome-marker-icon-black { | |
background-position: -682px 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Leaflet.AwesomeMarkers, a plugin that adds colorful iconic markers for Leaflet, based on the Font Awesome icons | |
(c) 2012-2013, Lennard Voogdt | |
http://leafletjs.com | |
https://github.com/lvoogdt | |
*/ | |
/*global L*/ | |
(function (window, document, undefined) { | |
"use strict"; | |
/* | |
* Leaflet.AwesomeMarkers assumes that you have already included the Leaflet library. | |
*/ | |
L.AwesomeMarkers = {}; | |
L.AwesomeMarkers.version = '2.0.1'; | |
L.AwesomeMarkers.Icon = L.Icon.extend({ | |
options: { | |
iconSize: [35, 45], | |
iconAnchor: [17, 42], | |
popupAnchor: [1, -32], | |
shadowAnchor: [10, 12], | |
shadowSize: [36, 16], | |
className: 'awesome-marker', | |
prefix: 'glyphicon', | |
spinClass: 'fa-spin', | |
extraClasses: '', | |
icon: 'home', | |
markerColor: 'blue', | |
iconColor: 'white' | |
}, | |
initialize: function (options) { | |
options = L.Util.setOptions(this, options); | |
}, | |
createIcon: function () { | |
var div = document.createElement('div'), | |
options = this.options; | |
if (options.icon) { | |
div.innerHTML = this._createInner(); | |
} | |
if (options.bgPos) { | |
div.style.backgroundPosition = | |
(-options.bgPos.x) + 'px ' + (-options.bgPos.y) + 'px'; | |
} | |
this._setIconStyles(div, 'icon-' + options.markerColor); | |
return div; | |
}, | |
_createInner: function() { | |
var iconClass, iconSpinClass = "", iconColorClass = "", iconColorStyle = "", options = this.options; | |
if(options.icon.slice(0,options.prefix.length+1) === options.prefix + "-") { | |
iconClass = options.icon; | |
} else { | |
iconClass = options.prefix + "-" + options.icon; | |
} | |
if(options.spin && typeof options.spinClass === "string") { | |
iconSpinClass = options.spinClass; | |
} | |
if(options.iconColor) { | |
if(options.iconColor === 'white' || options.iconColor === 'black') { | |
iconColorClass = "icon-" + options.iconColor; | |
} else { | |
iconColorStyle = "style='color: " + options.iconColor + "' "; | |
} | |
} | |
return "<i " + iconColorStyle + "class='" + options.extraClasses + " " + options.prefix + " " + iconClass + " " + iconSpinClass + " " + iconColorClass + "'></i>"; | |
}, | |
_setIconStyles: function (img, name) { | |
var options = this.options, | |
size = L.point(options[name === 'shadow' ? 'shadowSize' : 'iconSize']), | |
anchor; | |
if (name === 'shadow') { | |
anchor = L.point(options.shadowAnchor || options.iconAnchor); | |
} else { | |
anchor = L.point(options.iconAnchor); | |
} | |
if (!anchor && size) { | |
anchor = size.divideBy(2, true); | |
} | |
img.className = 'awesome-marker-' + name + ' ' + options.className; | |
if (anchor) { | |
img.style.marginLeft = (-anchor.x) + 'px'; | |
img.style.marginTop = (-anchor.y) + 'px'; | |
} | |
if (size) { | |
img.style.width = size.x + 'px'; | |
img.style.height = size.y + 'px'; | |
} | |
}, | |
createShadow: function () { | |
var div = document.createElement('div'); | |
this._setIconStyles(div, 'shadow'); | |
return div; | |
} | |
}); | |
L.AwesomeMarkers.icon = function (options) { | |
return new L.AwesomeMarkers.Icon(options); | |
}; | |
}(this, document)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment