Arcgis for js实现北京地铁的展示

概述:

通过获取百度地铁图数据,将北京市地铁图在Arcgis for js中实现展示。

效果:

初始化效果

放大后

鼠标经过线路高亮并显示名称

点击显示站点信息

实现:

1、获取地铁数据

a、打开百度地铁图

在chrom中输入地址http://map.baidu.com/subways/index.html?c=beijing

b、获取网络资源

按下f12,切换到NetWork面板,刷新页面,会出现如下所示:

在新的面板中打开对应的链接,即为北京地铁的原始数据,格式为xml

c、将xml转换为json

利用网络xml2json转换工具将xml转换为json,工具地址为http://www.bejson.com/go.php?u=http://www.bejson.com/xml2json/

2、将站点和线路添加到地图上

将上述转换后的json定义为一个变量,单独存储为一个js文件。在map的load事件完成后将站点和线路添加到地图,代码如下:

            map.on("load",function(){
                var legendDiv = $("<div />").addClass("legend-box").appendTo($("#map"));
                var titleDiv = $("<div />").addClass("tr-title").appendTo(legendDiv).html("图例");
                var legendTab = $("<table />").appendTo(legendDiv).hide();

                titleDiv.on("click",function(){
                    if(!legendTab.is(":hidden")){
                        legendTab.hide();
                    }
                    else{
                        legendTab.show();
                    }
                });
                var mulPt = new Multipoint();
                var subLines = subData.sw.l;
                for(var i= 0,ll =subLines.length;i<ll;i++){
                    var sms = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE,
                            8,
                            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                                    new Color(color[i]),
                                    3
                            ),
                            new Color([255,255,255])
                    );
                    var pms = PictureMarkerSymbol('chg_stat.png', 12, 12);

                    var sls = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                            new Color(color[i]),
                            4
                    );

                    var trItem = $("<tr />").appendTo(legendTab);
                    var legendColor = $("<div />").addClass("legend-item-color").css("background",color[i]);
                    var legendText = $("<div />").addClass("legend-item-text").html(subLines[i].lid);
                    var tdColor = $("<td />").appendTo(trItem).append(legendColor);
                    var tdText = $("<td />").appendTo(trItem).append(legendText);

                    var stops = subLines[i].p;
                    var line = new Array();
                    for(var j= 0,sl =stops.length;j<sl;j++){
                        var scrPt = new esri.geometry.ScreenPoint(stops[j].x,stops[j].y);
                        var mapPt = map.toMap(scrPt);
                        mulPt.addPoint(mapPt);
                        line.push([mapPt.x,mapPt.y]);

                        if(stops[j].st==="true"){
                            var smb = sms;
                            var font  = new esri.symbol.Font();
                            font.setSize("10px");
                            font.setFamily("微软雅黑");

                            if(stops[j].ex==="true"){
                                smb = pms;
                                font.setWeight("bold");
                            }

                            var gStop = new Graphic(mapPt,smb,stops[j]);
                            subwayStop.add(gStop);
                            var ts = new esri.symbol.TextSymbol(stops[j].lb);
                            ts.setFont(font);
                            ts.setOffset(0,5);
                            var gTxtStop = new Graphic(mapPt,ts);
                            subwayLabel.add(gTxtStop);
                        }
                    }
                    if(subLines[i].loop==="true"){
                        var scrPt = new esri.geometry.ScreenPoint(stops[0].x,stops[0].y);
                        var mapPt = map.toMap(scrPt);
                        line.push([mapPt.x,mapPt.y]);
                    }
                    var subLine = new Polyline({"paths":[line],
                        "spatialReference":{"wkid":4326}});
                    var gLine = new Graphic(subLine,sls,subLines[i]);
                    subwayLine.add(gLine);
                }
                var trItem = $("<tr />").appendTo(legendTab);
                var legendColor = $("<img />").attr("src","chg_stat.png").attr("width","15px").attr("height","15px");
                var legendText = $("<div />").addClass("legend-item-text").html("换乘站点");
                var tdColor = $("<td />").appendTo(trItem).append(legendColor);
                var tdText = $("<td />").appendTo(trItem).append(legendText);
                map.setExtent(mulPt.getExtent().expand(1.5));
            });

3、添加地图事件

