Header Ads

Tutorial Membuat Aplikasi Arah Kiblat Sederhana | Tutorial Android

Untuk tutorial kali ini kita akan mencoba membuat aplikasi arah kiblat sederhana untuk android. Yang sebenarnya sudah saya gunakan dibeberapa aplikasi. Tapi untuk yang cuman aplikasi arah kiblat aja kebetulan barusan saya upload ke Google Play, linknya ada disini : Simple Qibla Locator.

Bagaimana cara membuatnya? Silahkan ikuti step by step tutorialnya :

1. Buat projek baru dengan nama 'SimpleQiblaLocator' atau terserah teman-teman.
2. Buat kode program pada 'MainActivity.java' dan jangan lupa masukkan background di folder res -> Drawable : bg_kiblat3.png karena kebetulan pada projek ini background-nya saya namain seperti itu di kodingannya.

importandroid.app.Activity;
import android.location.Criteria;
importandroid.location.Location;
importandroid.location.LocationListener;
importandroid.location.LocationManager;
import android.os.Bundle;
importandroid.content.Context;
import android.content.pm.LabeledIntent;
import android.content.res.Resources;
import android.graphics.*;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
import android.util.Config;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class Kiblat extends Activity {

    private static final String TAG = "Compass";
   
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private SampleView kiblat;
    private float[] mValues;
    private double lonMosque;
       private double latMosque;
       private LocationManager lm;
    private LocationListener locListenD;

    //for finding north direction
    private final SensorEventListener mListener = newSensorEventListener() {
        public voidonSensorChanged(SensorEvent event) {
            if (Config.DEBUG) Log.d(TAG,
                     "sensorChanged ("+ event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");
            mValues = event.values;
            if (kiblat != null) {
                kiblat.invalidate();
            }
        }

        public voidonAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

       @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        kiblat = new SampleView(this);
        setContentView(kiblat);
       
        // for calling the gps
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location loc = lm.getLastKnownLocation("gps");
       
     // ask the Location Manager to send us location updates
        locListenD = new DispLocListener();
        lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);
       
        locListenD = new DispLocListener();
        lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);       
       }     
  
       //finding kabahlocation
    private double QiblaCount(double lngMasjid, double latMasjid) {
       doublelngKabah=39.82616111;
              doublelatKabah=21.42250833;
              doublelKlM=(lngKabah-lngMasjid);
              double sinLKLM= Math.sin(lKlM*2.0*Math.PI/360);
              double cosLKLM= Math.cos(lKlM*2.0*Math.PI/360);
              double sinLM = Math.sin(latMasjid *2.0 * Math.PI/360);
              double cosLM =  Math.cos(latMasjid *2.0 * Math.PI/360);
              double tanLK = Math.tan(latKabah*2*Math.PI/360);
              double denominator= (cosLM*tanLK)-sinLM*cosLKLM;
             
              double Qibla;
              double direction;
             
              Qibla = Math.atan2(sinLKLM, denominator)*180/Math.PI;
              direction= Qibla<0 ? Qibla+360 : Qibla;
              return direction;
      
       }

    //resume location update when we are resume
       @Override
    protected void onResume()
    {
        if (Config.DEBUG) Log.d(TAG, "onResume");
        super.onResume();

        mSensorManager.registerListener(mListener, mSensor,
                SensorManager.SENSOR_DELAY_GAME);
    }

       //stop location update when we are stop
    @Override
    protected void onStop()
    {
        if (Config.DEBUG) Log.d(TAG, "onStop");
        mSensorManager.unregisterListener(mListener);
        super.onStop();
    }

    private class SampleView extends View {
        private Paint   mPaint = new Paint();
        private Path    mPath = new Path();
        private boolean mAnimate;
             

        public SampleView(Context context) {
            super(context);

            // Construct a wedge-shaped path
            mPath.moveTo(0, -100);
            mPath.lineTo(20, 120);
            mPath.lineTo(0, 100);
            mPath.lineTo(-20, 120);
            mPath.close();
        }

        //make an arrow for pointing direction
       
        protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;
           
            this.setBackgroundResource(R.drawable.bg_kiblat3);
           
            paint.setAntiAlias(true);
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);

            int w = canvas.getWidth();
            int h = canvas.getHeight();
            int cx = w / 2;
            int cy = h / 2;
           float Qibla=(float) QiblaCount(lonMosque,latMosque);
         //   float Qiblat = mValues [0] + Qibla;
            canvas.translate(cx, cy);
            if (mValues != null) {
                canvas.rotate(-(mValues[0]+ Qibla));
            }
            canvas.drawPath(mPath, mPaint);
        }

        @Override
        protected void onAttachedToWindow() {
            mAnimate = true;
            if (Config.DEBUG) Log.d(TAG, "onAttachedToWindow. mAnimate=" + mAnimate);
            super.onAttachedToWindow();
        }

        @Override
        protected voidonDetachedFromWindow() {
            mAnimate = false;
            if (Config.DEBUG) Log.d(TAG, "onDetachedFromWindow. mAnimate=" + mAnimate);
            super.onDetachedFromWindow();
        }
   
            }
   
    private class DispLocListener implements LocationListener {
       public voidonLocationChanged(Location loc) {
       // update TextViews
        latMosque = loc.getLatitude();
        lonMosque = loc.getLongitude();
       }
      
       public voidonProviderDisabled(String provider) {
       }
      
       public voidonProviderEnabled(String provider) {
       }
      
       public voidonStatusChanged(String provider, int status, Bundle extras) {
       }
    }
}


3. Jangan lupa tambahin Permission di AndroidManifest.xml

   <uses-permission android:name="android.permission.ACCESS_LOCATION"/>
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
   <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

   <uses-permission android:name="android.permission.INTERNET" />

4. RUN dan lihat hasilnya





No comments

Powered by Blogger.