软件工程课堂四(合作开发项目-地铁线路查询)
2019-04-03 23:05 一念永恒s 阅读(319) 评论(0) 编辑 收藏 举报本次合作伙伴,尹坤,
本次项目花费时间10小时,
项目一开始是自己建立数据库
数据库结构:
1 DROP TABLE IF EXISTS `subway`; 2 CREATE TABLE `subway` ( 3 `id` int(11) NOT NULL AUTO_INCREMENT, 4 `stop` varchar(255) DEFAULT NULL, 5 `route` int(255) DEFAULT NULL, 6 `position` int(255) DEFAULT NULL, 7 `exchange` int(255) DEFAULT NULL, 8 PRIMARY KEY (`id`) 9 ) ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8;
数据库数据:
1 SET FOREIGN_KEY_CHECKS=0; 2 3 -- ---------------------------- 4 -- Table structure for subway 5 -- ---------------------------- 6 DROP TABLE IF EXISTS `subway`; 7 CREATE TABLE `subway` ( 8 `id` int(11) NOT NULL AUTO_INCREMENT, 9 `stop` varchar(255) DEFAULT NULL, 10 `route` int(255) DEFAULT NULL, 11 `position` int(255) DEFAULT NULL, 12 `exchange` int(255) DEFAULT NULL, 13 PRIMARY KEY (`id`) 14 ) ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8; 15 16 -- ---------------------------- 17 -- Records of subway 18 -- ---------------------------- 19 INSERT INTO `subway` VALUES ('1', '树路', '1', '1', null); 20 INSERT INTO `subway` VALUES ('2', '侯家庄', '1', '2', null); 21 INSERT INTO `subway` VALUES ('3', '罗家庄北', '1', '3', null); 22 INSERT INTO `subway` VALUES ('4', '罗家庄', '1', '4', '2'); 23 INSERT INTO `subway` VALUES ('5', '罗家庄南', '1', '5', null); 24 INSERT INTO `subway` VALUES ('6', '行政中心', '1', '6', null); 25 INSERT INTO `subway` VALUES ('7', '临济', '1', '7', null); 26 INSERT INTO `subway` VALUES ('8', '西庄', '1', '8', null); 27 INSERT INTO `subway` VALUES ('9', '东兆通', '1', '9', null); 28 INSERT INTO `subway` VALUES ('10', '东杜庄', '1', '10', '5'); 29 INSERT INTO `subway` VALUES ('11', '石家庄东', '1', '11', null); 30 INSERT INTO `subway` VALUES ('12', '卓达星辰', '1', '12', null); 31 INSERT INTO `subway` VALUES ('13', '海世界', '1', '13', '6'); 32 INSERT INTO `subway` VALUES ('14', '白佛口', '1', '14', null); 33 INSERT INTO `subway` VALUES ('15', '谈固', '1', '15', null); 34 INSERT INTO `subway` VALUES ('16', '艺术学校', '1', '16', null); 35 INSERT INTO `subway` VALUES ('17', '建百大楼', '1', '17', '4'); 36 INSERT INTO `subway` VALUES ('18', '河北医大', '1', '18', null); 37 INSERT INTO `subway` VALUES ('19', '省博物馆', '1', '19', null); 38 INSERT INTO `subway` VALUES ('20', '北国商城', '1', '20', '2'); 39 INSERT INTO `subway` VALUES ('21', '市招待所', '1', '21', null); 40 INSERT INTO `subway` VALUES ('22', '大石桥', '1', '22', null); 41 INSERT INTO `subway` VALUES ('23', '新百广场', '1', '23', '3'); 42 INSERT INTO `subway` VALUES ('24', '烈士陵园', '1', '24', null); 43 INSERT INTO `subway` VALUES ('25', '和平医院', '1', '25', '5'); 44 INSERT INTO `subway` VALUES ('26', '军医医院', '1', '26', null); 45 INSERT INTO `subway` VALUES ('27', '法医医院', '1', '27', null); 46 INSERT INTO `subway` VALUES ('28', '西王', '1', '28', null); 47 INSERT INTO `subway` VALUES ('29', '上庄', '1', '29', null); 48 INSERT INTO `subway` VALUES ('30', '只都', '2', '1', null); 49 INSERT INTO `subway` VALUES ('31', '北白皮', '2', '2', null); 50 INSERT INTO `subway` VALUES ('32', '蔡家岗', '2', '3', null); 51 INSERT INTO `subway` VALUES ('33', '桃村', '2', '4', null); 52 INSERT INTO `subway` VALUES ('34', '诸福屯', '2', '5', null); 53 INSERT INTO `subway` VALUES ('35', '罗家庄', '2', '6', '1'); 54 INSERT INTO `subway` VALUES ('36', '天元湖', '2', '7', null); 55 INSERT INTO `subway` VALUES ('37', '天元湖西', '2', '8', null); 56 INSERT INTO `subway` VALUES ('38', '北关', '2', '9', null); 57 INSERT INTO `subway` VALUES ('39', '石家庄学院', '2', '10', null); 58 INSERT INTO `subway` VALUES ('40', '正定', '2', '11', null); 59 INSERT INTO `subway` VALUES ('41', '西关', '2', '12', null); 60 INSERT INTO `subway` VALUES ('42', '神学院', '2', '13', null); 61 INSERT INTO `subway` VALUES ('43', '市农科院', '2', '14', null); 62 INSERT INTO `subway` VALUES ('44', '西古城', '2', '15', null); 63 INSERT INTO `subway` VALUES ('45', '铁道学院', '2', '16', null); 64 INSERT INTO `subway` VALUES ('46', '运河桥', '2', '17', null); 65 INSERT INTO `subway` VALUES ('47', '蓝天圣木', '2', '18', '5'); 66 INSERT INTO `subway` VALUES ('48', '长安公园', '2', '19', null); 67 INSERT INTO `subway` VALUES ('49', '北国商城', '2', '20', '1'); 68 INSERT INTO `subway` VALUES ('50', '河北大戏院', '2', '21', null); 69 INSERT INTO `subway` VALUES ('51', '新世隆', '2', '22', '6'); 70 INSERT INTO `subway` VALUES ('52', '东岗头', '2', '23', null); 71 INSERT INTO `subway` VALUES ('53', '东三教', '2', '24', null); 72 INSERT INTO `subway` VALUES ('54', '火车站', '2', '25', '3'); 73 INSERT INTO `subway` VALUES ('55', '塔谈', '2', '26', null); 74 INSERT INTO `subway` VALUES ('56', '塔谈南', '2', '27', '4'); 75 INSERT INTO `subway` VALUES ('57', '南位', '2', '28', null); 76 INSERT INTO `subway` VALUES ('58', '嘉华', '2', '29', null); 77 INSERT INTO `subway` VALUES ('59', '河北科技大学', '2', '30', null); 78 INSERT INTO `subway` VALUES ('60', '东尹村', '2', '31', null); 79 INSERT INTO `subway` VALUES ('61', '楼底', '2', '32', null); 80 INSERT INTO `subway` VALUES ('62', '平底', '2', '33', null); 81 INSERT INTO `subway` VALUES ('63', '留营', '2', '34', null); 82 INSERT INTO `subway` VALUES ('64', '油通村', '2', '35', null); 83 INSERT INTO `subway` VALUES ('65', '栾城西', '2', '36', null); 84 INSERT INTO `subway` VALUES ('66', '栾城', '2', '37', null); 85 INSERT INTO `subway` VALUES ('67', '工业园', '3', '1', null); 86 INSERT INTO `subway` VALUES ('68', '藁城东', '3', '2', null); 87 INSERT INTO `subway` VALUES ('69', '藁城', '3', '3', null); 88 INSERT INTO `subway` VALUES ('70', '藁城西', '3', '4', null); 89 INSERT INTO `subway` VALUES ('71', '南席东', '3', '5', null); 90 INSERT INTO `subway` VALUES ('72', '南席', '3', '6', null); 91 INSERT INTO `subway` VALUES ('73', '北乐乡', '3', '7', null); 92 INSERT INTO `subway` VALUES ('74', '韩通', '3', '8', null); 93 INSERT INTO `subway` VALUES ('75', '南豆', '3', '9', '6'); 94 INSERT INTO `subway` VALUES ('76', '中仰陵', '3', '10', null); 95 INSERT INTO `subway` VALUES ('77', '位同', '3', '11', null); 96 INSERT INTO `subway` VALUES ('78', '卓达', '3', '12', null); 97 INSERT INTO `subway` VALUES ('79', '南王', '3', '13', '4'); 98 INSERT INTO `subway` VALUES ('80', '裕华', '3', '14', null); 99 INSERT INTO `subway` VALUES ('81', '塔冢', '3', '15', null); 100 INSERT INTO `subway` VALUES ('82', '印刷二厂', '3', '16', null); 101 INSERT INTO `subway` VALUES ('83', '金利街', '3', '17', null); 102 INSERT INTO `subway` VALUES ('84', '火车站', '3', '18', '2'); 103 INSERT INTO `subway` VALUES ('85', '中星路', '3', '19', null); 104 INSERT INTO `subway` VALUES ('86', '槐安大街', '3', '20', '6'); 105 INSERT INTO `subway` VALUES ('87', '十一中', '3', '21', null); 106 INSERT INTO `subway` VALUES ('88', '新百广场', '3', '22', '1'); 107 INSERT INTO `subway` VALUES ('89', '市二中', '3', '23', null); 108 INSERT INTO `subway` VALUES ('90', '市庄', '3', '24', '5'); 109 INSERT INTO `subway` VALUES ('91', '柏林路', '3', '25', null); 110 INSERT INTO `subway` VALUES ('92', '高柱', '3', '26', null); 111 INSERT INTO `subway` VALUES ('93', '水上公园', '3', '27', null); 112 INSERT INTO `subway` VALUES ('94', '西三庄', '3', '28', null); 113 INSERT INTO `subway` VALUES ('95', '大郭镇', '3', '29', null); 114 INSERT INTO `subway` VALUES ('96', '南新城', '3', '30', null); 115 INSERT INTO `subway` VALUES ('97', '北海山', '3', '31', null); 116 INSERT INTO `subway` VALUES ('98', '鹿泉中心', '3', '32', null); 117 INSERT INTO `subway` VALUES ('99', '鹿泉医院', '3', '33', null); 118 INSERT INTO `subway` VALUES ('100', '鹿泉一中', '3', '34', null); 119 INSERT INTO `subway` VALUES ('101', '十里铺', '4', '1', null); 120 INSERT INTO `subway` VALUES ('102', '高营镇', '4', '2', null); 121 INSERT INTO `subway` VALUES ('103', '北翟营', '4', '3', null); 122 INSERT INTO `subway` VALUES ('104', '建华市场', '4', '4', '5'); 123 INSERT INTO `subway` VALUES ('105', '长安医院', '4', '5', null); 124 INSERT INTO `subway` VALUES ('106', '建百大楼', '4', '6', '1'); 125 INSERT INTO `subway` VALUES ('107', '儿童医院', '4', '7', null); 126 INSERT INTO `subway` VALUES ('108', '东明商城', '4', '8', '6'); 127 INSERT INTO `subway` VALUES ('109', '经济学院', '4', '9', null); 128 INSERT INTO `subway` VALUES ('110', '南王', '4', '10', '3'); 129 INSERT INTO `subway` VALUES ('111', '赵卜口', '4', '11', null); 130 INSERT INTO `subway` VALUES ('112', '涂料厂', '4', '12', null); 131 INSERT INTO `subway` VALUES ('113', '南栾', '4', '13', null); 132 INSERT INTO `subway` VALUES ('114', '塔谈南', '4', '14', '2'); 133 INSERT INTO `subway` VALUES ('115', '滨河街', '4', '15', null); 134 INSERT INTO `subway` VALUES ('116', '华医学院', '4', '16', '5'); 135 INSERT INTO `subway` VALUES ('117', '东良厢', '4', '17', null); 136 INSERT INTO `subway` VALUES ('118', '碧水蓝湾', '4', '20', null); 137 INSERT INTO `subway` VALUES ('119', '东五女', '5', '1', null); 138 INSERT INTO `subway` VALUES ('120', '南杨庄', '5', '2', null); 139 INSERT INTO `subway` VALUES ('121', '东杜庄', '5', '3', '1'); 140 INSERT INTO `subway` VALUES ('122', '西兆通', '5', '4', null); 141 INSERT INTO `subway` VALUES ('123', '土贤庄', '5', '5', null); 142 INSERT INTO `subway` VALUES ('124', '南翟营', '5', '6', null); 143 INSERT INTO `subway` VALUES ('125', '建华市场', '5', '7', '4'); 144 INSERT INTO `subway` VALUES ('126', '常青园', '5', '8', null); 145 INSERT INTO `subway` VALUES ('127', '蓝天圣木', '5', '9', '2'); 146 INSERT INTO `subway` VALUES ('128', '军械学院', '5', '10', null); 147 INSERT INTO `subway` VALUES ('129', '市庄', '5', '11', '3'); 148 INSERT INTO `subway` VALUES ('130', '火车北站', '5', '12', null); 149 INSERT INTO `subway` VALUES ('131', '和平西路', '5', '13', null); 150 INSERT INTO `subway` VALUES ('132', '河北二建', '5', '14', null); 151 INSERT INTO `subway` VALUES ('133', '和平医院', '5', '15', '1'); 152 INSERT INTO `subway` VALUES ('134', '益友百货', '5', '16', null); 153 INSERT INTO `subway` VALUES ('135', '审计厅', '5', '17', null); 154 INSERT INTO `subway` VALUES ('136', '碧海云天', '5', '18', '6'); 155 INSERT INTO `subway` VALUES ('137', '五星花园', '5', '19', null); 156 INSERT INTO `subway` VALUES ('138', '华医学院', '5', '20', '4'); 157 INSERT INTO `subway` VALUES ('139', '河滨街', '5', '21', null); 158 INSERT INTO `subway` VALUES ('140', '西岗头', '6', '1', null); 159 INSERT INTO `subway` VALUES ('141', '创业园', '6', '2', null); 160 INSERT INTO `subway` VALUES ('142', '碧海云天', '6', '3', '5'); 161 INSERT INTO `subway` VALUES ('143', '考试中心', '6', '4', null); 162 INSERT INTO `subway` VALUES ('144', '槐安大桥', '6', '5', '3'); 163 INSERT INTO `subway` VALUES ('145', '裕龙', '6', '6', null); 164 INSERT INTO `subway` VALUES ('146', '人防大厦', '6', '7', null); 165 INSERT INTO `subway` VALUES ('147', '新世隆', '6', '8', '2'); 166 INSERT INTO `subway` VALUES ('148', '石门公园', '6', '9', null); 167 INSERT INTO `subway` VALUES ('149', '省地震局', '6', '10', null); 168 INSERT INTO `subway` VALUES ('150', '东明商城', '6', '11', '4'); 169 INSERT INTO `subway` VALUES ('151', '国际城', '6', '12', null); 170 INSERT INTO `subway` VALUES ('152', '二十里铺', '6', '13', null); 171 INSERT INTO `subway` VALUES ('153', '海世界', '6', '14', '1'); 172 INSERT INTO `subway` VALUES ('154', '北豆', '6', '15', null); 173 INSERT INTO `subway` VALUES ('155', '南豆', '6', '16', '3'); 174 INSERT INTO `subway` VALUES ('156', '东仰陵', '6', '17', null); 175 INSERT INTO `subway` VALUES ('157', '工农路', '6', '18', null); 176 INSERT INTO `subway` VALUES ('158', '郗马', '6', '19', null);
利用对数据库的遍历查询:
一:若起始站和终点站在同一条线路上,则直接进行查询遍历
二:如起始站和终点站在不同线路上,则对数据库进行遍历,查找中转站,并按起始站到中转站,中转站到终点站进行遍历;
但是存在问题:
一:无法反向遍历查找
二:无法查找转站三次以上的线路
之后我们调用百度地图的API进行操作,简化了算法
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <meta name="viewport" 8 content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 9 <title>地铁路线查询</title> 10 <script src="//wow.techbrood.com/libs/three.r73.js"></script> 11 <script type="text/javascript" 12 src="https://api.map.baidu.com/api?type=subway&v=1.0&ak=SyGLbsO2V2hESPek1gB11yxcgUIlDVGI"></script> 13 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 14 <title>Insert title here</title> 15 <link rel="stylesheet" href="css/layui.css" media="all"> 16 <style type="text/css"> 17 #city { 18 19 } 20 21 #container { 22 height: 100% 23 } 24 </style> 25 </head> 26 <body> 27 <ul class="layui-nav layui-bg-cyan"> 28 <li class="layui-nav-item"><a href="">地铁线路规划</a></li> 29 <li class="layui-nav-item layui-this"><a href="#">线路查询</a></li> 30 <li class="layui-nav-item"><a href="">站点信息</a></li> 31 <li class="layui-nav-item"><a href="javascript:;">信息公开</a> 32 <dl class="layui-nav-child"> 33 <dd> 34 <a href=""></a> 35 </dd> 36 <dd> 37 <a href=""></a> 38 </dd> 39 <dd> 40 <a href=""></a> 41 </dd> 42 </dl></li> 43 <li class="layui-nav-item"><a href="">留言</a></li> 44 </ul> 45 <br> 46 <div> 47 <div> 48 <span style="margin-left: 20px; width:20px" >选择城市:</span> 49 <select id=city style="margin-left: 20px;"></select> 50 </div> 51 52 <div> 53 <span style="margin-left: 20px;">起始位置:</span> 54 <input type="text" id=startname value="起点" style=" width:100px ;height :25px" /> 55 </div> 56 57 <div> 58 <span style="margin-left: 20px;">终点位置:</span> 59 <input type="text" id=endname value="终点" style=" width:100px;height :25px"/> <input type="button" id=btn value="搜索路线"> 60 </div> 61 62 </div> 63 64 <div id="container"></div> 65 <script type="text/javascript"> 66 var list = BMapSub.SubwayCitiesList; 67 var subway; 68 $(function() { 69 InitSelect(list);//初始化城市选择框 70 InitMap(150, null); 71 72 }); 73 //城市选择框改变事件,用于城市地图切换 74 $("#city").change(function() { 75 //var a = $("#city :selected").text(); 76 //console.log(a); 77 InitMap($("#city").val(), null); 78 }); 79 //初始化城市选择框 80 function InitSelect(list) { 81 for ( var key in list) { 82 var obj = document.getElementById("city"); 83 var option = document.createElement("option");//创建option节点 84 option.innerText = list[key].name; 85 option.value = list[key].citycode; 86 obj.appendChild(option); 87 } 88 }; 89 // 获取地铁数据-初始化地铁图 90 function InitMap(citycode, startname) { 91 subway = new BMapSub.Subway('container', citycode); 92 subway.setZoom(0.7); 93 var zoomControl = new BMapSub.ZoomControl({ 94 anchor : BMAPSUB_ANCHOR_TOP_LEFT, 95 offset : new BMapSub.Size(10, 100) 96 }); 97 subway.addControl(zoomControl); 98 //添加站名点击事件 99 subway.addEventListener('tap', function(e) { 100 ///alert('您点击了"' + e.station.name + '"站'); 101 var marker = new BMapSub.Marker(e.station.name);//, {icon: startIcon} 102 subway.addMarker(marker); 103 subway.setCenter(e.station.name) 104 var a = $("#startname").val(); 105 var b = $("#endname").val(); 106 if (a == "" || a == "起点") { 107 $("#startname").val(e.station.name); 108 } else { 109 $("#endname").val(e.station.name); 110 } 111 subway.setZoom(0.7); 112 }); 113 if (startname != null) { 114 var marker = new BMapSub.Marker(startname);//, {icon: startIcon} 115 subway.addMarker(marker); 116 subway.setCenter(startname); 117 subway.setZoom(1.5); 118 } 119 120 }; 121 122 $("#btn").click(function() { 123 //alert("点击事件"); 124 var drct = new BMapSub.Direction(subway, false); 125 126 var a = $("#startname").val(); 127 var b = $("#endname").val(); 128 drct.search(a, b); 129 }); 130 </script> 131 <script> 132 layui.use('element', function() { 133 var element = layui.element; //导航的hover效果、二级菜单等功能,需要依赖element模块 134 //监听导航点击 135 element.on('nav(demo)', function(elem) { 136 //console.log(elem) 137 layer.msg(elem.text()); 138 }); 139 }); 140 </script> 141 </body> 142 </html> 143
结合Javascript进行线路规划:
很高兴
很高兴这次的合作,希望以后有机会还能合作!