(转)Arcgis for javascript实现百度地图ABCD marker的效果

概述:

在我的博客中,有一篇相关的文章,这段时间,有很多人问我求源码,只是时间过去已长,源代码已找不到,乘着这个9.3放假,又重新实现了下,并相关代码做了优化,在此贴出来,方便大家使用。

相关文章地址:

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

 

实现后效果:

为直观期间,先贴出来我做的效果

列表展示和地图展示以及联动

显示信息

 

实现思路:

1、列表与地图的互动

鼠标经过列表时,修改列表图标,并根据列表返回的值在地图上绘蓝色的marker;鼠标移出,修改列表图标为红色,清空地图marker图层。

关键代码:

 

[javascript] view plain copy
 
 print?
  1. title.on("mouseover",function(){  
  2.     var attr = $(this).data("attr");  
  3.     $("#icon"+attr.id).css("background","url('images/blue.png')");  
  4.     var pt=new Point(attr.x,attr.y,{"wkid":4326});  
  5.     var pms = new esri.symbol.PictureMarkerSymbol("images/blue.png",24,26)  
  6.     var gImg = new Graphic(pt,pms);  
  7.     gLyrHover.add(gImg);  
  8. });  
  9. title.on("mouseout",function(){  
  10.     var attr = $(this).data("attr");  
  11.     $("#icon"+attr.id).css("background","url('images/red.png')");  
  12.     gLyrHover.clear();  
  13. });  
 


2、地图与列表的互动

 

鼠标经过地图红色的marker时,修改对应列表图标,并将红色 marker的图片换成蓝色的;鼠标移出,修改对应列表图标,并修改marker为红色。

关键代码:

 

[javascript] view plain copy
 
 print?
  1. gLyr.on("mouse-over",function(e){  
  2.     map.setMapCursor("pointer");  
  3.     var sms = e.graphic.symbol;  
  4.     sms.url = "images/blue.png";  
  5.     gLyr.redraw();  
  6.     $("#icon"+e.graphic.attributes.id).css("background","url('images/blue.png')");  
  7. });  
  8. gLyr.on("mouse-out",function(e){  
  9.     map.setMapCursor("default");  
  10.     var sms = e.graphic.symbol;  
  11.     sms.url = "images/red.png";  
  12.     gLyr.redraw();  
  13.     $("#icon"+e.graphic.attributes.id).css("background","url('images/red.png')");  
  14. });  


3、地图上ABCD的文字是一个单独的图层,不参与互动。

 

4、数据以JSON形式存在。

 

[javascript] view plain copy
 
 print?
  1. var data = [  
  2.     {  
  3.         "id":"A","name":"拉萨", "x":91.162998, "y":29.71042,  
  4.         "desc":"拉萨是中国西藏自治区的首府,西藏的政治、经济、文化和宗教中心,也是藏传佛教圣地。"  
  5.     },  
  6.     {  
  7.         "id":"B", "name":"西宁","x":101.797303,"y":36.593642,  
  8.         "desc":"西宁是青海省的省会,古称西平郡、青唐城,取”西陲安宁“之意,是整个青藏高原最大的城市。"  
  9.     },  
  10.     {  
  11.         "id":"C","name":"兰州","x":103.584297,"y":36.119086,  
  12.         "desc":"兰州,甘肃省省会,西北地区重要的工业基地和综合交通枢纽,西部地区重要的中心城市之一,丝绸之路经济带的重要节点城市。"  
  13.     },  
  14.     {  
  15.         "id":"D","name":"成都","x":104.035508,"y":30.714179,  
  16.         "desc":"成都,简称蓉,四川省省会,1993年被国务院确定为西南地区的科技、商贸、金融中心和交通、通讯枢纽。"  
  17.     }  
  18. ];  



 

完整代码:

 

