Skip to main content
The 2024 Developer Survey results are live! See the results
edited title
Link
DarkBee
  • 16.4k
  • 6
  • 49
  • 64

Do I ***need***need to extend Leaflet's marker class to add properties?

added 2 characters in body
Source Link
Neuron
  • 5.6k
  • 5
  • 42
  • 61

It's something that I have very rarely done - adding properties to a Leaflet marker.

I was reading this page which says that we should

customMarker = L.Marker.extend({
   options: {
      name: '',
      type: ''
   }
});

var marker = new customMarker([28.63278, 77.21972],{
   clickable: true,
   name: 'Connaught Place',
   type: 'Neighbourhood'
}).addTo(map);

I see from some old code that I have simply

let marker = L.marker(markerLatLng, {
       name: 'Connaught Place',
       type: 'Neighbourhood'  

Since this works, it seems like Leaflet is intelligent enough to treat any non-Leaflet properties as extending the L.MarkerL.Marker class.

Is there a "correct" way (with a technical reason for it being correct)?

It's something that I have very rarely done - adding properties to a Leaflet marker.

I was reading this page which says that we should

customMarker = L.Marker.extend({
   options: {
      name: '',
      type: ''
   }
});

var marker = new customMarker([28.63278, 77.21972],{
   clickable: true,
   name: 'Connaught Place',
   type: 'Neighbourhood'
}).addTo(map);

I see from some old code that I have simply

let marker = L.marker(markerLatLng, {
       name: 'Connaught Place',
       type: 'Neighbourhood'  

Since this works, it seems like Leaflet is intelligent enough to treat any non-Leaflet properties as extending the L.Marker class.

Is there a "correct" way (with a technical reason for it being correct)?

It's something that I have very rarely done - adding properties to a Leaflet marker.

I was reading this page which says that we should

customMarker = L.Marker.extend({
   options: {
      name: '',
      type: ''
   }
});

var marker = new customMarker([28.63278, 77.21972],{
   clickable: true,
   name: 'Connaught Place',
   type: 'Neighbourhood'
}).addTo(map);

I see from some old code that I have simply

let marker = L.marker(markerLatLng, {
       name: 'Connaught Place',
       type: 'Neighbourhood'  

Since this works, it seems like Leaflet is intelligent enough to treat any non-Leaflet properties as extending the L.Marker class.

Is there a "correct" way (with a technical reason for it being correct)?

Source Link
Mawg
  • 39.6k
  • 106
  • 321
  • 567

Do I ***need*** to extend Leaflet's marker class to add properties?

It's something that I have very rarely done - adding properties to a Leaflet marker.

I was reading this page which says that we should

customMarker = L.Marker.extend({
   options: {
      name: '',
      type: ''
   }
});

var marker = new customMarker([28.63278, 77.21972],{
   clickable: true,
   name: 'Connaught Place',
   type: 'Neighbourhood'
}).addTo(map);

I see from some old code that I have simply

let marker = L.marker(markerLatLng, {
       name: 'Connaught Place',
       type: 'Neighbourhood'  

Since this works, it seems like Leaflet is intelligent enough to treat any non-Leaflet properties as extending the L.Marker class.

Is there a "correct" way (with a technical reason for it being correct)?