0

I want to use the bootstrap glyphicon set as markers for a kml layer in an application using openlayers 3. I have tried the following:

var shopStyle = new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [0.5, 0.5],
            size: [50, 50],
            opacity: 1,
            scale: 0.25,
            src: $('<span class="glyphicon glyphicon-shopping-cart"></span>')
        })
    });

This doesn't work - the layer isn't rendered at all. What is the right way to do it?

3 Answers 3

2

Although the dvmac01 concerns, I would like to complete the answer with glyphicons. You can use the folowing Text Style:

var styleIcon = new ol.style.Style({
    text: new ol.style.Text({
        text: '\ue062',
        font : 'normal 18px "Glyphicons Halflings"',
        textBaseline: 'Bottom',
        fill: new ol.style.Fill({
            color: 'blue',
        })                    
    })
3
  • This is not working on my end. Which version of OL and BS css do you use ? (I'm using OL 4.6 and BS 3.7) Commented Jan 4, 2018 at 15:23
  • I used it some time ago at the time with ol 3.15. If it helps, I made a fiddle jsfiddle.net/klaifer/7fzmqoet/5 Commented Jan 4, 2018 at 18:16
  • Thanks! I got it working, keeping those versions. I don't know why but the icon doesn't appear on the first page call (clear cache), it appears correctly on second refresh. Beware that BS4 drops glyphicons. I'll try to switch to font-awesome. Commented Jan 5, 2018 at 10:18
1

I could be a little off-track here but to my knowledge it isn't possible to define an icon this way using glyphicons as the ol.style.icon method is for images. Essentially you're trying to use an icon font which needs a text character (subsequently styled by css). Here's an example that I got working but have shelved for now as I couldn't find a way to allow the user to update them and they didn't seem to show up on mobile devices properly.

var FAstyle = function (feature, resolution) {

    var iconFont = 'font-awesome';
    var iconFontText = '\uf041'; // equates to fa-trash icon
    var iconSize = 24;
    var col = '#F58026';

    var styles = [];

    var styleIcon = new ol.style.Style({
        text: new ol.style.Text({
            font: 'Normal ' + iconSize + 'px ' + iconFont,
            text: iconFontText,
            fill: new ol.style.Fill({ color: col })
        })
    });
    styles.push(styleIcon);

    console.log(feature.get("type"));
    return styles;
    return function (feature, resolution) {
        styles.styleIcon.getText().setText(feature.get("iconCode"));
        return styles;
    };
};

Don't forget to include the relevant css files in the head of your document too ;)

2
  • Is it possible to use 2 text styles (text below icon)?
    – Joel
    Commented Jul 22, 2016 at 13:18
  • Got it working with font : 'normal 24px FontAwesome' Commented Jan 5, 2018 at 10:48
0

This is the way to do this, you can also set the stroke. Be careful, if you're trying to take the contents code from this site, remember to put the "u" character inside the string, like this: '\e059' -> '\ue059', otherwise it will not work.

var styleIcon = new ol.style.Style({
text: new ol.style.Text({
    text: '\ue059',
    font : 'normal 18px "Glyphicons Halflings"',
    stroke: new ol.style.Stroke({ color: 'black', width: 2 })
    fill: new ol.style.Fill({color: 'yellow'})
    textBaseline: 'Bottom',
    scale: 1              
})

Here is a working jsfiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.