0
votes

I have a map on which I plot features and give each of these features a title and description which I later use with a popup.

I create the features like such:

for (i = 0; i < locations.length; i++) { 
    var feature = new ol.Feature(
        new ol.geom.Point(ol.proj.transform([locations[i]['longitude'], locations[i]['latitude']], 'EPSG:4326', 'EPSG:3857'))
    );
    feature.title = locations[i]['name'];
    feature.description = 'Latitude: '+locations[i]['latitude']+'<br>Longitude: '+locations[i]['longitude']+'<br><a href="#" title="">Test</a>';
    feature.setStyle(circle);
    markerLayer.getSource().addFeature(feature);
}

This works fine and creates all the locations on the map.

Onclick I call the popover function from bootstrap like such:

map.on('click', function(evt) {
    var feature = map.forEachFeatureAtPixel(evt.pixel,
        function(feature, layer) {
            return feature;
        });
    if (feature) {
        popup.setPosition(feature.getGeometry().getCoordinates());
        $(element).popover({
            'placement': 'right',
            'html': true,
            'content': feature.description,
            'title': feature.title
        });
        $(element).popover('show');

    } else {
        $(element).popover('destroy');
    }
});

This works partially. It shows the popup when I click a feature and it closes it when I click on the map (where there's no feature). However, if I have a popup open and click another feature, it will open the popup on the right location but it won't change the content and title of the feature.

I thought I could solve this by first destroying the popover by adding

$(element).popover('destroy');

before setting the location of the popup and creating a new popover. If I do this however I get errors in Jquery and Bootstrap:

bootstrap.min.js:6 Uncaught TypeError: Cannot read property 'trigger' of null
at HTMLDivElement.q (bootstrap.min.js:6)
at HTMLDivElement.e (jquery.min.js:3)
at HTMLDivElement.handle (bootstrap.min.js:6)
at HTMLDivElement.dispatch (jquery.min.js:3)
at HTMLDivElement.q.handle (jquery.min.js:3)
at Object.trigger (jquery.min.js:4)
at HTMLDivElement.<anonymous> (jquery.min.js:4)
at Function.each (jquery.min.js:2)
at r.fn.init.each (jquery.min.js:2)
at r.fn.init.trigger (jquery.min.js:4)

Any idea how I can solve this problem?

1

1 Answers

1
votes

You try this, should work!

   if(feature){
                popup.setPosition(feature.getGeometry().getCoordinates());
                $(element).attr('data-placement', 'top');
                $(element).attr('data-html', true);
                $(element).attr('data-original-title', feature.get('your title key'));
                $(element).attr('data-content', feature.get('key - what you would like to include'));
                $(element).popover('show');

            }

            else{
                $(element).popover('destroy');
            }