Arcgis Server for JavaScript API之自定义InfoWindow

20 篇文章 0 订阅
16 篇文章 0 订阅

用过Arcgis Server for JavaScript API肯定知道InfoWIndow,你在用InfoWindow的时候会发现各种问题,例如不能完全显示的问题,遮盖对象的问题等等,所以呢我在实现这个功能的时候动了下脑子,想自己用div+css弄一个,倒腾了半天,弄出来了一个如下所示的:


做的比较丑陋,样式方面还得好好下下功夫,东西是差不多实现了,下面说说思路:

首先,DIV定义,这个样式,我定义了5个div,分别是infowin,title,colse,content,arrow,其中,infowin是整个InfoWindow的大框架,title为标题,close为关闭按钮,content为主要内容,arrow为下面的小尾巴,我们可以将这个小尾巴做的长一点,以免对象被遮盖的情况,代码为:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <div id="mapDiv">  
  2.     <div id="infowin">  
  3.         <div id="close" onClick="closeInfoWin()">X</div>  
  4.         <div id="title"></div>  
  5.         <div id="content"></div>  
  6.         <div id="arrow"></div>  
  7.     </div>  
  8. </div>  

定义了div就得进行布局,定义样式了,样式为:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.   <style>  
  2.         html, body, #mapDiv   
  3.  {  
  4.               padding:0;  
  5.               margin:0;  
  6.               height:100%;  
  7. font-size:10px;  
  8. position: relative;  
  9.         }  
  10.  #infowin  
  11.  {          
  12.   display:none;  
  13.   z-index:10000;        
  14.  }  
  15.  #close  
  16.  {  
  17.   float:right;  
  18.   padding-top:10px;  
  19.   font-weight:bold;  
  20.   font-size:12px;  
  21.   color:#FFF;  
  22.   border:#000 1px solid;  
  23.   height:20px;  
  24.   width:20px;  
  25.   text-align:center;  
  26.  }  
  27.   #close:hover  
  28.  {  
  29.   cursor:pointer;  
  30.  }  
  31.  #title  
  32.  {  
  33.   background-color:#666;  
  34.   padding:10px;  
  35.   font-weight:bold;  
  36.   font-size:12px;  
  37.  }  
  38.  #content  
  39.  {  
  40.   padding-left:10px;  
  41.   padding-top:10px;  
  42.   background-color:#999;  
  43.   height:200px;  
  44.  }  
  45.  #arrow  
  46.  {  
  47.   background-image:url(arrow.png);  
  48.   height:30px;  
  49.  }  
  50.   </style>  
样式定义完之后就得考虑事件了,一般InfoWindow是在点击某个对象时弹出来的,所以我们得定义对象图层的click事件:

[javascript]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">      </span>function leftClick(evt){  
  2.             infowin.style.display="none";  
  3.               
  4.             var strtitle="城市名称"           
  5.             var strcontent = "****是一座美丽的城市<br><br>****是一座好看的城市<br><br>****是一座富饶的城市<br><br>****是一座漂亮的城市";  
  6.               
  7.             infowin.style.left=(evt.clientX-width/2)+"px";  
  8.             infowin.style.top=(evt.clientY-height-50)+"px";   
  9.             infowin.style.position="absolute";  
  10.             infowin.style.width=width+"px";  
  11.             infowin.style.height=height+"px";  
  12.             infowin.style.display="block";  
  13.               
  14.             title.innerHTML = strtitle;  
  15.             content.innerHTML = strcontent;  
  16.   
  17.         }  
  18.         //鼠标单击  
  19.         featurelayercity.on("click", leftClick);  
