Google Maps实现右键菜单

Google Maps实现右键菜单

        最近在做公司项目(www.youmonitor.us 它可以帮助你检测你的网站的稳定性,如果你的网站当了,它会给你发警报,而且是免费的,有兴趣的朋友可以去看看,不是广告,希望版主不要删我的帖子)的第二版,需要用到Google Maps的右键菜单(类似google maps官方网站的右键菜单功能 http://maps.google.com/),但是好像google的官方API上面没有这种示例,于是求助google的搜索引擎,找到了这篇文章:http://www.econym.demon.co.uk/googlemaps/context.htm,发现google maps自己有一个鼠标右键事件:singlerightclick,文章我就不翻译了,英文不好的朋友可以直接看它的代码:http://www.econym.demon.co.uk/googlemaps/examples/context.htm。但是这篇文章还有一个美中不足,就是我希望在用户点击了marker以后弹出的右键菜单和在别的地方弹出的不一样。类似这样的效果:

 

 

 

 

 

 

 

 

 

 

 

 

marker上的比其它的多了Clear Search Results这一项,起初我想要使用事件传进来的参数marker("singlerightclick"的第三个参数是overlay?http://www.google.com/apis/maps/documentation/reference.html)来判断:   

java 代码
  1. GEvent.addListener(map,"singlerightclick",function(pixel,tile, marker) {   
  2.         // store the "pixel" info in case we need it later   
  3.         // adjust the context menu location if near an egde   
  4.         // create a GControlPosition   
  5.         // apply it to the context menu, and make the context menu visible   
  6.         clickedPixel = pixel;   
  7.         var x=pixel.x;   
  8.         var y=pixel.y;   
  9.         if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }   
  10.         if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }   
  11.         var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));     
  12.         pos.apply(contextmenu);   
  13.     if(marker){   
  14.             contextmenu.style.visibility = "visible";   
  15.     }   
  16.       });  

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

 

java 代码
  1. if (GBrowserIsCompatible()) {   
  2.   
  3.       // === Global variable that can be used by the context handling functions ==   
  4.       var clickedPixel;    
  5.   
  6.       var map = new GMap2(document.getElementById("map"));   
  7.       map.addControl(new GLargeMapControl());   
  8.       map.addControl(new GMapTypeControl());   
  9.       map.setCenter(new GLatLng(43,-79),8);   
  10.       var marker = new GMarker(new GLatLng(43,-79),{title:"center"});    
  11.       map.addOverlay(marker);   
  12.       ...   
  13.       // === listen for singlerightclick ===   
  14.       GEvent.addListener(map,"singlerightclick",function(pixel,tile, marker) {   
  15.         // store the "pixel" info in case we need it later   
  16.         // adjust the context menu location if near an egde   
  17.         // create a GControlPosition   
  18.         // apply it to the context menu, and make the context menu visible   
  19.         clickedPixel = pixel;   
  20.         var x=pixel.x;   
  21.         var y=pixel.y;   
  22.         if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }   
  23.         if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }   
  24.         var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));     
  25.         pos.apply(contextmenu);   
  26.     if(tile.__marker__){alert(tile.__marker__)   
  27.             contextmenu.style.visibility = "visible";   
  28.     }else{   
  29.         contextmenu.style.visibility = "hidden";   
  30.     }   
  31.       });  

搞定,It works! 原来google还留了一手的,呵呵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值