Styling GeoJSON layers (loaded from an external source) with different icons in Leaflet
by Alex Azuero
HTML
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css">
<script src="http://makinacorpus.github.io/Leaflet.Spin/spin.js/dist/spin.min.js"></script>
<script src="http://makinacorpus.github.io/Leaflet.Spin/leaflet.spin.js"></script>
<div id="map"></div>
CSS
html, body, #map {
height: 100%;
width:100%;
padding:0px;
margin:0px;
}
JavaScript
////////////////////////////////////////////////////////////////////////////////////////////
//setting up the map//
////////////////////////////////////////////////////////////////////////////////////////////
// set center coordinates
var coords = [34.069, -118.293];
// set default zoom level
var zoomLevel = 13;
// initialize map
var map = L.map('map').setView(coords, zoomLevel);
// set source for map tiles
ATTR = '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' +
'© <a href="http://cartodb.com/attributions">CartoDB</a>';
CDB_URL = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';
// add tiles to map
L.tileLayer(CDB_URL, {attribution: ATTR}).addTo(map);
/////////////////////////////////////////////////////////////////////////////////////////////
//styling and popups//
/////////////////////////////////////////////////////////////////////////////////////////////
//create icons
var pintGlass = L.icon({
iconUrl: 'http://www.free-icons-download.net/images/beer-icon-12582.png',
iconSize: [32, 32]
});
var wineBottle = L.icon({
iconUrl: 'http://www.free-icons-download.net/images/wine-icon-42053.png',
iconSize: [32, 32]
});
//create markers with different icons
function getWineMarker(feature, latlng) {
return L.marker(latlng, {
icon: wineBottle
});
}
function getBeerMarker(feature, latlng) {
return L.marker(latlng, {
icon: pintGlass
});
}
//attach popups to the markers
function getPopup(feature, layer) {
layer.bindPopup("<strong>" + feature.properties.NAME + "</strong><br/>" + feature.properties.STREETNUM + " " + feature.properties.STREET + ", " + feature.properties.CITY + "<br/>" + "<a target = _blank href=" +
feature.properties.URL + ">" + feature.properties.URLDISPLAY + "</a>");
}
/////////////////////////////////////////////////////////////////////////////////////////////
//displaying the...