(轉)Arcgis for Js之鼠標經過顯示對象名的實現


http://blog.csdn.net/gisshixisheng/article/details/41889345

在瀏覽地圖時,移動鼠標經過某個對象或者POI的時候,能夠提示該對象的名稱對用戶來說是很實用的,本文講述在Arcgis for Js中,用兩種不同的方式來實現該效果。

 

為了有個直觀的概念,先給大家看看實現后的效果:

百度地圖的效果

效果1

效果2

直觀的看到了效果,下面說說在Arcgis for Js中實現的兩種方式。在實現給效果的時候,有layer的兩個事件,mouse-over和mouse-out事件,鼠標經過顯示對象名稱,鼠標移除清除顯示。

1、通過TextSymbol和GraphicMarkerSymbol實現

通過這種方式顯示是直接用Arcgis的方式實現的,實現的代碼如下,效果為效果2:

 

[javascript]  view plain  copy
 
 print?
  1. function mouseOverLayer(e){  
  2.     map.setMapCursor("pointer");  
  3.     console.log(e.graphic);   
  4.     var font  = new esri.symbol.Font();  
  5.     font.setSize("10pt");  
  6.     font.setFamily("微軟雅黑");  
  7.     var cpoint = event.graphic.geometry;  
  8.     var text = new esri.symbol.TextSymbol(event.graphic.attributes.name);  
  9.     text.setFont(font);  
  10.     text.setColor(new dojo.Color([0,0,0,100]));  
  11.     text.setOffset(20,-35);  
  12.       
  13.     pmsTextBg.setOffset(20,-30);  
  14.     var textLength=event.graphic.attributes.name.length;  
  15.     pmsTextBg.setWidth(textLength*13.5+5);  
  16.     var bgGraphic = new esri.Graphic(cpoint, pmsTextBg);  
  17.     showTextLayer.add(bgGraphic);  
  18.     var labelGraphic = new esri.Graphic(cpoint,text);  
  19.     showTextLayer.add(labelGraphic);  
  20.       
  21. };  
  22. function mouseOutLayer(){  
  23.     map.graphics.clear();  
  24.     showTextLayer.clear();  
  25.     map.setMapCursor("default");  
  26. }  


2、直接用div顯示

 

通過獲取鼠標點位置或者幾何體位置,將位置轉換為屏幕坐標,將信息用div的形式展示出來,代碼如下,效果為效果1:

 

[html]  view plain  copy
 
 print?
  1. function mouseOverLayer(e){  
  2.     map.setMapCursor("pointer");  
  3.     console.log(e.graphic.attributes);  
  4.     var scrPt = map.toScreen(e.graphic.geometry);  
  5.     console.log(scrPt);  
  6.     var textDiv = dojo.doc.createElement("div");  
  7.     dojo.attr(textDiv,{  
  8.         "id":"text"  
  9.     });  
  10.     dojo.style(textDiv, {  
  11.         "left": scrPt.x+10 + "px",  
  12.         "top": scrPt.y+10 + "px",  
  13.         "position": "absolute",  
  14.         "z-index":99,  
  15.         "background":"#fcffd1",  
  16.         "font-size":"10px",  
  17.         "border":"1px solid #0096ff",  
  18.         "padding": "0.1em 0.3em 0.1em",  
  19.         "font-size": "11px",  
  20.         "border-radius": "3px",  
  21.         "box-shadow": "0 0 0.75em #777777"  
  22.     });  
  23.     textDiv.innerHTML =e.graphic.attributes.name;  
  24.     dojo.byId("map").appendChild(textDiv);  
  25. };  
  26. function mouseOutLayer(e){  
  27.     map.setMapCursor("default");  
  28.     dojo.byId("map").removeChild(dojo.byId("text"));  
  29. };  


比較:

 

以上兩種方式都可實現相同的效果,但就實現的難易程度,第二種比第一種簡單,在實現的美觀程度上,第二種比第一種更好調整與控制,在實現效率上,第二種比第一種好一點,可是,就在與地圖的結合上,很顯然,第二種比第一種稍微差一點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM