search
HomeWeb Front-endH5 Tutorial基于HTML5制作在线上海地铁图


       某市政项目要用到地铁图,展示上海地铁站点以及相关信息流,尝试使用HTML5技术来实现,自己折腾有点慢,找到一个HTML5的图形组件-Qunee for HTML5,简单学习一下,就可以很好的解决这类需求,做出优美的展现,下面以上海2012地铁图为例,效果如下:

基于HTML5制作在线上海地铁图


      示例讲解

       首先需要解决数据问题,可以从维基百科或者上海地铁官网中获取,不过也免不了人工,要达到良好的显示效果,需要不只要记录站点的位置,还需要设置文本标签的理想位置,有时为了避免文字叠加,需要设置旋转角度……总之事在人为,想想办法,最终解决了数据问题,再加上Qunee图形组件的强大展示效果,做出来可以交互的在线地铁图

       数据格式

       采用JSON格式数据,分三种类型:文本标签、站点、地铁线

       总的结构如下:

  1. {
  2. "labels" : [  ... ],
  3. "stations" : [ ... ],
  4. "lines" : [ ... ]
  5. }
复制代码

       文本标签数据

       包含坐标和文字信息,如果文字需要旋转,则会增加”rotate”属性,下面是“莘庄”文本标签信息
  1. {
  2. "text" : "莘庄",
  3. "x" : 883.591,
  4. "y" : 1625.695
  5. }
复制代码

       文字与节点旋转效果

基于HTML5制作在线上海地铁图


       站点数据

       包含坐标、旋转角度以及编号信息,下面是“莘庄”站的信息

  1. {
  2. "id" : 5,
  3. "x" : 869.8513512641732,
  4. "y" : 1597.6559686949402,
  5. "rotate" : 0.7853981633974483
  6. }
复制代码

       地铁线数据

       包含名称,颜色,以及经过的站点编号

  1. {
  2. "name" : "1",
  3. "color" : "#e52035",
  4. "stations" : [64, 70, 67, 71, 72, 65, 69, 73, 66, 68, 63, 62, 22, 61, 60, {"id": 21, "yOffset": 0.5}, 59, {"id": 18, "yOffset": -0.5}, 17, 58, 14, 7, 57, 6,
  5. 56, 44, 47, 5]
  6. }
复制代码

       对于特殊情况,比如两条地铁线共用一条线路的情况,会出现两条线重合,为了避免这种情况,还可以指定站点横向偏移量,比如上面一号线中的如下数据

  1. {"id": 21, "yOffset": 0.5}
复制代码

       因为上海地铁三号线与四号线共用线路较多,所以这种处理更加明显

       三号线数据

  1. {
  2. "name" : "3",
  3. "color" : "#f9d300",
  4. "stations" : [6, 95, 96, 97, {"id":12,"yOffset":0.5}, {"id":11,"yOffset":0.5}, {"id":8,"yOffset":0.5}, {"id":9,"yOffset":0.5},
  5. {"id":10,"yOffset":0.5}, {"id":25,"yOffset":0.5}, {"id":26,"yOffset":0.5}, {"id":238,"yOffset":0.5}, {"id":22,"yOffset":-0.5}, {"id":27,"yOffset":-0.5},
  6. 98, 99, 100, 101, 104, 105, 107, 108, 109, 106, 110, 111]
  7. }
复制代码

       地铁共线效果

基于HTML5制作在线上海地铁图

       创建图元

       数据需要转换成qunee图元对象,三种类型分别对应三个创建函数

       创建文本标签
  1. function createText(name, x, y, rotate){
  2.     var text = graph.createNode(name, x, y);
  3.     if(rotate){
  4.         text.rotate = rotate;
  5.     }
  6.     text.zIndex = 20;
  7.     text.image = null;
  8.     text.setStyle(Q.Styles.BACKGROUND_COLOR, Q.toColor(0x88FFFFFF));
  9.     text.setStyle(Q.Styles.LABEL_ANCHOR_POSITION, Q.Position.LEFT_BOTTOM);
  10.     text.setStyle(Q.Styles.LABEL_POSITION, Q.Position.CENTER_MIDDLE);
  11.     text.setStyle(Q.Styles.LABEL_PADDING, PADDING);
  12.     return text;
  13. }
复制代码

       创建站点

  1. function createStation(station){
  2.     var node = graph.createNode(null/**station.name*/, station.x, station.y);
  3.     node.stationId = station.id;
  4.     node.setStyle(Q.Styles.LABEL_FONT_SIZE, 10);
  5.     node.setStyle(Q.Styles.LABEL_ANCHOR_POSITION, Q.Position.CENTER_MIDDLE);
  6.     node.setStyle(Q.Styles.LABEL_POSITION, Q.Position.CENTER_MIDDLE);
  7.     node.zIndex = 10;
  8.     if(station.rotate){
  9.         node.image = roundRect;
  10.         node.rotate = station.rotate;
  11.     }else{
  12.         node.image = circle;
  13.     }
  14.     node.setStyle(Q.Styles.SHAPE_FILL_COLOR, "#FFF");
  15.     node.setStyle(Q.Styles.SHAPE_STROKE_STYLE, "#000");
  16.     return node;
  17. }
复制代码

       创建地铁线

       createLine(…)函数用于创建地铁线,使用了节点类型图元,并设置节点主体为路径,函数updateLine(…)用于从站点信息自动生成线路路径

  1. function createLine(line){
  2.     var stations = line.stations;

  3.     var node = graph.createNode(line.name);
  4.     node.stations = stations;
  5.     node.movable = false;
  6.     node.setStyle(Q.Styles.LABEL_FONT_SIZE, 50);
  7.     node.setStyle(Q.Styles.LABEL_COLOR, line.color);
  8.     node.setStyle(Q.Styles.LABEL_ANCHOR_POSITION, Q.Position.LEFT_BOTTOM);
  9.     node.setStyle(Q.Styles.LABEL_POSITION, Q.Position.LEFT_TOP);
  10.     node.setStyle(Q.Styles.LAYOUT_BY_PATH, true);
  11.     node.anchorPosition = null;
  12.     node.setStyle(Q.Styles.SHAPE_STROKE, size);
  13.     node.setStyle(Q.Styles.SHAPE_STROKE_STYLE, line.color);

  14.     updateLine(node, true);
  15.     return node;
  16. }
复制代码
  1. function updateLine(line, addListener){
  2.     var path = new Q.Path();
  3.     line.image = path;

  4.     var stations = line.stations;
  5.     var first = true;
  6.     Q.forEach(stations, function(s){
  7.         var station = getStation(s.id || s);
  8.         if(!station){
  9.             return;
  10.         }
  11.         if(addListener){
  12.             addLocationChangeListener(station.stationId, line);
  13.         }
  14.         var location = station.location;
  15.         var x = location.x, y = location.y;
  16.         if(s.yOffset){
  17.             var offset = s.yOffset * size;
  18.             var rotate = station.rotate || 0;
  19.             var sin = Math.sin(rotate);
  20.             var cos = Math.cos(rotate);
  21.             x += cos * offset;
  22.             y += sin * offset;
  23.         }
  24.         if(first){
  25.             first = false;
  26.             path.moveTo(x, y);
  27.         }else{
  28.             path.lineTo(x, y);
  29.         }
  30.     })
  31. }
复制代码

        交互处理

        增加交互处理,监听站点拖动事件,保持地铁路线跟随站点位置变化

  1. graph.interactionDispatcher.addListener(function(evt){
  2.     if(evt.kind != Q.InteractionEvent.ELEMENT_MOVING){
  3.         return;
  4.     }
  5.     var datas = evt.datas;

  6.     Q.forEach(datas, function(data){
  7.         if(!data.stationId){
  8.             return;
  9.         }
  10.         var listeners = stationLocationChangeListeners[data.stationId];
  11.         if(listeners){
  12.             for(var l in listeners){
  13.                 updateLine(listeners[l]);
  14.             }
  15.         }
  16.     });
  17. });
复制代码

在线示例

来源:http://blog.chinaunix.net/uid-29563534-id-4171575.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft