Jumat, 23 Desember 2016

Menghitung Jarak dua Koordinat

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;

   double meterConversion = 1609;
   double myjr=dist * meterConversion;
   return Math.floor(myjr/1609);
   }

double hitJarak(double alat,double along,String myLati2,String myLongi2){

  double lat=-6.423161;
       double lon=106.848722;
       try{
        lat=Double.parseDouble(myLati2);
        lon=Double.parseDouble(myLongi2);
        }
       catch(Exception e){  lat=-6.192585; lon=106.848722;}

double jrk=getJarak(alat,along,lat,lon);
return jrk;
}


BuubleSORT:

   int j;
        boolean flag = true;  
        double temp=0.0;
        String stemp="";
        while ( flag ){
               flag= false;  
               for( j=0;  j < jd -1;  j++ ){
                      if ( dJarak[ j ] > dJarak[j+1] ) {
                              temp = dJarak[ j ];              
                              dJarak[ j ] = dJarak[ j+1 ];
                              dJarak[ j+1 ] = temp;
                             
                              stemp=arNama[j];
                              arNama[ j ] = arNama[ j+1 ];
                              arNama[ j+1 ] = stemp;
                             
                              stemp=arAlamat[j];
                              arAlamat[ j ] = arAlamat[ j+1 ];
                              arAlamat[ j+1 ] = stemp;
                             
                              stemp=arLati[j];
                              arLati[ j ] = arLati[ j+1 ];
                              arLati[ j+1 ] = stemp;
                             
                              stemp=arKet[j];
                              arKet[ j ] = arKet[ j+1 ];
                              arKet[ j+1 ] = stemp;
                             
                              stemp=arLongi[j];
                              arLongi[ j ] = arLongi[ j+1 ];
                              arLongi[ j+1 ] = stemp;
                             
                              flag = true;            
                      }
               }
         }


IMPLEMENTASI:

package com.example.damkar;



import java.io.IOException;
import java.util.List;
import java.util.Locale;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class listmenu2 extends Activity implements AdapterView.OnItemClickListener{
   private ListView listView1;
   String latitude,longitude,posisi,metode;
 
   int jd=0;
  String[]arLati;
  String[]arLongi;
  String[]arNama;
  String[]arAlamat;
  String[]arKet;
  double[]dJarak;

 listheadermenu []listheadericon_data;
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.listmenu);
       
       Intent io = this.getIntent();
       latitude=io.getStringExtra("latitude");
   longitude=io.getStringExtra("longitude");
   posisi=io.getStringExtra("posisi");
   metode=io.getStringExtra("metode");
 
   double lat=-6.423161;
       double lon=106.848722;
       try{
        lat=Double.parseDouble(latitude);
        lon=Double.parseDouble(longitude);
        }
       catch(Exception e){  lat=-6.192585; lon=106.848722;}
     
   
   jd=5;
   arLati=new String[jd];
   arLongi=new String[jd];
   arNama=new String[jd];
   arAlamat=new String[jd];
   arKet=new String[jd];
   dJarak=new double[jd];
       
   arNama[0]="Damkar Grand Depok City(021)77827280";
   arAlamat[0]="Jl.Boulevard Grand Depok City Depok";
   arLati[0]="-6.423161";
   arLongi[0]="106.828784";
   arKet[0]="StandBy 24 jam";
   dJarak[0]=hitJarak(lat,lon,arLati[0],arLongi[0]);
 
   arNama[1]="Damkar UPT Cimanggis Depok(021)87745313";
   arAlamat[1]="Jl.Raya Jakarta Bogor,Kecamatan Cimanggis Depok";
   arLati[1]="-6.388360";
   arLongi[1]="106.867236";
   arKet[1]="StandBy 24 jam";
   dJarak[1]=hitJarak(lat,lon,arLati[1],arLongi[1]);
 
   arNama[2]="Damkar UPT Cinere Depok(021)7543025";
   arAlamat[2]="Jl.Raya Cinere,Kecamatan Cinere Depok";
   arLati[2]="-6.345027";
   arLongi[2]="106.777972";
   arKet[2]="StandBy 24 jam";
   dJarak[2]=hitJarak(lat,lon,arLati[2],arLongi[2]);
 
   arNama[3]="Damkar UPT Bojong Sari Depok(021)28917777";
   arAlamat[3]="Jl.Raya Muchtar No.30,Kecamatan Bojong Sari Depok";
   arLati[3]="-6.401734";
   arLongi[3]="106.756683";
   arKet[3]="StandBy 24 jam";
   dJarak[3]=hitJarak(lat,lon,arLati[3],arLongi[3]);
 
   arNama[4]="Damkar UPT Margonda Depok(021)77212004  ";
   arAlamat[4]="Jl.Margonda Raya,No.54 Depok";
   arLati[4]="-6.379830";
   arLongi[4]="106.830844";
   arKet[4]="StandBy 24 jam";
   dJarak[4]=hitJarak(lat,lon,arLati[4],arLongi[4]);
 
        int j;
        boolean flag = true;  
        double temp=0.0;
        String stemp="";
        while ( flag ){
               flag= false;  
               for( j=0;  j < jd -1;  j++ ){
                      if ( dJarak[ j ] > dJarak[j+1] ) {
                              temp = dJarak[ j ];              
                              dJarak[ j ] = dJarak[ j+1 ];
                              dJarak[ j+1 ] = temp;
                             
                              stemp=arNama[j];
                              arNama[ j ] = arNama[ j+1 ];
                              arNama[ j+1 ] = stemp;
                             
                              stemp=arAlamat[j];
                              arAlamat[ j ] = arAlamat[ j+1 ];
                              arAlamat[ j+1 ] = stemp;
                             
                              stemp=arLati[j];
                              arLati[ j ] = arLati[ j+1 ];
                              arLati[ j+1 ] = stemp;
                             
                              stemp=arKet[j];
                              arKet[ j ] = arKet[ j+1 ];
                              arKet[ j+1 ] = stemp;
                             
                              stemp=arLongi[j];
                              arLongi[ j ] = arLongi[ j+1 ];
                              arLongi[ j+1 ] = stemp;
                             
                              flag = true;            
                      }
               }
         }

       
//    for(int i=0;i<jd;i++){
//     listheadericon_data[i]= new listheadermenu(R.drawable.map, arNama[i]);
//    }
 
   listheadericon_data = new listheadermenu[]{
           new listheadermenu(R.drawable.map, arNama[0]+" ("+String.valueOf(dJarak[0])+" Km)"),
        new listheadermenu(R.drawable.map, arNama[1]+" ("+String.valueOf(dJarak[1])+" Km)"),
             new listheadermenu(R.drawable.map, arNama[2]+" ("+String.valueOf(dJarak[2])+" Km)"),
             new listheadermenu(R.drawable.map, arNama[3]+" ("+String.valueOf(dJarak[3])+" Km)"),
             new listheadermenu(R.drawable.map, arNama[4]+" ("+String.valueOf(dJarak[4])+" Km)")
           };
     
       listitemmenu adapter = new listitemmenu(this,R.layout.listitemmenu, listheadericon_data);
       listView1 = (ListView)findViewById(R.id.listView1);

       View header = (View)getLayoutInflater().inflate(R.layout.listheadermenu, null);
       listView1.addHeaderView(header);
       listView1.setAdapter(adapter);
       listView1.setOnItemClickListener((OnItemClickListener) this);
     
//        TextView  txtMarquee=(TextView)findViewById(R.id.txtHeader);
//        txtMarquee.setSelected(true);
//        String kata="KLIK SALAH SATU MENU DIATAS UNTUK MENCARI TEMPAT WISATA DI YOGYAKARTA";
//        String POLA =getString(R.string.marquee);
//        String kalimat=String.format(POLA,TextUtils.htmlEncode(kata));
//         txtMarquee.setText(Html.fromHtml(kalimat));        

       
}



public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
position=position-1;
Intent  i= new Intent(listmenu2.this,rterdekat.class);
i.putExtra("nama", arNama[position]);
i.putExtra("alamat",arAlamat[position]);
i.putExtra("latitudeDamkar", arLati[position]);
i.putExtra("longitudeDamkar", arLongi[position]);
i.putExtra("ket", arKet[position]);
i.putExtra("latitude", latitude);
i.putExtra("longitude", longitude);
i.putExtra("posisi", posisi);
i.putExtra("jarak", String.valueOf(dJarak[position]));
startActivity(i);
}

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;

   double meterConversion = 1609;
   double myjr=dist * meterConversion;
   return Math.floor(myjr/1609);
   }

double hitJarak(double alat,double along,String myLati2,String myLongi2){

  double lat=-6.423161;
       double lon=106.848722;
       try{
        lat=Double.parseDouble(myLati2);
        lon=Double.parseDouble(myLongi2);
        }
       catch(Exception e){  lat=-6.192585; lon=106.848722;}

double jrk=getJarak(alat,along,lat,lon);
return jrk;
}

}


+++++++++++++++++++
listmenu.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </ListView>
   
</LinearLayout>


listitemmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="1dp"
    >
 
     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp" />
     
     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="16dp"
        android:textColor="#000000"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="5dp" />
     
</LinearLayout>




Tidak ada komentar:

Posting Komentar