Setiap titik dibumi dapat dinyatakan
dalam longtitude dan latitude
rumus untuk menghitung adalah
cos(y) =
cos(x)cos(y)+sin(x)sin(z)cos(B)
cos(z)=
cos(x)cos(y)+sin(x)sin(y)cos(C)
sin(x)/sin(A)=sin(y)/sin(B)=sin(z)/sin(C)
dari tiga rumus diatas di gabungin jadi
tan(B) =
sin(C)/sin(x)cot(y)-cos(a)cos(C)
C = Bx-By
x = 90 – Ly
y = 90 – Lx
dan
cos(90-x)=sin(x)
sin(90-x)=cos(x)
cot(90-x)=tan(x)
maka persamaan akan menjadi
tan(B)=sin(Bx-By)/cos(Ly)tan(Lx)
sin(Ly)cos(Bx By)
sudut B = archtan(tan(B))
titik A = posisi kita
titik B = posisi ka'bah
titik C = kutub utara
Arah kiblat azimuth ditunjukan sudut B.
secara matematis penghitungan arah kiblat seperti ini
secara program seperti ini
MainActivity.java
package com.baunAndroid;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "Compass";
private SensorManager mSensorManager;
private Sensor mSensor;
private SampleView mView;
private float[] mValues;
private double lonMosque;
private double latMosque;
private LocationManager lm;
private LocationListener loclistenD;
//for find north direction
private final SensorEventListener mListener = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy){
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mView = new SampleView(this);
setContentView(mView);
//calling 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 ka'bah location
private double QiblaCount(double lngMasjid,double latMasjid){
double lngKabah= 39.82616111;
double latKabah= 21.42250833;
double lKlM= (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(){
super.onResume();
mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME);
}
@Override
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);
// TODO Auto-generated constructor stub
mPath.moveTo(0, -50);
mPath.lineTo(20, 60);
mPath.lineTo(0, 50);
mPath.lineTo(-20, 60);
mPath.close();
}
//Make arrow for pointing direction
@Override
protected void onDraw(Canvas canvas){
Paint paint = mPaint;
canvas.drawColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setColor(Color.DKGRAY);
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 Qibla = 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;
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow(){
mAnimate = false;
super.onDetachedFromWindow();
}
}
private class DispLocListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
//update TextViews
latMosque = loc.getLatitude();
lonMosque = loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
manifestnya
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baunAndroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ketika di launch akan keluar
run dengan menekan ctrl+f11