1. 程式人生 > >ArcGIS API for JavaScript實現地圖雙屏聯動

ArcGIS API for JavaScript實現地圖雙屏聯動

使用ArcGIS API for JavaScript,可以很方便的實現地圖聯動。最簡單的方式是藉助Map物件的extent-change 事件,當然,也可以藉助zoom-end、pan-end、mouse-drag-end等事件,本文使用第一種方式,實現地圖的平移聯動、滑鼠雙擊放大聯動、點選縮放按鈕聯動等,具體實現程式碼如下: [html] view plain copy print?
  1. <!-- 
  2. 功能:地圖分屏 
  3. -->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7.     <metahttp-equiv="Content-Type"
    content="text/html; charset=utf-8">
  8.     <!--The viewport meta tag is used to improve the presentation and behavior of the samples  
  9.       on iOS devices-->
  10.     <metaname="viewport"content="initial-scale=1, maximum-scale=1,user-scalable=no">
  11.     <title>地圖分屏</title>
  12.     <linkrel
    ="stylesheet"href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/claro/claro.css">
  13.     <linkrel="stylesheet"href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
  14.     <linkrel="stylesheet"href="css/fenping.css">
  15.     <scriptsrc="http://js.arcgis.com/3.8/"></script>
  16.     <script>
  17.         require([  
  18.             "dojo/ready",  
  19.             "dojo/parser",  
  20.             "dijit/layout/BorderContainer",  
  21.             "dijit/layout/ContentPane",  
  22.             "esri/map",  
  23.             "esri/geometry/Extent",  
  24.             "esri/layers/ArcGISTiledMapServiceLayer",  
  25.             "dojo/domReady!"  
  26.         ], function(ready,parser,BorderContainer, ContentPane,Map,Extent,ArcGISTiledMapServiceLayer) {  
  27.             parser.parse();  
  28.             /*  
  29.              * 向量地圖的構建  
  30.              */  
  31.             var vecMap=new Map("vecMap",{  
  32.                 extent: new Extent({xmin:105,ymin:31,xmax:110,ymax:40,  
  33.                     spatialReference:{wkid:4326}}),  
  34.                 logo:false  
  35.             });  
  36.             var tiledMapServiceLayerV=new ArcGISTiledMapServiceLayer("向量地圖服務的url");  
  37.             vecMap.addLayer(tiledMapServiceLayerV);  
  38.             /*  
  39.              * 影像地圖的構建  
  40.              */  
  41.             var imgMap=new Map("imgMap",{  
  42.                 extent: new Extent({xmin:105,ymin:31,xmax:110,ymax:40,  
  43.                     spatialReference:{wkid:4326}}),  
  44.                 logo:false  
  45.             })  
  46.             var tiledMapServiceLayerI=new ArcGISTiledMapServiceLayer("影像地圖的url");  
  47.             imgMap.addLayer(tiledMapServiceLayerI);  
  48.             /*  
  49.              * 新增extent-change事件,實現地圖聯動  
  50.              * flagV向量地圖的extent-change控制標誌,flagI影像地圖的extent-change控制標誌消除死迴圈情況  
  51.              */  
  52.             var flagV=true,flagI=true;  
  53.             vecMap.on("extent-change",function(){  
  54.                 if(flagV){  
  55.                     var vExtent=vecMap.extent;  
  56.                     imgMap.setExtent(vExtent);  
  57.                     flagI=false;  
  58.                 }else if(!flagV){  
  59.                     flagV=true;  
  60.                 }  
  61.             });  
  62.             imgMap.on("extent-change",function(){  
  63.                 if(flagI){  
  64.                     var iExtent=imgMap.extent;  
  65.                     vecMap.setExtent(iExtent);  
  66.                     flagV=false;  
  67.                 }else if(!flagI){  
  68.                     flagI=true;  
  69.                 }  
  70.             });  
  71.         });  
  72.     </script>
  73. </head>
  74. <bodyclass="claro">
  75. <divid="mainWindow"data-dojo-type="dijit.layout.BorderContainer"data-dojo-props="design:'headline'"
  76.      style="width:100%; height:100%;">
  77.     <divid="header"data-dojo-type="dijit.layout.ContentPane"data-dojo-props="region:'top'">
  78.         <divid="title">天地圖</div>
  79.         <divid="snippet">
  80.         </div>
  81.     </div>
  82.     <divid="vecMap"data-dojo-type="dijit.layout.ContentPane"data-dojo-props="region:'left'">
  83.     </div>
  84.     <divid="imgMap"data-dojo-type="dijit.layout.ContentPane"data-dojo-props="region:'center'">
  85.     </div>
  86. </div>
  87. </body>
  88. </html>
