ThinkGeo.com    |     Documentation    |     Premium Support

ContextMenuItem OnClientClick

I am setting up a ContextMenu on my Map and would like to fire off some client side code. I noticed that no event arguments are getting passed into my javascript functions. Also, my Maps OnClientClick function is getting raised as well as the ContextMenuItems OnClientClick function


Here is how I setup the context menu:



ContextMenu contextMenu = new ContextMenu("divContextMenu", 150);
ContextMenuItem contextMenuItem = new ContextMenuItem("Center Map Here");
contextMenuItem.OnClientClick = "CenterMapHere";
contextMenu.MenuItems.Add(contextMenuItem);
Map1.ContextMenu = contextMenu;

Here is my client function



function CenterMapHere(e) // e is undefined
{
    // trying to grab point where user right clicked,
    // without also raising Map1.OnClientClick event
    var lonlat = this.getLonLatFromViewPortPx(e.xy); 
    var x = lonlat.lon;
    var y = lonlat.lat;
    Map1.SetCenter(x,y);
}


I think I solved my own problems here. Hopefully someone else will find this useful.



The firing of both events was fixed by adding the workaround mentioned in:
 gis.thinkgeo.com/Support/DiscussionForums/tabid/143/aff/12/aft/5355/afv/topic/Default.aspx


As for getting the point that the user clicked, I was able to get a handle on the ContextMenu div, and used its Top Left corner. Here is the revised client function



function CenterMapHere() {
  var divContextMenu = $get('divContextMenu');
  divContextMenu.style.display = 'none';
  var pixel = new OpenLayers.Pixel(divContextMenu.style.pixelLeft, divContextMenu.style.pixelTop);
  var lonlat = olMap.getLonLatFromViewPortPx(pixel);
  var x = lonlat.lon;
  var y = lonlat.lat;
  Map1.SetCenter(x,y);
}


Thanks for sharing, Rob.