Is possible keep popup open when map redraw?

Viewed 55

I have a map that works perfectly, not that this is a problem, but when I switch layers with popup triggered, the popup disappears, I would like to know if there is something possible for it to stay open when another layer is loaded. Or get the click coordinates to call again, something like that. This is part of my code where I call the popup;

var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');


/**
 * Create an overlay to anchor the popup to the map.
 */
var overlay = new ol.Overlay({
  element: container,
  autoPan: true,
  autoPanAnimation: {
    duration: 250
  }
});

let zoomMapInit = 5;

if ($(document).width() <= 768) {
  zoomMapInit = 4;
  chartWidth = 200, chartHeight = 100;
  showZoomMap = false;
}

const view = new ol.View({
  projection: 'EPSG:4326',
  center: [-54.672122, -13.746438],
  zoom: zoomMapInit
});

const map = new ol.Map({
  target: 'map',
  layers: [new ol.layer.Tile({
    source: new ol.source.OSM({
      attributions: [
        '&copy; <a href="http://www.i3gs.org/" target="_blank">i3GS</a> | ',
        ol.source.OSM.ATTRIBUTION
      ],
    })
  }), switcherLayersGroupBiomas, switcherLayersGroupCamadas],
  overlays: [overlay],
  controls: ol.control.defaults({
    zoom: showZoomMap,
  }),
  view: view
});

const layerSwitcher = new ol.control.LayerSwitcher({
  tipLabel: 'Layers',
  groupSelectStyle: 'children',
  collapse: false,
});
map.addControl(layerSwitcher);

addNewUniqueLayer(geoserverGetLayerHome());

map.on('singleclick', function (event) {
  overlay.setPosition(undefined);
  currentYearValue = '';
  insertChart(event);
  cleanMunicipio();
  $("#map").click();
});
1 Answers

I managed to solve this, but it's not the best way and I believe it's not even the most professional. I created a function that would receive the lat and long that I save in a variable at each new click. It was a better solution at the time, but I believe it can be improved with time. Don't forget called this function after layers redraw.


var new_coordinate;

function updatePopup() {
  if (new_coordinate != undefined) {
    insertChart(new_coordinate);
  } else {
    true
  }
}

map.on('singleclick', function (event) {
  overlay.setPosition(undefined);
  currentYearValue = '';
  insertChart(event.coordinate);
  cleanMunicipio();
  new_coordinate = event.coordinate;
});

Related