添加鼠标经过和单机等事件,代码如下:

            map.on("zoom-end",function(){
                var level =map.getLevel();
                if(level>0){
                    subwayLabel.show();
                }
                else{
                    subwayLabel.hide();
                }
            });

            subwayStop.on("mouse-over",function(evt){
                var scrPt = map.toScreen(evt.graphic.geometry);
                var statName = evt.graphic.attributes.lb;
                var stopName = $("<div />").attr("id","stopName")
                        .addClass("stat-name-box")
                        .html(statName)
                        .css("left",scrPt.x)
                        .css("top",scrPt.y+20)
                        .appendTo($("#map"));
                map.setMapCursor("pointer");
            });
            subwayStop.on("mouse-out",function(evt){
                $("#stopName").remove();
                map.setMapCursor("default");
            });
            subwayStop.on("click",function(evt){
                var stopName= evt.graphic.attributes.lb;
                var lineName = evt.graphic.attributes.ln;
                lineName = lineName.split(",");
                var title = "<span style='font-weight: bold;'>"+stopName+"</span>";
                var content="<span style='font-weight: bold;'>可乘坐</span>:<br />";
                for(var i=0;i<lineName.length;i++){
                    var pathName = lineName[i].split("|")[1];
                    content = content+pathName+"<br />";
                }
                map.infoWindow.setTitle(title);
                map.infoWindow.setContent(content);
                map.infoWindow.show(evt.graphic.geometry);
                $(".maximize").hide();
            });

            subwayLine.on("mouse-over",function(evt){
                var scrPt = map.toScreen(evt.mapPoint);
                var lineTitle = evt.graphic.attributes.lb;
                var lineName = $("<div />").attr("id","lineName")
                        .addClass("stat-name-box")
                        .html(lineTitle)
                        .css("left",scrPt.x)
                        .css("top",scrPt.y+20)
                        .appendTo($("#map"));
                map.setMapCursor("pointer");
                evt.graphic.symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                        new Color("#00ffff"),
                        4
                );
                subwayLine.redraw();
            });
            subwayLine.on("mouse-out",function(evt){
                $("#lineName").remove();
                map.setMapCursor("default");
                evt.graphic.symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                        new Color(color[evt.graphic.attributes.i-1]),
                        4
                );
                subwayLine.redraw();
            });
            subwayLine.on("click",function(evt){
                var geometry = evt.graphic.geometry;

                var stops = evt.graphic.attributes.p;
                var startStop =stops[0],endStop=stops[stops.length-1];

                var scrStart = new esri.geometry.ScreenPoint(startStop.x,startStop.y);
                var scrEnd = new esri.geometry.ScreenPoint(endStop.x,endStop.y);
                var startPt = map.toMap(scrStart);
                var endPt = map.toMap(scrEnd);

                var startName = startStop.lb, endName = endStop.lb;
                var font  = new esri.symbol.Font();
                font.setSize("8pt");
                font.setFamily("微软雅黑");
                var startText = new esri.symbol.TextSymbol(startName),endText = new esri.symbol.TextSymbol(endName);
                startText.setFont(font), endText.setFont(font);
                startText.setColor(new dojo.Color([0,0,0,100])),endText.setColor(new dojo.Color([0,0,0,100]));
                subwayLabel.add(new Graphic(startPt, startText));
                subwayLabel.add(new Graphic(endPt, endText));

                map.setExtent(geometry.getExtent().expand(1.8));
            });

