Monday 30 December 2019

openlayers 2 - Popup is stationary does not move with the location


I am using the GeoExt example and used the same code in my application. This is my code


The problem I am facing is that the Popup box in my application on http://128.196.142.94/geo/test/test.html is stationary. Where ever you may click, the popup simply does not move. Not sure where I am going wrong?




Answer



anchored: false should be anchored: true:


if (!popup) {
popup = new GeoExt.Popup({
title: "Popup",
width: 200,
maximizable: true,
collapsible: true,
map: mapPanel.map,
anchored: true,

listeners: {
close: function() {
// closing a popup destroys it, but our reference is truthy
popup = null;
}
}
});
}

And remove location: loc.xy from popup definition.



loc.xy is undefined and if you have a look into GeoExt Popup.js source you can find that in this case anchored property automatically sets to false.


UPDATE:


It looks like you are using old version of geoext library. I've adapted your example for working with this version:


var mapPanel, popup;

Ext.onReady(function() {

function addToPopup(loc) {

if (!popup) {

popup = new GeoExt.Popup({
title: "Popup",
width: 200,
maximizable: true,
collapsible: true,
map: mapPanel.map,
/* Fix #1
loc - object of OpenLayers.Pixel class
*/
location: loc,

anchored: true,
listeners: {
close: function() {
popup = null;
}
}
});
}

popup.add({

xtype: "box",
autoEl: {
/* Fix #2
Convert pixel coordinates to LonLat
*/
html: "You clicked on (" + mapPanel.map.getLonLatFromViewPortPx(loc).lon.toFixed(2) + ", " + mapPanel.map.getLonLatFromViewPortPx(loc).lat.toFixed(2) + ")"
}
});

/* Fix #3

It is not necessary because location was defined within constructor
*/
//popup.location = loc;

popup.doLayout();
popup.show();
}

var mapPanel = new GeoExt.MapPanel({
title: "Map",

renderTo: "container",
width: 650, height: 356,
layers: [
new OpenLayers.Layer.WMS(
"Global Imagery",
"http://maps.opengeo.org/geowebcache/service/wms",
{layers: "bluemarble"}
)
],
center: [0, 0],

zoom: 2
});

var control = new OpenLayers.Control.Click({
trigger: function(evt) {
/* Fix #4
Get pixel coordinates from click and send to popup constructor
*/
var loc = evt.xy;
if (popup) popup.close();

addToPopup(loc);
}
});

mapPanel.map.addControl(control);
control.activate();

});

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...