点击对象,在鼠标的点击位置出现,所以我们得将infowin的position样式设为absolute,并定义left和top分别为clientX和clientY,并将其display设置为block,将其显示,实现的详细代码如下:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.     <!--The viewport meta tag is used to improve the presentation and behavior of the samples   
  6.       on iOS devices-->  
  7.     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">  
  8.     <title>Feature Layer - display results as an InfoWindow onHover</title>  
  9.   
  10.     <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.8/3.8/js/dojo/dijit/themes/claro/claro.css">  
  11.     <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.8/3.8/js/dojo/dijit/themes/tundra/tundra.css">  
  12.     <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.8/3.8/js/esri/css/esri.css">  
  13.     <style>  
  14.       html, body, #mapDiv   
  15.       {  
  16.         padding:0;  
  17.         margin:0;  
  18.         height:100%;  
  19.         font-size:10px;  
  20.         position: relative;  
  21.       }  
  22.       #infowin  
  23.       {         
  24.           display:none;  
  25.           z-index:10000;        
  26.       }  
  27.       #close  
  28.       {  
  29.           float:right;  
  30.           padding-top:10px;  
  31.           font-weight:bold;  
  32.           font-size:12px;  
  33.           color:#FFF;  
  34.           border:#000 1px solid;  
  35.           height:20px;  
  36.           width:20px;  
  37.           text-align:center;  
  38.       }  
  39.        #close:hover  
  40.       {  
  41.           cursor:pointer;  
  42.       }  
  43.       #title  
  44.       {  
  45.           background-color:#666;  
  46.           padding:10px;  
  47.           font-weight:bold;  
  48.           font-size:12px;  
  49.       }  
  50.       #content  
  51.       {  
  52.           padding-left:10px;  
  53.           padding-top:10px;  
  54.           background-color:#999;  
  55.           height:200px;  
  56.       }  
  57.       #arrow  
  58.       {  
  59.           background-image:url(arrow.png);  
  60.           height:30px;  
  61.       }  
  62.     </style>      
  63.     <script src="http://js.arcgis.com/3.9/"></script>  
  64.     <script>  
  65.       var infowin,colse,title,content;  
  66.         
  67.       var width=400,height=230;  
  68.         
  69.       var closeInfoWin = function (evt){  
  70.           infowin=document.getElementById("infowin");  
  71.           infowin.style.display="none";  
  72.       };  
  73.   
  74.       require([  
  75.         "esri/map", //地图  
  76.         "esri/layers/ArcGISTiledMapServiceLayer",  
  77.         "esri/layers/FeatureLayer",//特征层  
  78.         "esri/symbols/PictureMarkerSymbol",//图片点符号  
  79.         "esri/renderers/SimpleRenderer", //简单渲染  
  80.         "esri/graphic", //图片  
  81.         "esri/lang",               
  82.         "dojo/domReady!"  
  83.       ], function(  
  84.         Map,ArcGISTiledMapServiceLayer,FeatureLayer,PictureMarkerSymbol,SimpleRenderer,esriLang  
  85.       ) {  
  86.         var map = new Map("mapDiv", {  
  87.           logo:false,  
  88.           center: [106.6854, 35.8364],  
  89.           zoom: 4,  
  90.           slider: true  
  91.         });   
  92.           
  93.         var shpServiceURL="***************************************";  
  94.         var shpTitlelayer=new ArcGISTiledMapServiceLayer(shpServiceURL);  
  95.         map.addLayer(shpTitlelayer);  
  96.   
  97.         //--------------------------------------------------------------------------------------------------------  
  98.         var featurelayercity = new FeatureLayer("******************************************************", {  
  99.           mode: FeatureLayer.MODE_SNAPSHOT,  
  100.           outFields: ["*"]  
  101.         });  
  102.         var pmsRed = new PictureMarkerSymbol('../images/location_icon_blue.png', 20, 20).setOffset(0, 15);  
  103.         //简单渲染  
  104.         var sr=new SimpleRenderer(pmsRed);  
  105.         featurelayercity.setRenderer(sr);         
  106.         map.addLayer(featurelayercity);   
  107.           
  108.         infowin = document.getElementById("infowin");  
  109.         colse = document.getElementById("close");  
  110.         title = document.getElementById("title");  
  111.         content = document.getElementById("content");  
  112.         function leftClick(evt){  
  113.             infowin.style.display="none";  
  114.               
  115.             var strtitle="城市名称"           
  116.             var strcontent = "****是一座美丽的城市<br><br>****是一座好看的城市<br><br>****是一座富饶的城市<br><br>****是一座漂亮的城市";  
  117.               
  118.             infowin.style.left=(evt.clientX-width/2)+"px";  
  119.             infowin.style.top=(evt.clientY-height-50)+"px";   
  120.             infowin.style.position="absolute";  
  121.             infowin.style.width=width+"px";  
  122.             infowin.style.height=height+"px";  
  123.             infowin.style.display="block";  
  124.               
  125.             title.innerHTML = strtitle;  
  126.             content.innerHTML = strcontent;  
  127.   
  128.         }  
  129.         //鼠标单击  
  130.         featurelayercity.on("click", leftClick);          
  131.       });  
  132.     </script>  
  133.   </head>  
  134.   <body class="tundra">  
  135.     <div id="mapDiv">  
  136.         <div id="infowin">  
  137.             <div id="close" onClick="closeInfoWin()">X</div>  
  138.             <div id="title"></div>  
  139.             <div id="content"></div>  
  140.             <div id="arrow"></div>  
  141.         </div>  
  142.     </div>  
  143.   </body>  
  144. </html>  
目前只实现到了这儿, 还有以下问题待解决:1、地图拖动后infowin随着地图的联动;2、地图缩放后infowin随着地图的联动;3、内容不在可视范围时候的移动;4、样式,挺难看的。希望有人实现后共享下代码,造福全GISer。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值