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