由于需要,需要在百度地图的基础上实现小范围(例如校园,景区等地)内的详细最短路径导航,百度地图无法实现,因此通过SQLite数据库自行添加地点坐标信息,然后通过Dijkstra算法实现了两地点间的最短路径搜索。
直接上代码:
首先是定义的一个节点雷:Side.java
用于存放以及获取节点信息
package com.xjdx.suanfa;
public class Side {
private int preNode; //前驱节点
private int nextNode;//后继节点
private int weight;//权重
public Side(int preNode,int nextNode,int weight){
this.preNode = preNode;
this.nextNode = nextNode;
this.weight = weight;
}
public int getPreNode(){
return this.preNode;
}
public void setPreNode(int preNode){
this.preNode = preNode;
}
public int getNextNode(){
return this.nextNode;
}
public void setNextNode(int nextNode){
this.nextNode = nextNode;
}
public int getWeight(){
return this.weight;
}
public void setWeight(int weight){
this.weight = weight;
}
}
然后是一个用于存储以及获取最短路径类:MinShortPath.java
package com.xjdx.suanfa;
import java.util.ArrayList;
public class MinShortPath {
private ArrayList<Integer> nodeList;//最短路径集
private int weight;//最短路径
public MinShortPath(int node){
nodeList = new ArrayList<Integer>();
nodeList.add(node);
weight = -1;
}
public ArrayList<Integer> getNodeList(){
return nodeList;
}
public void setNodeList(ArrayList<Integer> nodeList){
this.nodeList = nodeList;
}
public void addNode(int node){
if(nodeList == null){
nodeList = new ArrayList<Integer>();
}
nodeList.add(0,node);
}
public int getLastNode(){
int size = nodeList.size();
return nodeList.get(size-1);
}
public int getWeight(){
return weight;
}
public void setWeight(int weight){
this.weight = weight;
}
public void outputPath(){
outputPath(-1);
}
public void outputPath(int srcNode){
String result = "[";
if(srcNode != -1){
nodeList.add(srcNode);
}
for(int i=0;i<nodeList.size();i++){
result += "" + nodeList.get(i);
if(i<nodeList.size()-1){
result += ",";
}
}
result +="]:"+weight;
System.out.println(result);
}
public void addWeight(int w){
if(weight == -1){
weight = w;