<!--
功能:地圖分屏
-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>地圖分屏</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
    <link rel="stylesheet" href="css/fenping.css">
    <script src="http://js.arcgis.com/3.8/"></script>
    <script>
        require([
            "dojo/ready",
            "dojo/parser",
            "dijit/layout/BorderContainer",
            "dijit/layout/ContentPane",
            "esri/map",
            "esri/geometry/Extent",
            "esri/layers/ArcGISTiledMapServiceLayer",
            "dojo/domReady!"
        ], function(ready,parser,BorderContainer, ContentPane,Map,Extent,ArcGISTiledMapServiceLayer) {
            parser.parse();
            /*
             * 向量地圖的構建
             */
            var vecMap=new Map("vecMap",{
                extent: new Extent({xmin:105,ymin:31,xmax:110,ymax:40,
                    spatialReference:{wkid:4326}}),
                logo:false
            });
            var tiledMapServiceLayerV=new ArcGISTiledMapServiceLayer("向量地圖服務的url");
            vecMap.addLayer(tiledMapServiceLayerV);
            /*
             * 影像地圖的構建
             */
            var imgMap=new Map("imgMap",{
                extent: new Extent({xmin:105,ymin:31,xmax:110,ymax:40,
                    spatialReference:{wkid:4326}}),
                logo:false
            })
            var tiledMapServiceLayerI=new ArcGISTiledMapServiceLayer("影像地圖的url");
            imgMap.addLayer(tiledMapServiceLayerI);
            /*
             * 新增extent-change事件,實現地圖聯動
             * flagV向量地圖的extent-change控制標誌,flagI影像地圖的extent-change控制標誌消除死迴圈情況
             */
            var flagV=true,flagI=true;
            vecMap.on("extent-change",function(){
                if(flagV){
                    var vExtent=vecMap.extent;
                    imgMap.setExtent(vExtent);
                    flagI=false;
                }else if(!flagV){
                    flagV=true;
                }
            });
            imgMap.on("extent-change",function(){
                if(flagI){
                    var iExtent=imgMap.extent;
                    vecMap.setExtent(iExtent);
                    flagV=false;
                }else if(!flagI){
                    flagI=true;
                }
            });

        });
    </script>
</head>

<body class="claro">
<div id="mainWindow" data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline'"
     style="width:100%; height:100%;">
    <div id="header" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'top'">
        <div id="title">天地圖</div>
        <div id="snippet">
        </div>
    </div>

    <div id="vecMap" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'left'">
    </div>
    <div id="imgMap" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'">
    </div>

</div>
</body>

</html>

      頁面對應的樣式檔案為fenping.css,程式碼如下

[css] view plain copy print?
  1. html,body {  
  2.   height:100%;  
  3.   width:100%;  
  4.   margin:0;  
  5.   padding:0;  
  6. }  
  7. body {  
  8.   background-color:#FFF;  
  9.   color:#333;  
  10.   font-size:14px;  
  11.   overflow:hidden;  
  12.   font-family:Helvetica"Trebuchet MS"Arialsans-serif;  
  13. }  
  14. #header {  
  15.   border:solid1px#224a54;  
  16.   height:50px;  
  17.   margin:2px;  
  18.   background-imageurl("../images/background.jpg");  
  19. }  
  20. #title {  
  21.   color:white;  
  22.   font-size:30px;  
  23.   text-align:left;  
  24.   font-weight:bolder;  
  25.   padding-top:10px;  
  26.   padding-left:15px;  
  27. }  
  28. #snippet {  
  29.   font-size:14px;  
  30.   color:#444;  
  31.   padding-left:15px;  
  32. }  
  33. #vecMap {  
  34.   border:solid1px#b0c4de;  
  35.   width:48%;  
  36.   margin:2px;  
  37. }  
  38. #imgMap {  
  39.   border:solid1px#b0c4de;  
  40.   width:48%;  
  41.   margin:2px;  
  42. }  
  43. .dj_ie .infowindow .window .top .right .user .content { positionrelative; }  
  44. .dj_ie .simpleInfoWindow .content {positionrelative;}  
  html,body {
    height:100%;
    width:100%;
    margin:0;
    padding:0;
  }

  body {
    background-color:#FFF;
    color:#333;
    font-size:14px;
    overflow:hidden;
    font-family:Helvetica, "Trebuchet MS", Arial, sans-serif;
  }

  #header {
    border:solid 1px #224a54;
    height:50px;
    margin:2px;
    background-image: url("../images/background.jpg");
  }

  #title {
    color:white;
    font-size:30px;
    text-align:left;
    font-weight:bolder;
    padding-top:10px;
    padding-left:15px;
  }

  #snippet {
    font-size:14px;
    color:#444;
    padding-left:15px;
  }

  #vecMap {
    border:solid 1px #b0c4de;
    width:48%;
    margin:2px;
  }

  #imgMap {
    border:solid 1px #b0c4de;
    width:48%;
    margin:2px;
  }
  .dj_ie .infowindow .window .top .right .user .content { position: relative; }
  .dj_ie .simpleInfoWindow .content {position: relative;}


      效果圖如下: