Minggu, 11 Oktober 2020

Android Code Dijkstra

 package com.example.parmalteng;


import android.app.Activity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.text.Html;

import android.text.TextUtils;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;


import org.apache.http.NameValuePair;

import org.apache.http.message.BasicNameValuePair;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


import java.io.InputStream;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.List;


public class Dijkstra extends Activity {



int jb=5;

String[]arB;

String kodenode1A,namanode1A,kodenode1B,namanode1B;

double jarak1A,jarak1B;


int jd=28;

String[]arKode;

double[]arLati;

double[]arLongi;

double[]arJarakA;

double[]arJarakB;

String[]arNama;




String ip="";

String id_wisata0="";


TextView txtnama_wisata;

TextView txtalamat,txtalamat2;

TextView txtkategori;

TextView txtdeskripsi;

TextView txtketerangan;


String gambar,gabKoor="";

String latitude;

String longitude;


Button btnDjikstra;


private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();


private static final String TAG_SUKSES = "sukses";

private static final String TAG_record = "record";


private static final String TAG_nama_wisata = "nama_wisata";

private static final String TAG_alamat = "alamat";

private static final String TAG_kategori = "kategori";

private static final String TAG_latitude = "latitude";

private static final String TAG_longitude = "longitude";

private static final String TAG_deskripsi = "deskripsi";

private static final String TAG_gambar = "gambar";

private static final String TAG_keterangan = "keterangan";


String myLati = "";//-3.705235

String myLongi = "";//128.088799

String myPosisi = "";//Bandar Udara Internasional Pattimura


String myLati2 = "";//-3.705235

String myLongi2 = "";//128.088799

String myPosisi2 = "";//Bandar Udara Internasional Pattimura


String NW="", KATW="";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.dijkstra);


ip=jsonParser.getIP();

// callMarquee();



txtnama_wisata= (TextView) findViewById(R.id.txtnama_wisata);

txtkategori= (TextView) findViewById(R.id.txtkategori);

txtalamat= (TextView) findViewById(R.id.txtalamat);

txtalamat2= (TextView) findViewById(R.id.txtalamat2);

txtdeskripsi= (TextView) findViewById(R.id.txtdeskripsi);

txtketerangan= (TextView) findViewById(R.id.txtketerangan);



btnDjikstra = (Button) findViewById(R.id.btnDjikstra);



Intent i = getIntent();

id_wisata0 = i.getStringExtra("pk");


myLati = i.getStringExtra("myLati");

myLongi= i.getStringExtra("myLongi");

myPosisi = i.getStringExtra("myPosisi");


myLati2 = i.getStringExtra("myLati2");

myLongi2= i.getStringExtra("myLongi2");

myPosisi2 = i.getStringExtra("myPosisi2");



NW = i.getStringExtra("NW");

KATW= i.getStringExtra("KATW");

gambar = i.getStringExtra("gambar");



txtnama_wisata.setText(NW+" ("+id_wisata0+")");

txtkategori.setText(KATW);




btnDjikstra.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

cetak(gabKoor);

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(gabKoor));

//intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");

startActivity(intent);


}});




String arUrlFoto=ip+"ypathfile/"+gambar;

new DownloadImageTask((ImageView) findViewById(R.id.myGambar)).execute(arUrlFoto);

callMarquee();



start();

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

ImageView bmImage;


public DownloadImageTask(ImageView bmImage) {

this.bmImage = bmImage;

}


protected Bitmap doInBackground(String... urls) {

String urldisplay = urls[0];

Bitmap mIcon11 = null;

try {

InputStream in = new java.net.URL(urldisplay).openStream();

mIcon11 = BitmapFactory.decodeStream(in);

}

catch (Exception e) {Log.e("Error", e.getMessage());e.printStackTrace();}

return mIcon11;

}


protected void onPostExecute(Bitmap result) {

bmImage.setImageBitmap(result); }

}



void callMarquee(){

Calendar cal = Calendar.getInstance();

int jam = cal.get(Calendar.HOUR);

int menit= cal.get(Calendar.MINUTE);

int detik= cal.get(Calendar.SECOND);


int tgl= cal.get(Calendar.DATE);

int bln= cal.get(Calendar.MONTH)+1;

int thn= cal.get(Calendar.YEAR);


String stgl=String.valueOf(tgl)+"-"+String.valueOf(bln)+"-"+String.valueOf(thn);

String sjam=String.valueOf(jam)+":"+String.valueOf(menit)+":"+String.valueOf(detik);


TextView  txtMarquee=(TextView)findViewById(R.id.txtMarquee);

txtMarquee.setSelected(true);

String kata="Selamat Datang Aplikasi Android Wisata Maluku Tengah "+stgl+"/"+sjam+" #";

String kalimat=String.format("%1$s",TextUtils.htmlEncode(kata));

txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat));

}


public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

finish();

return true;

}

return super.onKeyDown(keyCode, event);

}


void start(){

init();

double mlati=Double.parseDouble(myLati);

double mlongi=Double.parseDouble(myLongi);


double mlati2=Double.parseDouble(myLati2);

double mlongi2=Double.parseDouble(myLongi2);



double jmaxA=1000000000;

int indexA=0;

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

double r=0;

try{

r=getJarak(mlati,mlongi,arLati[i],arLongi[i]);

arJarakA[i]=r;

if(r<jmaxA){

jmaxA=r;

indexA=i;

}

}

catch(Exception ee){}

}


double jmaxB=1000000000;

int indexB=0;

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

double r=0;

try{

r=getJarak(mlati2,mlongi2,arLati[i],arLongi[i]);

arJarakB[i]=r;

if(r<jmaxB){

jmaxB=r;

indexB=i;

}

}

catch(Exception ee){}

}



kodenode1A=arKode[indexA];

jarak1A=arJarakA[indexA];

namanode1A=arNama[indexA];


kodenode1B=arKode[indexB];

jarak1B=arJarakB[indexB];

namanode1B=arNama[indexB];


cetak("LOKASI:"+kodenode1A+"-"+kodenode1B);

//==========================

//mulai("A","b");

mulai(kodenode1A,kodenode1B);


}


void mulai(String src,String dst) {


Graph a=new Graph();

a.ins_simpul('A');   ////0

a.ins_simpul('B');   ////1

a.ins_simpul('C');   ////2

a.ins_simpul('D');   ////3

a.ins_simpul('E');   ////4

a.ins_simpul('F');   ////5

a.ins_simpul('G');   ////6

a.ins_simpul('H');   ////7

a.ins_simpul('I');   ////8

a.ins_simpul('J');   ////9

a.ins_simpul('K');   ////10

a.ins_simpul('L');   ////11

a.ins_simpul('M');   ////12

a.ins_simpul('N');   ////13

a.ins_simpul('O');   ////14

a.ins_simpul('P');   ////15

a.ins_simpul('Q');   ////16

a.ins_simpul('R');   ////17

a.ins_simpul('S');   ////18

a.ins_simpul('T');   ////19

a.ins_simpul('U');   ////20

a.ins_simpul('V');   ////21

a.ins_simpul('W');   ////22

a.ins_simpul('X');   ////23

a.ins_simpul('Y');   ////24

a.ins_simpul('Z');   ////25

a.ins_simpul('a');   ////26

a.ins_simpul('b');   ////27


a.ins_adjacent(0, 1, 3338);//AB

a.ins_adjacent(0, 2, 7544);//AC


a.ins_adjacent(1, 0, 3338);//BA

a.ins_adjacent(1, 7, 9897);//BH

a.ins_adjacent(1, 8, 9926);//BI


a.ins_adjacent(2, 0, 12892);//CA

a.ins_adjacent(2, 3, 10166);//CD


a.ins_adjacent(3, 2, 5893);//DC

a.ins_adjacent(3, 4, 10501);//DE


a.ins_adjacent(4, 3, 10501);//ED

a.ins_adjacent(4, 5, 21118);//EF


a.ins_adjacent(5, 4, 21118);//FE

a.ins_adjacent(5, 6, 9949);//FG


a.ins_adjacent(6, 5, 9949);//GF

a.ins_adjacent(6, 10, 6627);//GK

a.ins_adjacent(6, 26, 18860);//Gb


a.ins_adjacent(7, 1, 9897);//HB

a.ins_adjacent(7, 8, 885);//HI

a.ins_adjacent(7, 10, 2899);//HK


a.ins_adjacent(8, 1, 9926);//IB

a.ins_adjacent(8, 7, 885);//IH

a.ins_adjacent(8, 9, 1873);//IJ


a.ins_adjacent(9, 8, 1873);//JI

a.ins_adjacent(9, 13, 7361);//JN


a.ins_adjacent(10, 6, 6627);//KG

a.ins_adjacent(10, 7, 2289);//KH

a.ins_adjacent(10, 11, 3107);//KL


a.ins_adjacent(11, 10, 3107);//LK

a.ins_adjacent(11, 12, 1207);//LM


a.ins_adjacent(12, 11, 1207);//ML

a.ins_adjacent(12, 13, 1003);//MN

a.ins_adjacent(12, 15, 1110);//MP


a.ins_adjacent(13, 9, 7361);//NJ

a.ins_adjacent(13, 12, 1003);//NM

a.ins_adjacent(13, 14, 417);//NO


a.ins_adjacent(14, 13, 417);//ON

a.ins_adjacent(14, 15, 1185);//OP


a.ins_adjacent(15, 12, 1110);//PM

a.ins_adjacent(15, 14, 1185);//PO

a.ins_adjacent(15, 16, 3850);//PQ


a.ins_adjacent(16, 15, 3850);//QP

a.ins_adjacent(16, 17, 7146);//QR

a.ins_adjacent(16, 18, 5392);//QS


a.ins_adjacent(17, 16, 7146);//RQ

a.ins_adjacent(17, 18, 3166);//RS


a.ins_adjacent(18, 16, 5392);//SQ

a.ins_adjacent(18, 17, 3166);//SR

a.ins_adjacent(18, 19, 1608);//ST

a.ins_adjacent(18, 20, 1227);//SU


a.ins_adjacent(19, 18, 1608);//TS


a.ins_adjacent(20, 18, 1227);//US

a.ins_adjacent(20, 21, 1802);//UV

a.ins_adjacent(20, 23, 1375);//UX


a.ins_adjacent(21, 20, 1802);//VU

a.ins_adjacent(21, 22, 1016);//VW

a.ins_adjacent(21, 24, 947);//VY


a.ins_adjacent(22, 21, 1016);//WV


a.ins_adjacent(23, 20, 1376);//XU

a.ins_adjacent(23, 24, 1110);//XY


a.ins_adjacent(23, 21, 947);//YV

a.ins_adjacent(24, 23, 1110);//YX

a.ins_adjacent(24, 25, 4876);//YZ


a.ins_adjacent(25, 24, 4876);//ZY

a.ins_adjacent(25, 26, 1105);//Za

a.ins_adjacent(25, 27, 1799);//Zb


a.ins_adjacent(26, 25, 1105);//aZ

a.ins_adjacent(26, 27, 1995);//ab


a.ins_adjacent(27, 6, 18860);//bG

a.ins_adjacent(27, 25, 1799);//bZ

a.ins_adjacent(27, 26, 1995);//ba


String []HSL= a.Dijkstra();

System.out.println("++++++++++++++++++++++++++++++++++");

//System.out.println("HS="+HSL);


int total=0;

String gab1="Posisi Awal:"+myPosisi+" ("+myLati+","+myLongi+") =>Dekat "+namanode1A+"#"+kodenode1A+" :"+String.valueOf(jarak1A)+" m";

String gab2="Posisi Ahir:"+myPosisi2+" ("+myLati2+","+myLongi2+") =>Dekat "+namanode1B+"#"+kodenode1B+" :"+String.valueOf(jarak1B)+" m";


txtdeskripsi.setText(myPosisi+" ("+myLati+","+myLongi+") =>Dekat "+namanode1A+"#"+kodenode1A+" :"+String.valueOf(jarak1A)+" m");

txtketerangan.setText(myPosisi2+" ("+myLati2+","+myLongi2+") =>Dekat "+namanode1B+"#"+kodenode1B+" :"+String.valueOf(jarak1B)+" m");




String mrute="";



for(int k=1;k<HSL.length;k++){

if(HSL[k].length()>3){

String mHasil="A#"+HSL[k];

int AW=mHasil.indexOf(src);

int AH=mHasil.indexOf(dst);

if(AW>=0 && AH>=0){//ADA

if(AW<=AH){//MAJU

mrute=mHasil;

cetak("FF="+mrute);

}

else{//REVERSE

mrute=mHasil+"#";

String reverse="";

for (int i = mrute.length() - 1 ; i >= 0 ; i--){

reverse = reverse + mrute.charAt(i);

}

mrute =reverse;

mrute=mrute.substring(1,mrute.length());

cetak("REV="+mrute);

}


}//ada >=0

//=========================

}

}//k

if(mrute.length()<3){

for(int b=0;b<jb;b++){

String lrute=arB[b];

int AW=lrute.indexOf(src);

int AH=lrute.indexOf(dst);

if(AW>=0 && AH>=0){//ADA

if(AW<=AH){//MAJU

mrute=lrute;

cetak("F2="+mrute);

}

else{//REVERSE


mrute=lrute;

String reverse="";

for (int i = lrute.length() - 1 ; i >= 0 ; i--){

reverse = reverse + lrute.charAt(i);

}

mrute =reverse;

cetak("REV2="+mrute);

}

}

else{

cetak("NONE");

}

}//jb

}//for

double totalJarak=0;

if(mrute.length()>0){

double LAT1=0,LON1=0;

double LAT2=0,LON2=0;



LAT1=Double.parseDouble(myLati);

LON1=Double.parseDouble(myLongi);

LAT2=Double.parseDouble(myLati2);

LON2=Double.parseDouble(myLongi2);

double jarak=0;

String gabKota=myPosisi+"-";

String gabKode="GPS-";

gabKoor="https://www.google.co.id/maps/dir/"+LAT1+","+LON1;


String gabJarak="";

mrute=mrute.replaceAll("#", "");

cetak("GET="+mrute);

//aZYWVUSQPMLKHIBACD

int x1=mrute.indexOf(src);

int y1=mrute.indexOf(dst)+1;



mrute=mrute.substring(x1,y1);


cetak("GET="+mrute);

// mrute=mrute+"A";

char[]ar=mrute.toCharArray();


String kode2="",kota2="";

int id1=getIndex(String.valueOf(ar[0]));

int id2=getIndex(String.valueOf(ar[0]));

jarak=  getJarak(LAT1,LON1,arLati[id1],arLongi[id1]);

gabJarak=String.valueOf(jarak)+"+";

totalJarak=jarak;


for(int i=1;i<ar.length;i++){

String kode1=String.valueOf(ar[i-1]);

kode2=String.valueOf(ar[i]);

id1=getIndex(kode1);

id2=getIndex(kode2);

String kota1=arNama[id1];

kota2=arNama[id2];

LAT1=arLati[id1];

LON1=arLongi[id1];

LAT2=arLati[id2];

LON2=arLongi[id2];

jarak=getJarak(LAT1,LON1,LAT2,LON2);

gabKota+=kota1+"-";

gabKode+=kode1+"-";

gabKoor+="/"+LAT1+","+LON1;

gabJarak+=jarak+"+";


String br=kode1+"-"+kode2+"="+jarak;

cetak(i+"."+br);


totalJarak+=jarak;

}

jarak=  getJarak(arLati[id2],arLongi[id2],LAT2,LON2);

//totalJarak+=jarak;

totalJarak+=jarak1B;


//gabJarak+=String.valueOf(jarak);

gabJarak+=String.valueOf(jarak1B);


gabKode+=kode2+"-"+"TUJUAN_WISATA";

gabKota+=kota2+"-"+myPosisi2;

gabKoor+="/"+LAT2+","+LON2+"/"+myLati2+","+myLongi2;


cetak(gabKode);

cetak(gabJarak+"\nTOTAL JARAK="+String.valueOf(totalJarak)+" m");

cetak(gabKota);

cetak(gabKoor);


txtalamat2.setText(gabKota);

txtalamat.setText(gabKode+"==>"+gabJarak);


}

else{

cetak("No Route.....");

}

}