到此,基本功能已完成,完整代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
    <title>北京地铁</title>
    <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.9/3.9/js/esri/css/esri.css">
    <style>
        html, body, #map{
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
        .stat-name-box{
            position: absolute;
            left: 0px;
            top: 0px;
            z-index: 10;
            border: rgb(139, 164, 220) 1px solid;
            width: auto;
            height: auto;
            font:normal 11px "宋体",Arial;
            color: rgb(0, 0, 0);
            background: rgb(255, 255, 255);
            box-shadow: rgba(0, 0, 0, 0.34902) 2px 2px 3px;
            border-radius: 3px;
            padding: 3px 5px;
        }
        .legend-box{
            position: absolute;
            top: 10px;
            right: 10px;
            z-index: 10;
            border: rgb(139, 164, 220) 1px solid;
            width: 130px;
            height: auto;
            color: rgb(0, 0, 0);
            background: rgb(255, 255, 255);
            box-shadow: rgba(0, 0, 0, 0.34902) 2px 2px 3px;
            border-radius: 3px;
        }
        tr{
            padding: 2px 8px;
        }
        .tr-title{
            text-align: center;
            font:bold 13px "宋体",Arial;
            padding: 3px 0px;
            background: rgb(139, 164, 220);
            color: #ffffff;
            border-radius: 3px;
        }
        .tr-title:hover{
            cursor: pointer;
        }
        .legend-item-color{
            width: 10px;
            height: 10px;
            float: left;
            background: rgb(139, 164, 220);
            margin-top: 5px;
        }
        .legend-item-text{
            font:normal 8px "宋体",Arial;
            padding: 3px 5px;
            float: left;
        }
    </style>
    <script src="http://localhost/arcgis_js_api/library/3.9/3.9/init.js"></script>
    <script src="bj_sub_data.js"></script>
    <script src="jquery-1.8.3.js"></script>
    <script>
        var map;
        var color = ["#d92528","#166db2","#02a9bb","#b51c84","#bd9001","#ff9000","#17997d","#90ba78","#0696cb",
            "#ffd600","#f55585","#714286","#00a9bc","#ca1e72","#ed9aac","#cd2c24","#eb5e19","#ac96bd"
        ];
        require(["esri/map",
                    "esri/layers/ArcGISTiledMapServiceLayer",
                    "esri/layers/GraphicsLayer",
                    "esri/graphic",
                    "esri/geometry/Point",
                    "esri/geometry/Multipoint",
                    "esri/geometry/Polyline",
                    "esri/SpatialReference",
                    "esri/symbols/SimpleMarkerSymbol",
                    "esri/symbols/PictureMarkerSymbol",
                    "esri/symbols/SimpleLineSymbol", //简单线符号
                    "dojo/_base/Color",
                    "dojo/domReady!"
        ],
        function(Map,
                 Tiled,
                 GraphicsLayer,
                 Graphic,
                 Point,
                 Multipoint,
                 Polyline,
                 SpatialReference,
                 SimpleMarkerSymbol,
                 PictureMarkerSymbol,
                 SimpleLineSymbol,
                 Color) {
            map = new Map("map",{
                logo:false
            });
            var tiled = new Tiled("http://localhost:6080/arcgis/rest/services/BJ/MapServer",{"id":"tiled"});
            map.addLayer(tiled);
            tiled.hide();
            var pt = new Point(116.88590597052463,40.37521501133166);
            map.centerAndZoom(pt, 1);

            var subwayStop = new GraphicsLayer({"id":"subwayStop"});
            var subwayLine = new GraphicsLayer({"id":"subwayLine"});
            var subwayLabel = new GraphicsLayer({"id":"subwayLabel"});
            map.addLayer(subwayLine);
            map.addLayer(subwayStop);
            map.addLayer(subwayLabel);
            subwayLabel.hide();

            map.on("zoom-end",function(){
                var level =map.getLevel();
                if(level>0){
                    subwayLabel.show();
                }
                else{
                    subwayLabel.hide();
                }
            });

            subwayStop.on("mouse-over",function(evt){
                var scrPt = map.toScreen(evt.graphic.geometry);
                var statName = evt.graphic.attributes.lb;
                var stopName = $("<div />").attr("id","stopName")
                        .addClass("stat-name-box")
                        .html(statName)
                        .css("left",scrPt.x)
                        .css("top",scrPt.y+20)
                        .appendTo($("#map"));
                map.setMapCursor("pointer");
            });
            subwayStop.on("mouse-out",function(evt){
                $("#stopName").remove();
                map.setMapCursor("default");
            });
            subwayStop.on("click",function(evt){
                var stopName= evt.graphic.attributes.lb;
                var lineName = evt.graphic.attributes.ln;
                lineName = lineName.split(",");
                var title = "<span style='font-weight: bold;'>"+stopName+"</span>";
                var content="<span style='font-weight: bold;'>可乘坐</span>:<br />";
                for(var i=0;i<lineName.length;i++){
                    var pathName = lineName[i].split("|")[1];
                    content = content+pathName+"<br />";
                }
                map.infoWindow.setTitle(title);
                map.infoWindow.setContent(content);
                map.infoWindow.show(evt.graphic.geometry);
                $(".maximize").hide();
            });

            subwayLine.on("mouse-over",function(evt){
                var scrPt = map.toScreen(evt.mapPoint);
                var lineTitle = evt.graphic.attributes.lb;
                var lineName = $("<div />").attr("id","lineName")
                        .addClass("stat-name-box")
                        .html(lineTitle)
                        .css("left",scrPt.x)
                        .css("top",scrPt.y+20)
                        .appendTo($("#map"));
                map.setMapCursor("pointer");
                evt.graphic.symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                        new Color("#00ffff"),
                        4
                );
                subwayLine.redraw();
            });
            subwayLine.on("mouse-out",function(evt){
                $("#lineName").remove();
                map.setMapCursor("default");
                evt.graphic.symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                        new Color(color[evt.graphic.attributes.i-1]),
                        4
                );
                subwayLine.redraw();
            });
            subwayLine.on("click",function(evt){
                var geometry = evt.graphic.geometry;

                var stops = evt.graphic.attributes.p;
                var startStop =stops[0],endStop=stops[stops.length-1];

                var scrStart = new esri.geometry.ScreenPoint(startStop.x,startStop.y);
                var scrEnd = new esri.geometry.ScreenPoint(endStop.x,endStop.y);
                var startPt = map.toMap(scrStart);
                var endPt = map.toMap(scrEnd);

                var startName = startStop.lb, endName = endStop.lb;
                var font  = new esri.symbol.Font();
                font.setSize("8pt");
                font.setFamily("微软雅黑");
                var startText = new esri.symbol.TextSymbol(startName),endText = new esri.symbol.TextSymbol(endName);
                startText.setFont(font), endText.setFont(font);
                startText.setColor(new dojo.Color([0,0,0,100])),endText.setColor(new dojo.Color([0,0,0,100]));
                subwayLabel.add(new Graphic(startPt, startText));
                subwayLabel.add(new Graphic(endPt, endText));

                map.setExtent(geometry.getExtent().expand(1.8));
            });

            map.on("load",function(){
                var legendDiv = $("<div />").addClass("legend-box").appendTo($("#map"));
                var titleDiv = $("<div />").addClass("tr-title").appendTo(legendDiv).html("图例");
                var legendTab = $("<table />").appendTo(legendDiv).hide();

                titleDiv.on("click",function(){
                    if(!legendTab.is(":hidden")){
                        legendTab.hide();
                    }
                    else{
                        legendTab.show();
                    }
                });
                var mulPt = new Multipoint();
                var subLines = subData.sw.l;
                for(var i= 0,ll =subLines.length;i<ll;i++){
                    var sms = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE,
                            8,
                            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                                    new Color(color[i]),
                                    3
                            ),
                            new Color([255,255,255])
                    );
                    var pms = PictureMarkerSymbol('chg_stat.png', 12, 12);

                    var sls = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                            new Color(color[i]),
                            4
                    );

                    var trItem = $("<tr />").appendTo(legendTab);
                    var legendColor = $("<div />").addClass("legend-item-color").css("background",color[i]);
                    var legendText = $("<div />").addClass("legend-item-text").html(subLines[i].lid);
                    var tdColor = $("<td />").appendTo(trItem).append(legendColor);
                    var tdText = $("<td />").appendTo(trItem).append(legendText);

                    var stops = subLines[i].p;
                    var line = new Array();
                    for(var j= 0,sl =stops.length;j<sl;j++){
                        var scrPt = new esri.geometry.ScreenPoint(stops[j].x,stops[j].y);
                        var mapPt = map.toMap(scrPt);
                        mulPt.addPoint(mapPt);
                        line.push([mapPt.x,mapPt.y]);

                        if(stops[j].st==="true"){
                            var smb = sms;
                            var font  = new esri.symbol.Font();
                            font.setSize("10px");
                            font.setFamily("微软雅黑");

                            if(stops[j].ex==="true"){
                                smb = pms;
                                font.setWeight("bold");
                            }

                            var gStop = new Graphic(mapPt,smb,stops[j]);
                            subwayStop.add(gStop);
                            var ts = new esri.symbol.TextSymbol(stops[j].lb);
                            ts.setFont(font);
                            ts.setOffset(0,5);
                            var gTxtStop = new Graphic(mapPt,ts);
                            subwayLabel.add(gTxtStop);
                        }
                    }
                    if(subLines[i].loop==="true"){
                        var scrPt = new esri.geometry.ScreenPoint(stops[0].x,stops[0].y);
                        var mapPt = map.toMap(scrPt);
                        line.push([mapPt.x,mapPt.y]);
                    }
                    var subLine = new Polyline({"paths":[line],
                        "spatialReference":{"wkid":4326}});
                    var gLine = new Graphic(subLine,sls,subLines[i]);
                    subwayLine.add(gLine);
                }
                var trItem = $("<tr />").appendTo(legendTab);
                var legendColor = $("<img />").attr("src","chg_stat.png").attr("width","15px").attr("height","15px");
                var legendText = $("<div />").addClass("legend-item-text").html("换乘站点");
                var tdColor = $("<td />").appendTo(trItem).append(legendColor);
                var tdText = $("<td />").appendTo(trItem).append(legendText);
                map.setExtent(mulPt.getExtent().expand(1.5));
            });
        });
    </script>
