Chinaunix

标题: Google Maps实现右键菜单 [打印本页]

作者: 杨竹青    时间: 2008-07-29 13:52
标题: Google Maps实现右键菜单
java 代码
GEvent.addListener(map,"singlerightclick",function(pixel,tile, marker) {   
        // store the "pixel" info in case we need it later   
        // adjust the context menu location if near an egde   
        // create a GControlPosition   
        // apply it to the context menu, and make the context menu visible   
        clickedPixel = pixel;   
        var x=pixel.x;   
        var y=pixel.y;   
        if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }   
        if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }   
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));      
        pos.apply(contextmenu);   
    if(marker){   
            contextmenu.style.visibility = "visible";   
    }   
      });   

但是好像行不通,不管在不在marker上点击鼠标右键if(marker)永远成立,这可把我难住了,怎么办呢?经过一番的探索我发现这个事件的第二个参数"src"里面有一个property叫做:__marker__,是不是这就是我要找的?于是我就把代码改成这个样子:



java 代码
if (GBrowserIsCompatible()) {   
   
      // === Global variable that can be used by the context handling functions ==   
      var clickedPixel;     
   
      var map = new GMap2(document.getElementById("map"));   
      map.addControl(new GLargeMapControl());   
      map.addControl(new GMapTypeControl());   
      map.setCenter(new GLatLng(43,-79),8);   
      var marker = new GMarker(new GLatLng(43,-79),{title:"center"});     
      map.addOverlay(marker);   
      ...   
      // === listen for singlerightclick ===   
      GEvent.addListener(map,"singlerightclick",function(pixel,tile, marker) {   
        // store the "pixel" info in case we need it later   
        // adjust the context menu location if near an egde   
        // create a GControlPosition   
        // apply it to the context menu, and make the context menu visible   
        clickedPixel = pixel;   
        var x=pixel.x;   
        var y=pixel.y;   
        if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }   
        if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }   
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));      
        pos.apply(contextmenu);   
    if(tile.__marker__){alert(tile.__marker__)   
            contextmenu.style.visibility = "visible";   
    }else{   
        contextmenu.style.visibility = "hidden";   
    }   
      });




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2