Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert custom marker icons in a leaflet map #301

Closed
sckott opened this issue Nov 15, 2013 · 5 comments
Closed

Insert custom marker icons in a leaflet map #301

sckott opened this issue Nov 15, 2013 · 5 comments
Labels

Comments

@sckott
Copy link

sckott commented Nov 15, 2013

Hey @ramnathv - Curious how to use custom icons in a leaflet map. I think I got it except for I don't know where to define the new icon:

var LeafIcon = L.Icon.extend({
    options: {
        shadowUrl: 'leaf-shadow.png',
        iconSize:     [38, 95],
        shadowSize:   [50, 64],
        iconAnchor:   [22, 94],
        shadowAnchor: [4, 62],
        popupAnchor:  [-3, -76]
    }
});

Which I can then use e.g., by

var points = L.geoJson(labels, {
        pointToLayer: function (feature, latlng) {
            return L.marker(latlng, {icon: LeafIcon });
        }
});

Where can I define in a new icon var?

@ramnathv
Copy link
Owner

Here is how you can do it now. Unfortunately, it is a little messy as there is no mechanism currently to define a separate icon variable and then use it.

L1$geoJson(toGeoJSON(data_), 
  pointToLayer =  "#! function(feature, latlng){
    return L.marker(latlng, {icon: L.Icon.extend({
      options: {
        shadowUrl: 'leaf-shadow.png',
        iconSize:     [38, 95],
        shadowSize:   [50, 64],
        iconAnchor:   [22, 94],
        shadowAnchor: [4, 62],
        popupAnchor:  [-3, -76]
      }})
    })
  } !#"
)

@sckott
Copy link
Author

sckott commented Nov 15, 2013

Thanks!

@miguelfg
Copy link

miguelfg commented Oct 2, 2014

Hi Ramnathv,

And how would it be for two type of icons?, I have two data frames with the coordinates points i want to add, each represent different items, so i want to place different markers, at least about color.

Should be something like this? (i'm affraid that no, because i'm not sure that calling twice to $geoJson it'd append both set of points to the map)

set markers type 1

L1$geoJson(toGeoJSON(df1),
onEachFeature = '#! function(feature, layer){
layer.bindPopup(feature.properties.popup)
} !#',
pointToLayer = "#! function(feature, latlng){
return L.circleMarker(latlng, {
radius: 4,
fillColor: feature.properties.fillColor || 'red',
color: '#000',
weight: 1,
fillOpacity: 0.8
})
} !#")

set markers type 2

L1$geoJson(toGeoJSON(df2),
onEachFeature = '#! function(feature, layer){
layer.bindPopup(feature.properties.popup)
} !#',
pointToLayer = "#! function(feature, latlng){
return L.circleMarker(latlng, {
radius: 4,
fillColor: feature.properties.fillColor || 'blue',
color: '#2b32ac',
weight: 1,
fillOpacity: 0.8
})
} !#")

@pfuhlert
Copy link

pfuhlert commented Jun 8, 2015

Any help with multiple icons? The code above does not work for me. $geoJson does not append the new icons, but overwrites the old ones.

@miguelfg
Copy link

Sorry, I've never managed :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants