when custom icons are added, icons appears in a different position than when default marker added.

37 views
Skip to first unread message

Shalini A

unread,
Nov 11, 2015, 1:36:47 PM11/11/15
to Leaflet
Dear All,

I'm facing a weird issue while adding markers with custom icons to my map. Basically I'm trying to show polylines in red color and mark the waypoints with the custom icons with a popup connecting the media information. When the default marker is added, the track appears as in the attachment, default_marker.png. All the points looks connected. But when custom icons are added, the marker position is changed. Refer attachment, custom_markers.png. I'm not sure where I'm going wrong. Below are the code snippets.

var addedTrack = L.geoJson(track, {
         style: function(feature) {
           return {
                color: 'red',
                };
         },
         pointToLayer: function(feature,latlng){
           switch(feature.properties.name) {
           case 'Picture':
                return L.marker(latlng, {icon:pictureIcon});
           case 'Voice recording':
                return L.marker(latlng,{icon:audioIcon});
           default:
                return L.marker(latlng,{icon:textIcon});
           }
         },
         onEachFeature: function(feature, layer) {
           switch(feature.properties.name) {
           case 'Picture':
             layer.bindPopup("<img class='img-responsive' src=" + obj.get('data-path') + '/' +
                             feature.properties.link + "/>");
             break;
           case 'Voice recording':
             layer.bindPopup("<audio controls='controls' src=" + obj.get('data-path')+ '/' +
                             feature.properties.link + ".mp3" + "/>");
             break;
           default:
             layer.bindPopup(feature.properties.name);
             break;
           }
         }
       }).addTo(map);

Thank you for any help.
default_marker.png
custom_markers.png

ghybs

unread,
Nov 12, 2015, 7:19:48 AM11/12/15
to Leaflet
Hi,

Please how did you defined your custom icons? (pictureIcon, audioIcon, textIcon)
Did you make sure the icon and shadow anchors are properly set?
Otherwise, Leaflet will place your markers' icons so that their top left corner is on the specified latLng position.

See http://leafletjs.com/reference.html#icon-iconanchor

Hope this helps.

Shalini A

unread,
Nov 12, 2015, 11:36:59 PM11/12/15
to Leaflet
Thank you ghybs.
I have defined the custom icons this way 
var ColorIcon = L.Icon.extend({ 
  options: {
        iconAnchor:   [10, 34],     
        popupAnchor: [0, -34]                                    
        }
  });   
  
   var pictureIcon = new ColorIcon({iconUrl: '../static/images/picture_icon.png'}), 
        audioIcon = new ColorIcon({iconUrl: '../static/images/audio_icon.png'}),
        textIcon = new ColorIcon({iconUrl: '../static/images/text_icon.png'});
 
Is it very necessary to set shadow url and anchor? On what basis the iconAnchor has to be set if the center(where image points) has to point to the actual latlng? Is it based on the icon(used as marker) width and height?

ghybs

unread,
Nov 13, 2015, 3:29:38 AM11/13/15
to Leaflet
Hi,

No, you do not need to specify any shadow if you do not want one.

Sorry I forgot to mention that you absolutely need to specify the "iconSize" as well.
http://leafletjs.com/reference.html#icon-iconsize

The iconAnchor is based on this iconSize: the anchor is the offset (from top left corner) to use to correctly position the "tip" of your icon on the latLng position.

Hope this helps.
Reply all
Reply to author
Forward
0 new messages