Click here to Skip to main content
15,881,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
i make Google map that can have cross all airports in Egypt by shortest path with Neighborliness algorithm but i want to make Toast.makeText tell me Order of airports or the all airports cross by order

code :

public class Sixth extends Activity {
GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sixth);
createMapView();
TSPNearestNeighbour TSP = new TSPNearestNeighbour();
int[] shortest = new int[21];
String[] placeNames = {"Cairo International Airport","Alexandria International Airport","Borg El Arab Airport",
"Marsa Matrouh Airport","Sharm el-Sheikh International Airport","Taba International Airport","El Kharga Airport",
"Assiut Airport","Luxor International Airport","Aswan International Airport","El Arish International Airport",
"St. Catherine International Airport","Sharq Al-Owainat Airport","Abu Simbel Airport","Sohag International Airport",
"Port Said Airport","El Tor Airport","Dakhla Oasis Airport","Marsa Alam International Airport","Cairo West Air Base","Almaza Air Force Base"};
String[] placeNamesSnippet = {"Cairo International Airport1","Alexandria International Airport2","Borg El Arab Airport3", "Marsa Matrouh Airport4","Sharm el-Sheikh International Airport5","Taba International Airport6","El Kharga Airport7", "Assiut Airport8","Luxor International Airport9","Aswan International Airport10","El Arish International Airport11", "St. Catherine International Airport12","Sharq Al-Owainat Airport13","Abu Simbel Airport14","Sohag International Airport15", "Port Said Airport16","El Tor Airport17","Dakhla Oasis Airport18","Marsa Alam International Airpor19t","Cairo West Air Bas20e","Almaza Air Force Base21"};
Double[] placeLatitude = {30.111370, 31.192553, 30.917138,31.324435,27.978620,29.590988,27.188222,27.047695, 25.670264,
23.960397,31.076449,28.684537,22.580600,22.375813,26.331926,31.281150,28.208842,25.688581,25.558141,
30.116704,30.095975};
Double[] placeLongitude = {31.413910, 29.953141,29.693375, 27.222200,34.393354,34.778946 , 33.800840, 31.013473 , 32.704063,
32.821163,33.832256,34.062882, 28.720754, 31.611667,31.728437,32.242223,33.645257,28.972356,34.582821,
30.916667,31.362748};

Double[][] Matrix = new Double[21][21];

googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(placeLatitude[0], placeLongitude[0]), 13));
for(int i = 0 ; i<21;i++)
{
googleMap.addMarker(new MarkerOptions()
.snippet(placeNamesSnippet[i])
.position(new LatLng(placeLatitude[i], placeLongitude[i]))
.title(placeNames[i]));
}

for(int i=0;i<21;i++)
{
for(int j=0;j<21;j++)
{
Matrix[i][j] = distance(placeLatitude[i],placeLongitude[i],placeLatitude[j],placeLongitude[j],'K');
}
}

shortest = TSP.tsp(Matrix);


for(int i=0; i < 20 ; i++)
{

googleMap.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(placeLatitude[shortest[i]],placeLongitude[shortest[i]]))
.add(new LatLng(placeLatitude[shortest[i+1]],placeLongitude[shortest[i+1]]))
);
}

}

private void createMapView(){
/**
* Catch the null pointer exception that
* may be thrown when initialising the map
*/
try {
if(null == googleMap){
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
/**
* If the map is still null after attempted initialisation,
* show an error to the user
*/
if(null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
}
private double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<integer> stack;

public TSPNearestNeighbour()
{
stack = new Stack<integer>();
}

public int[] tsp(Double adjacencyMatrix[][])
{
numberOfNodes = adjacencyMatrix[0].length ;
int[] result = new int[adjacencyMatrix[0].length];
int resultCounter = 1;
int[] visited = new int[numberOfNodes];
visited[0] = 1;
stack.push(0);
int element, dst = 0, i;
Double min = Double.MAX_VALUE;
boolean minFlag = false;
result[0] = 0;

//System.out.print(1 + "\t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 0;
min = Double.MAX_VALUE;
while (i < numberOfNodes)
{
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{
if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
result[resultCounter] = dst;
resultCounter++;
//System.out.print(dst + "\t");
minFlag = false;
continue;
}
stack.pop();
}
return result;
}

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900