android 地铁最短路线换乘查询系统(1)
这是我之前做的一个广州地铁地铁最短路线换乘查询的android应用程序。实现了最短路线换乘查询和优化过的查询结果。其中难点有3:一是用图这个数据结构来构建整个地铁站点图;二是最短路径算法;三是查询结果的优化。
特点:资源与算法核心高度分离,随时可以更新地铁的线路和站点信息,而不用更改算法等其它部分。自动生成图, 数据更新方便,移植性强,可重用性高
1. 程序框架www.zzzyk.com
MetroSearch:主Activity,提供线路查询功能。
MapDisplay: 副Activity,提供地铁图线路浏览功能。(附加功能)
PathSearch:地铁线路查询的核心类,包括了地铁图的数据结构描述,算法实现,最短路径描述等功能。
ResFinalVars:资源类,包括了地铁线路信息。
GraphEntry:邻接表类, 用于表示地铁线路图的数据结构。
TableEntry:用于记录最短路径,用于路径描述。
2. 地铁线路的数据结构描述
第一步:将地铁站点表示为数字编号,便于处理
方法:采用Hash表来存储地铁站点名字和数字编号的映射关系
优点:自动编号,搜索速度快
第二步:采用邻接表来构建图数据结构,资源数据与方法分开
方法:采用GraphEntry这个类来构建,其中图的构建全部是根据资源类ResFinalVars来自动生成图。
特点:所有地铁信息的改动,只需要在ResFinalVars修改就可以了,其它的地方一律不用改变。
优点:自动生成图,数据更新方便,移植性强,可重用性高
3. 查询结果处理与优化
GraphEntry类
class GraphEntry {
private ArrayList<Integer> list;
private int line;
private int info;
…
}
成员line保存着地铁线路号,如一号线,二号线。采用位域来表示,可以保存多条路线信息,且节省空间。(int型可以保存最多32条路线信息)
成员info保存着是普通站还是换乘站的信息,方便快速判断
4. 代码实现(后续)
资源类:
/*
* Author: shenjunyong
* E-mail: junyong.shen@samsung.com
* Date: 2012.6.14
* */
package com.xiaoben.metrosearch2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import java.util.*;
final class ResFinalVars{
public static final int INFINITE = 0xffff;
public static final int UNKNOWN = -1;
public static final int TRANSIT = 0;
public static final int NORMAL = 1;
public static enum LINESINFO{
LINE1(1), LINE2(2), LINE3(4), LINE4(8), LINE5(0x10), LINE6(0x20), LINE7(0x40), LINE8(0x80), LINEGF(0x100);
private final int value;
private LINESINFO(int value){
this.value = value;
}
public int getValue(){
return this.value;
}
};
public static final int LINE1 = 1;
public static final int LINE2 = 2;
public static final int LINE3 = 4;
public static final int LINE4 = 8;
public static final int LINE5 = 0x10;
public static final int LINE6 = 0x20;
public static final int LINE7 = 0x40;
public static final int LINE8 = 0x80;
public static final int LINEGF = 0x100;
public static final String[] lines_number = {
"地铁一号线", "地铁二号线", "地铁三号线", "地铁四号线", "地铁五号线", "", "", "地铁八号线", "地铁广佛线"
};
public static final String line1_stations[] = {
"广州东站", "体育中心", "体育西路", "杨箕", "东山口", "烈士陵园", "农讲所", "公园前", "西门口", "陈家祠", "长寿路", "黄沙", "芳村", "花地湾", "坑口", "西朗",
};
public static final String line2_stations[] = {
"广州南站", "石壁", "会江", "南浦", "洛溪", "南洲", "东晓南", "江泰路", "昌岗", "江南西", "市二宫", "海珠广场", "公园前", "纪念堂", "越秀公园", "广州火车站", "三元里", "飞翔公园", "白云公园", "白云文化广场", "萧岗", "江夏", "黄边", "嘉禾望岗",
};
public static final String line3a_stations[] = {
"天河客运站", "五山", "华师", "岗顶", "石牌桥", "体育西路", "珠江新城", "赤岗塔", "客村", "大塘", "沥滘", "厦滘", "大石", "汉溪长隆", "市桥", "番禺广场"
};
public static final String line3b_stations[] = {
"体育西路", "林和西", "广州东站", "燕塘", "梅花园", "京溪南方医院", "同和", "永泰", "白云大道北", "嘉禾望岗", "龙归", "人和", "机场南站",
};
public static final String line4_stations[] = {
"黄村", "车陂", "车陂南", "万胜围", "官洲", "大学城北", "大学城南", "新造", "石碁", "海傍", "低涌", "东涌", "黄阁汽车城", "黄阁", "蕉门", "金洲",
};
public static final String line5_stations[] = {
"滘口", "坦尾", "中山八", "西场", "西村", "广州火车站", "小北", "淘金", "区庄", "动物园", "杨箕", "五羊邨", "珠江新城", "猎德", "潭村",
补充:移动开发 , Android ,