Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use Custom Icons in Leaflet

Continuing with the map navigation application I am trying to create, I have encountered the following error:

I parse the OSM tile and get a list of all the Points of Interest. This is successful. I then want to put a marker in Leaflet, using a few custom icons. The code for the mentioned parts is as follows

First the includes:

<link rel="stylesheet"href="./includes/leaflet.css" />
<link rel="stylesheet" href="./includes/style.css" type="text/css">
<script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js"></script>
<script src="./includes/jquery.js"></script>
<script src="./includes/geo.js" type="text/javascript"></script>
<script src="./includes/gears_init.js" type="text/javascript"></script>
<script type="text/javascript">

For those nav Points that are found I have a custom icon like this:

if($.inArray(EleID, MarkerArray)==-1)
                {

                    if(LocType!="")
                    {
                        var markerLocation = new L.LatLng(EleLat,EleLon);
                        var Icon = new CustomIcon("./images/"+LocType+".png");
                        var marker = new L.Marker(markerLocation,{icon : Icon});

                        if(EleText!="")
                        {
                            marker.bindPopup(EleText);

                        }

                        Layergroup.addLayer(marker);
                        MarkerArray.push(EleID);
                    }

                }

These icons are in the ./image/ directory ,with their type being the name of the icon (p.ex. hotel in hotel.png)

Finally I create the map in the Body Section of the HTML document

var map = new L.Map('map');


        var cloudmadeUrl = 'http://a.tile.openstreetmap.org/{z}/{x}/{y}.png',
            cloudmadeAttrib = 'Map data CC-BY-SA &amp; ODbL 2012 OpenStreetMap contributors, Data via <a href="http://www.overpass-api.de/">Overpass API</a>',
            cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttrib});


        var CustomIcon = L.Icon.extend({
            iconUrl: './images/hotel.png',
            shadowUrl: './images/shadow.png',
            iconSize: new L.Point(32, 32),
            opacity: 0.5,
            //shadowSize: new L.Point(68, 95),
            iconAnchor: new L.Point(16, 16),
            popupAnchor: new L.Point(0, -18)
        });

While this worked great with an old leaflet script I had, it doesnt work with the updated version I am working with giving me this error:


Uncaught Error: iconUrl not set in Icon options (see the docs). leaflet.js:6


I am not exactly sure where the IconURL is being set inside the leaflet, does anyone have any idea what I am doing wrong?

like image 975
Spyros Avatar asked Mar 23 '23 14:03

Spyros


1 Answers

When defining your CustomIcon by extending L.Icon.extend(), you have to wrap your options up in the options object. Like so:

var CustomIcon = L.Icon.extend({
   options: {
        iconUrl: './images/hotel.png',
        shadowUrl: './images/shadow.png',
        iconSize: new L.Point(32, 32),
        opacity: 0.5,
        //shadowSize: new L.Point(68, 95),
        iconAnchor: new L.Point(16, 16),
        popupAnchor: new L.Point(0, -18)
      }
    });
like image 69
Patrick D Avatar answered Apr 02 '23 13:04

Patrick D