Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet Awesome-Markers (Adding Numbers)

I am using the Leaflet.Awesome-Markers plugin with LeafletJS.

I have implemented it correctly, however now I'd like to be able to use numbers from 0 - 9 to represent markers.

Here's a JS Fiddle implementation to show how the plugin behaves.

http://jsfiddle.net/fulvio/VPzu4/200/

The plugin allows the use of font-awesome icons and glyph icons (both of course, do not offer any 0 - 9 numbers as icons. argh!)

  • http://getbootstrap.com/components/#glyphicons
  • http://fortawesome.github.io/Font-Awesome/cheatsheet/

The documentation mentions the ability to use extraClasses and I was wondering whether anyone could point me in the right direction as to how to leverage from this in order to display numbers rather than icons or whether there is simply another way to achieve this.

Thanks in advance for your help.

UPDATE:

Thanks for the comment @Can.

The author of awesome-markers got another tree where he added exactly what you are looking for awesome-markers with numbers/letters be sure to grab the unminified JS.

like image 914
fulvio Avatar asked Mar 24 '14 23:03

fulvio


People also ask

How do you add a custom marker in leaflet?

Marker Custom IconsStep 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.

How many markers can leaflet handle?

The Clusterer can handle 10,000 or even 50,000 markers (in chrome).

How do I use font awesome icons in leaflet?

You can use font-awesome icons instead of built-in marker icons like this: const fontAwesomeIcon = L. divIcon({ html: '<i class="fa fa-map-marker fa-4x"></i>', iconSize: [20, 20], className: 'myDivIcon' }); L.

What is l in Leaflet?

If you take a look at directly importing it, this is what they have at the last line : window.L = exports; and exports are the whole things one by one. cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet-src.js.


2 Answers

I have tried Numbered Markers plugin, but it icon is not pretty as other Awesome Markers, and make page layout style inconsistent, so I made small changes in Awesome-Markers plugin to make it support numbers. It is very simple.

  1. this is Numbered Markers plugin effect, if you like it please skip my answer. enter image description here

  2. change leaflet.awesome-markers.js line 2, add html:""

    L.AwesomeMarkers.Icon = L.Icon.extend({
    options: {
        iconSize: [35, 45],
        iconAnchor:   [17, 42],
        popupAnchor: [1, -32],
        shadowAnchor: [10, 12],
        shadowSize: [36, 16],
        className: 'awesome-marker',
        prefix: 'glyphicon',
        spinClass: 'fa-spin',
        extraClasses: '',
        icon: 'home',
        markerColor: 'blue',
        iconColor: 'white',
        html : ""
    },
    
  3. change leaflet.awesome-markers.js line 80,

    return "<i " + iconColorStyle + "class='" + options.extraClasses + " " 
    + options.prefix + " " + iconClass + " " + iconSpinClass + " " 
    + iconColorClass + "'>" + options.html + "</i>";
    
  4. when creating icon, call like before

    var jobMarkerIcon = L.AwesomeMarkers.icon({
    icon: '',
    markerColor: 'darkblue',
    prefix: 'fa',
    html: (i+1)
    });
    
  5. comment out line 45 and 47.

  6. the result is like below screenshot. enter image description here

  7. code changes diff shows below. enter image description here

like image 126
rockXrock Avatar answered Oct 09 '22 12:10

rockXrock


Instead of using the Awesome-Markers plugin, you could follow this article on creating numbered markers in Leaflet:

http://blog.charliecroom.com/index.php/web/numbered-markers-in-leaflet

The associated Gist is here:

https://gist.github.com/comp615/2288108

An simple example of how this would work is as follows:

// The text could also be letters instead of numbers if that's more appropriate
var marker = new L.Marker(new L.LatLng(0, 0), {
    icon:   new L.NumberedDivIcon({number: '1'})
});
like image 32
Ben Smith Avatar answered Oct 09 '22 10:10

Ben Smith