Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Leaflet.js Essentials

You're reading from   Leaflet.js Essentials Create interactive, mobile-friendly mapping applications using the incredibly light yet powerful Leaflet.js platform.

Arrow left icon
Product type Paperback
Published in Aug 2014
Publisher
ISBN-13 9781783554812
Length 180 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Using a custom marker in Leaflet


To create a marker icon in Leaflet, you need to create an instance of the L.icon class. The L.icon class takes 10 options, as follows:

  • iconUrl

  • iconRetinaUrl

  • iconSize

  • iconAnchor

  • shadowUrl

  • shadowRetinaUrl

  • shadowSize

  • shadowAnchor

  • popupAnchor

  • className

The only required option is iconUrl. In this example, you will ignore the retina images and the class name. Open LeafletEssentials.html and add the following code:

var myIcon = L.icon({
    iconUrl: 'mymarker.png',
    shadowUrl: 'shadow.png',
    iconSize:     [40, 60], 
    shadowSize:   [60, 40], 
    iconAnchor:   [20, 60], 
    shadowAnchor: [20, 40],  
    popupAnchor:  [0, -53] 
});

The preceding code sets the options. The iconUrl option directs the URL to the icon image and the shadowUrl option directs the URL to the shadow image. The iconSize and shadowSize options require the dimensions of the images in the format of width by height.

The iconAnchor options set the point at which the marker and icon...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image