</head>
<body>
<div id="map">
</div>
</body>
</html>

时间: 2024-10-25 12:23:31

Arcgis for js实现北京地铁的展示的相关文章

Arcgis for js,Openlayers中加载GeoJSON

概述: 在前文中,讲述了在JAVA环境下如何将shp转换为GeoJSON,在本文,分别讲述在Arcgis for js,Openlayers2和Openlayers3中加载展示GeoJSON. 实现: 1.Openlayers2中加载GeoJSON 在OL2中,可以直接调用OL2的借口实现GeoJSON的加载,代码示例: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT

Arcgis for js加载百度地图

概述: 在前面的文章里提到了Arcgis for js加载天地图,在本节,继续讲解如何在Arcgis for js中加载百度地图. 效果: 地图 影像-无标注 影像-有标注 实现: 第一,在此感谢http://www.cnblogs.com/chenyuming507950417/p/3343729.html这篇文章的主人,给了我很大的启发与帮助,因为一直在找相关的参数,都不对,只有这个参数是没有问题的. 第二,百度地图可调用的有地图切片,影像切片,以及道路等POI切片,我将之用TiledMap

Arcgis for Js实现graphiclayer的空间查询

本节讲的是Arcgis for Js的针对graphiclayer的空间查询,内容非常easy.代码例如以下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="initial-