[html] view plain copy
 
 print?
    1. <!DOCTYPE html>  
    2. <html>  
    3. <head>  
    4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    5.     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>  
    6.     <title></title>  
    7.     <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.9/3.9/js/esri/css/esri.css">  
    8.     <style type="text/css">  
    9.         html, body, #map {  
    10.             height: 100%;  
    11.             margin: 0;  
    12.             padding: 0;  
    13.             font-size: 62.5%;  
    14.             font-family:"微软雅黑";  
    15.         }  
    16.         .search-box{  
    17.             z-index: 99;  
    18.             background: #fff;  
    19.             border: 1px solid #888888;  
    20.             border-radius: 5px;  
    21.             width: 220px;  
    22.             max-height:600px;  
    23.             overflow-y: auto;  
    24.             position: absolute;  
    25.             top: 120px;  
    26.             left: 10px;  
    27.         }  
    28.         .search-box-title{  
    29.             padding: 6px 10px;  
    30.             text-align: left;  
    31.             font-size: 13px;  
    32.             font-weight: bold;  
    33.             color: #f2f2f2;  
    34.             background: #85b0db;  
    35.         }  
    36.         .search-box-result{  
    37.             list-style: none;  
    38.             margin-left:-40px;  
    39.             margin-top: 0px;  
    40.         }  
    41.         .search-box-result-item{  
    42.             border-bottom: 1px solid #eeeeee;  
    43.             padding: 5px 8px;  
    44.         }  
    45.         .search-name{  
    46.             float: right;  
    47.             font-weight: bold;  
    48.             font-size: 13px;  
    49.             margin-top: 3px;  
    50.             margin-right: 10px;  
    51.         }  
    52.         .search-name-title{  
    53.             background: #f2f2f2;  
    54.         }  
    55.         .search-name-title:hover{  
    56.             cursor: pointer;  
    57.         }  
    58.         .search-detail{  
    59.             border-top:  1px dashed #eeeeee;  
    60.             margin-top: 3px;  
    61.             padding: 3px 5px;  
    62.             line-height: 18px;  
    63.         }  
    64.         .search-icon{  
    65.             background: url("images/red.png");  
    66.             width: 24px;  
    67.             height: 26px;  
    68.             background-repeat: no-repeat;  
    69.         }  
    70.         .search-text{  
    71.             color: #ffffff;  
    72.             font-weight: bold;  
    73.             font-size: 16px;  
    74.             margin-left:7px ;  
    75.         }  
    76.         .detail{  
    77.             color: #85b0db;  
    78.             font-weight: bold;  
    79.             text-align: right;  
    80.         }  
    81.         .detail:hover{  
    82.             cursor: pointer;  
    83.         }  
    84.     </style>  
    85.     <script src="http://localhost/arcgis_js_api/library/3.9/3.9/init.js"></script>  
    86.     <script src="jquery-1.8.3.js"></script>  
    87.     <script type="text/javascript">  
    88.         var map;  
    89.         var data = [  
    90.             {  
    91.                 "id":"A","name":"拉萨", "x":91.162998, "y":29.71042,  
    92.                 "desc":"拉萨是中国西藏自治区的首府,西藏的政治、经济、文化和宗教中心,也是藏传佛教圣地。"  
    93.             },  
    94.             {  
    95.                 "id":"B", "name":"西宁","x":101.797303,"y":36.593642,  
    96.                 "desc":"西宁是青海省的省会,古称西平郡、青唐城,取”西陲安宁“之意,是整个青藏高原最大的城市。"  
    97.             },  
    98.             {  
    99.                 "id":"C","name":"兰州","x":103.584297,"y":36.119086,  
    100.                 "desc":"兰州,甘肃省省会,西北地区重要的工业基地和综合交通枢纽,西部地区重要的中心城市之一,丝绸之路经济带的重要节点城市。"  
    101.             },  
    102.             {  
    103.                 "id":"D","name":"成都","x":104.035508,"y":30.714179,  
    104.                 "desc":"成都,简称蓉,四川省省会,1993年被国务院确定为西南地区的科技、商贸、金融中心和交通、通讯枢纽。"  
    105.             }  
    106.         ];  
    107.         require([  
    108.                     "esri/map",  
    109.                     "esri/layers/ArcGISTiledMapServiceLayer",  
    110.                     "esri/geometry/Point",  
    111.                     "esri/layers/GraphicsLayer",  
    112.                     "esri/graphic",  
    113.                     "dojo/_base/Color",  
    114.                     "dojo/domReady!"],  
    115.                 function(Map,  
    116.                          Tiled,  
    117.                          Point,  
    118.                          GraphicsLayer,  
    119.                          Graphic,  
    120.                          Color)  
    121.                 {  
    122.                     map = new Map("map",{logo:false});  
    123.                     var tiled = new Tiled("http://localhost:6080/arcgis/rest/services/china/MapServer",{"id":"tiled"});  
    124.                     map.addLayer(tiled);  
    125.                     var mapCenter = new Point(103.847, 36.0473, {"wkid":4326});  
    126.                     map.centerAndZoom(mapCenter,0);  
    127.                     var gLyr = new GraphicsLayer({"id":"gLyr"});  
    128.                     map.addLayer(gLyr);  
    129.                     var gLyrHover = new GraphicsLayer({"id":"gLyrHover"});  
    130.                     map.addLayer(gLyrHover);  
    131.                     var gLyrLbl = new GraphicsLayer({"id":"gLyrLbl"});  
    132.                     map.addLayer(gLyrLbl);  
    133.                     map.on("load",function(){  
    134.                         $("#search").show();  
    135.                         for(var i=0;i<data.length;i++){  
    136.                             var li = $("<li />").addClass("search-box-result-item").appendTo($("#result"));  
    137.                             var name = $("<div />").addClass("search-name").html(data[i].name);  
    138.                             var icon = $("<div />").addClass("search-icon")  
    139.                                     .attr("id","icon"+data[i].id)  
    140.                                     .append("<div class='search-text'>"+data[i].id+"</div>");  
    141.                             var title = $("<div />").addClass("search-name-title")  
    142.                                     .append(name).append(icon).appendTo(li)  
    143.                                     .data("attr",data[i]);  
    144.                             var desc = $("<div />").addClass("search-detail").html(data[i].desc).appendTo(li);  
    145.                             var more = $("<div />").addClass("detail").appendTo(li).html(">>详细");  
    146.                             title.on("mouseover",function(){  
    147.                                 var attr = $(this).data("attr");  
    148.                                 $("#icon"+attr.id).css("background","url('images/blue.png')");  
    149.                                 var pt=new Point(attr.x,attr.y,{"wkid":4326});  
    150.                                 var pms = new esri.symbol.PictureMarkerSymbol("images/blue.png",24,26)  
    151.                                 var gImg = new Graphic(pt,pms);  
    152.                                 gLyrHover.add(gImg);  
    153.                             });  
    154.                             title.on("mouseout",function(){  
    155.                                 var attr = $(this).data("attr");  
    156.                                 $("#icon"+attr.id).css("background","url('images/red.png')");  
    157.                                 gLyrHover.clear();  
    158.                             });  
    159.                             title.on("click",function(){  
    160.                                 var attr = $(this).data("attr");  
    161.                                 showCity(attr);  
    162.                             });  
    163.                             var pt=new Point(data[i].x,data[i].y,{"wkid":4326});  
    164.                             var pms = new esri.symbol.PictureMarkerSymbol("images/red.png",24,26)  
    165.                             var gImg = new Graphic(pt,pms,data[i]);  
    166.                             gLyr.add(gImg);  
    167.                             var font  = new esri.symbol.Font();  
    168.                             font.setSize("10pt");  
    169.                             font.setFamily("微软雅黑");  
    170.                             var text = new esri.symbol.TextSymbol(data[i].id);  
    171.                             text.setOffset(0,-2);  
    172.                             text.setFont(font);  
    173.                             text.setColor(new dojo.Color([255,255,255,100]));  
    174.                             var gLbl = new esri.Graphic(pt,text,data[i]);  
    175.                             gLyrLbl.add(gLbl);  
    176.                         }  
    177.                         gLyr.on("mouse-over",function(e){  
    178.                             map.setMapCursor("pointer");  
    179.                             var sms = e.graphic.symbol;  
    180.                             sms.url = "images/blue.png";  
    181.                             gLyr.redraw();  
    182.                             $("#icon"+e.graphic.attributes.id).css("background","url('images/blue.png')");  
    183.                         });  
    184.                         gLyr.on("mouse-out",function(e){  
    185.                             map.setMapCursor("default");  
    186.                             var sms = e.graphic.symbol;  
    187.                             sms.url = "images/red.png";  
    188.                             gLyr.redraw();  
    189.                             $("#icon"+e.graphic.attributes.id).css("background","url('images/red.png')");  
    190.                         });  
    191.                         gLyr.on("click",function(e){  
    192.                             var attr = e.graphic.attributes;  
    193.                             showCity(attr);  
    194.                         });  
    195.                     });  
    196.   
    197.                     function showCity(attr){  
    198.                         var pt=new Point(attr.x,attr.y,{"wkid":4326});  
    199.                         map.infoWindow.setTitle(attr.name);  
    200.                         map.infoWindow.setContent(attr.desc);  
    201.                         map.infoWindow.resize(200,80);  
    202.                         map.infoWindow.show(pt);  
    203.                         map.centerAndZoom(pt,0);  
    204.                     }  
    205.                 });  
    206.     </script>  
    207. </head>  
    208. <body>  
    209. <div id="search" class="search-box" style="display: none;">  
    210.     <div class="search-box-title">查询结果</div>  
    211.     <ul class="search-box-result" id="result">  
    212.     </ul>  
    213. </div>  
    214. <div id="map">  
    215. </div>  
    216. </body>  
    217. </html>  
 
posted @ 2017-06-09 15:52  疯子110  阅读(834)  评论(0)    收藏  举报