2

I'm working with openLayers 3, which is new to me, and I've been able to add a popup to my map that comes up when you click on a feature in a vector layer. The trouble is the popup content does not update when you click on another feature; you need to click on an area where there isn't a feature to close it, then click on another feature for the popup content to refresh. I'd like for the popup content to update when you click on a new feature, without having to click on an area where there is not feature first. Is there a way I can do this? Here is the code for the popup that I am using:

map.on('click', function(evt) {
  var feature = map.forEachFeatureAtPixel(evt.pixel,
      function(feature, layer) {
        return feature;
      });
  if (feature) {
    popup.setPosition(evt.coordinate);
    $(element).popover({
      'placement': 'top',
      'html': true,
      'content': '<b>Parcel No: </b><br>'+
      feature.get('parcel')+'<br>'+
      '<b>Owner: </b><br>'+
      feature.get('primowner')
    });
    $(element).popover('show');
  } else {
    $(element).popover('destroy');
  }
});

1 Answer 1

2

Destroy it first, then show:

  if (feature) {

      $(element).popover('destroy');
      popup.setPosition(coordinate);
      // the keys are quoted to prevent renaming in ADVANCED mode.
      $(element).popover({
          'placement': 'top',
          'animation': false,
          'html': true,
          'content': feature.get('txt')
      });
      $(element).popover('show');
  } else {
      $(element).popover('destroy');
      popup.setPosition(undefined);
  }

http://jsfiddle.net/jonataswalker/9qorz7kd/

0

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.