void cekJarak(String aw,String ah){

int index1=getIndex(aw);

int index2=getIndex(ah);


double lat1=arLati[index1];

double lon1=arLongi[index1];

double lat2=arLati[index2];

double lon2=arLongi[index2];


double jrk=getJarak(lat1,lon1,lat2,lon2);

String gab=aw+"-"+ah+"="+String.valueOf(jrk)+" m";

cetak(gab);

}


int getIndex(String ch){

int index=0;

for(int i=0;i<arKode.length;i++){

if(ch.equals(arKode[i])){index=i;break;}

}

return index;

}


void cetak(String info){

Log.d("Info",info);

//System.out.println(info);

}

void init(){

int jd=28;

arKode = new String[jd];


arLati = new double[jd];

arLongi = new double[jd];

arJarakA = new double[jd];

arJarakB = new double[jd];

arNama=new String[jd];


arKode[0]="A";

arLati[0]=-3.704186;

arLongi[0]=128.088038;

arNama[0]="Jl. Propinsi Bandara";


arKode[1]="B";

arLati[1]=-3.688551;

arLongi[1]=128.113615;

arNama[1]="Jl. Propinsi Tawiri";


arKode[2]="C";

arLati[2]=-3.742976;

arLongi[2]=128.032114;

arNama[2]="Jl. Propinsi Allang";


arKode[3]="D";

arLati[3]=-3.773732;

arLongi[3]=127.945806;

arNama[3]="Jl. Propinsi Wakasihu";


arKode[4]="E";

arLati[4]=-3.682396;

arLongi[4]=127.921659;

arNama[4]="Jl. Propinsi Asilulu";


arKode[5]="F";

arLati[5]=-3.584788;

arLongi[5]=128.084958;

arNama[5]="Jl. Propinsi Hila";


arKode[6]="G";

arLati[6]=-3.585407;

arLongi[6]=128.174626;

arNama[6]="Jl. Propinsi Hitu";


arKode[7]="H";

arLati[7]=-3.648689;

arLongi[7]=128.193389;

arNama[7]="Poka";


arKode[8]="I";

arLati[8]=-3.655857;

arLongi[8]=128.196871;

arNama[8]="Jl. Dr. J. Leimena";


arKode[9]="J";

arLati[9]=-3.672499;

arLongi[9]=128.199532;

arNama[9]="Sirimau";


arKode[10]="K";

arLati[10]=-3.631248;

arLongi[10]=128.212820;

arNama[10]="Hunuth";


arKode[11]="L";

arLati[11]=-3.630785;

arLongi[11]=128.240829;

arNama[11]="Negeri Lama";


arKode[12]="M";

arLati[12]=-3.623666;

arLongi[12]=128.249054;

arNama[12]="Passo";


arKode[13]="N";

arLati[13]=-3.632148;

arLongi[13]=128.252139;

arNama[13]="Passo, Baguala 1";


arKode[14]="O";

arLati[14]=-3.630758;

arLongi[14]=128.255635;

arNama[14]="Passo, Baguala 2";


arKode[15]="P";

arLati[15]=-3.620501;

arLongi[15]=128.258551;

arNama[15]="Passo, Singsingamangaraja";


arKode[16]="Q";

arLati[16]=-3.621413;

arLongi[16]=128.291651;

arNama[16]="Suli";


arKode[17]="R";

arLati[17]=-3.599242;

arLongi[17]=128.353811;

arNama[17]="Tulehu 1";


arKode[18]="S";

arLati[18]=-3.587181;

arLongi[18]=128.327961;

arNama[18]="Jl. Propinsi Tulehu ";


arKode[19]="T";

arLati[19]=-3.586484;

arLongi[19]=128.313485;

arNama[19]="Tulehu 2";


arKode[20]="U";

arLati[20]=-3.576531;

arLongi[20]=128.325056;

arNama[20]="Jl. Propinsi Waai 1";


arKode[21]="V";

arLati[21]=-3.561847;

arLongi[21]=128.318215;

arNama[21]="Jl. Propinsi Waai 2";


arKode[22]="W";

arLati[22]=-3.558669;

arLongi[22]=128.309599;

arNama[22]="Waai";


arKode[23]="X";

arLati[23]=-3.564994;

arLongi[23]=128.320583;

arNama[23]="Jl. Propinsi Waai 3";


arKode[24]="Y";

arLati[24]=-3.555631;

arLongi[24]=128.324060;

arNama[24]="Jl. Propinsi Waai 4";


arKode[25]="Z";

arLati[25]=-3.514292;

arLongi[25]=128.338754;

arNama[25]="Jl. Propinsi Liang 1";


arKode[26]="a";

arLati[26]=-3.505168;

arLongi[26]=128.342712;

arNama[26]="Liang 1";


arKode[27]="b";

arLati[27]=-3.506121;

arLongi[27]=128.324752;

arNama[27]="Jl. Propinsi Liang 2";



jb=29;

arB=new String[jb];

arB[0]="EFGbZ";

arB[1]="EFGKLMPQSUVYZb";

arB[2]="EDCABIJNOPQSUVYZb";

}



public static double getJarak(double lat1, double lng1, double lat2, double lng2) {

double earthRadius = 3958.75;

double dLat = Math.toRadians(lat2-lat1);

double dLng = Math.toRadians(lng2-lng1);

double a = Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *

Math.sin(dLng/2) * Math.sin(dLng/2);

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

double dist = earthRadius * c;


int meterConversion = 1609;

double myjr=dist * meterConversion;

return Math.floor(myjr);


}

}


Tidak ada komentar:

Posting Komentar