基于Arcgis for Js的web GIS数据在线采集简介

在前一篇博文"Arcgis for js之WKT和geometry转换"中实现了wkt和geometry之间的相互转化,博文原文地址为:http://blog.csdn.net/gisshixisheng/article/details/44057453.在本节,接上文,简单讲述基于Arcgis for Js的web GIS数据在线采集. 实现数据的在线采集,最主要的是数据的存储,即将采集到的数据的geometry对象保存下来,并后续可以转换为shp数据.在本文,我的处理方式为将前段绘

Arcgis for Js之鼠标经过显示对象名的实现

在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来说是很实用的,本文讲述在Arcgis for Js中,用两种不同的方式来实现该效果. 为了有个直观的概念,先给大家看看实现后的效果: 百度地图的效果 效果1 效果2 直观的看到了效果,下面说说在Arcgis for Js中实现的两种方式.在实现给效果的时候,有layer的两个事件,mouse-over和mouse-out事件,鼠标经过显示对象名称,鼠标移除清除显示. 1.通过TextSymbol和GraphicMar

Arcgis for JS之Cluster聚类分析的实现

原文:Arcgis for JS之Cluster聚类分析的实现 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来 的,包含XY坐标信息的,通过graphic和graphiclayer 的方式添加到地图上,其中有一个对象的数量很多,上万了吧,通过上述的方式无法在地图上进行展示,就想到了聚类,当时由于技术和时间的关系,没有实现,最 近,稍微有点先下时间,就又想起这事,继续研究,终于,皇天不负有心人,出来了,出来的第一时间写出来,以便大家使用. 首先,看看实现后的效果: 初始化

北京地铁和广州地铁之感想

在北京生活了八年,前年回到广州,日常出行就靠地铁公交这些公共交通工具.对比帝都和羊城的地铁,各有各的特点.帝都地铁历史更悠久,线路更多:由于北京道路一般是横平竖直,地铁站命名更规律,十字路口的东北出入口为A,然后按顺时针分布依次为ABCD.羊城地铁最早的一号线建于九十年代,其他比起帝都地铁更新更先进,建设之初就通达机场各大火车站,更便捷.换成也比北京地铁方便,一般上下楼梯就可以换乘,不像北京地铁要绕很远才能换乘. 就日常使用来说,相对而言帝都地铁细节设计实用性更高一些,更人性化.例如:1.帝都地

arcgis for js开发之路径分析

arcgis for js开发之路径分析 //方法封装 function routeplan(x1, x2, y1, y2, barrierPathArray, isDraw, callback) { require([ "esri/symbol/SimpleLineSymbol", "esri/Color", "esri/tasks/RouteTask", "esri/tasks/FreatureSet", "es

arcgis server 10.1 发布动态图层展示海量及频繁更新的数据步骤

转自原文 arcgis server 10.1 发布动态图层展示海量及频繁更新的数据步骤 Arcgis server  发布动态图层及调用动态图层 做这个动态图层功能的原由是 有一个30万的数据需要通过arcgis GP工具转成shp然后渲染加载进地图,原来的做法是遍历生成shp面要素,读取面的环状点然后转成json,这个也能实现结果但是时间较长,与预期的时间差别较多,在公司内部讨论时有提到这个动态图层可以解决海量频繁数据的替换,要是每个都发一个图层服务这样不可取.故此讨论后研究这个神奇的功能.