Leaflet Markers With Custom Icons

http://leafletjs.com/examples/custom-icons/

by Manuel Gertrudix

HTML

<!DOCTYPE html>
<html>
  <head>
  
  <!--Enlazamos la hoja de estilo de Leaftletjs -->
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"
   integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
   crossorigin=""/>
   
      <!--Ponemos después el enlace al js de Leaftletjs. Debe ir detrás de la hoja de estilo -->
   <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"
   integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
   crossorigin=""></script>
   
   
</head>
<body>

<!--Creamos un div para que se cargue el mapa en él  -->
 <div id="mapid"></div>
 
</body>
</html>

CSS

/*Le damos un alto al div que contiene el mapa a través del id */

#mapid { height: 480px; }

JavaScript

// Creamos el mapa y le asignamos las coordenadas decimales y el nivel de zoom
var mymap = L.map('mapid').setView([40.4530541, -3.688344], 17);

// Cargamos una capa de mapa (tile layer) desde Mapbox. Necesitaremos incluir el accessToken de mapbox. Si no tenemos uno, tendremos que crearlo
// En id podemos incluir cualquiera de los estilos de mapa base disponibles en https://www.mapbox.com/api-documentation/#maps 

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZ2VydHJ1ZGl4IiwiYSI6IlRES051RU0ifQ.KLZsP10MfXGUwb9uGEV7SQ', {
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
    maxZoom: 18,
    id: 'mapbox.emerald',
    accessToken: 'pk.eyJ1IjoiZ2VydHJ1ZGl4IiwiYSI6IlRES051RU0ifQ.KLZsP10MfXGUwb9uGEV7SQ'
}).addTo(mymap);


// Creamos un icono

var greenIcon = L.icon({
    iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-green.png',
    shadowUrl: 'http://leafletjs.com/examples/custom-icons/leaf-shadow.png',

    iconSize:     [38, 95], // size of the icon
    shadowSize:   [50, 64], // size of the shadow
    iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
    shadowAnchor: [4, 62],  // the same for the shadow
    popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
});

// Ponemos el icono como marcador
L.marker([40.4530541, -3.688344], {icon: greenIcon}).addTo(mymap);