[GIS] How to get the popup value to change when I click on another feature, without clicking on a map area where there are no features first

openlayerspopupvector-layerwfs

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');
  }
});

Best Answer

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/

Related Question