0

私はイスラムのアンドロイドアプリに実装したいと思う普遍的なqiblaの方向ロジックを知りたいと思っています。私はちょうどユーザーの緯度と経度を取得しています。緯度、経度にかかわらず、英国、米国、インド、スリランカなどどこにいてもその人のqiblaの方向を見つけたいと思います。qiblaの方向を見つけるためのユニバーサルロジック

私は、この公式を発見し、それがどのように動作するかを正確に知っていない:

float lonDelta = (lon2 - lon1); 
float y = Math.sin(lonDelta) * Math.cos(lat2); 
float x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lonDelta); 
float brng = Math.atan2(y, x).toDeg(); 

正確な式を知るには、私を助けてください。

答えて

0

正確なqiblaの方向を取得するには、次の方法を使用します。

public double calculateQibla(double latitude, double longitude){ 
    double phiK = 21.4 * Math.PI/180.0; 
    double lambdaK = 39.8 * Math.PI/180.0; 
    double phi = latitude*Math.PI/180.0; 
    double lambda = longitude*Math.PI/180.0; 
    double psi = 180.0/Math.PI * Math.atan2(Math.sin(lambdaK-lambda),Math.cos(phi)*Math.tan(phiK)-Math.sin(phi)*Math.cos(lambdaK-lambda)); 
    return Math.round(psi); 
} 

希望します。

+0

どのような値を返さないここですか? –

+0

場合は、Kaabaにベアリングを返します。 – jack

0

とにかくユーザーの場所を特定する必要があるため、Locationオブジェクトがあります。

オブジェクトは、LocationオブジェクトをパラメータとするbearingTo()メソッドを持ちます。それはメッカのKaabaの座標でなければなりません。 (私が理解してきたように。)

あなたはそれが好きで使用することができます。

Location kaabaLocation = new Location(""); 
kaabaLocation.setLatitude(21.4225); 
kaabaLocation.setLongitude(39.8262); 
float kaabaBearing = userLocation.bearingTo(kaabaLocation); 

ので、Androidはすでに数学が組み込まれてい

1

の代わりに、この式

float lonDelta = (lon2 - lon1); 
float y = Math.sin(lonDelta) * Math.cos(lat2); 
float x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lonDelta); 
float brng = Math.atan2(y, x).toDeg(); 

を使用してあなたはこのように簡単にベアリングを使うことができます。

Location userLoc=new Location("service Provider"); 
    //get longitudeM Latitude and altitude of current location with gps class and set in userLoc 
    userLoc.setLongitude(longitude); 
    userLoc.setLatitude(latitude); 
    userLoc.setAltitude(altitude); 

    Location destinationLoc = new Location("service Provider"); 
    destinationLoc.setLatitude(21.422487); //kaaba latitude setting 
    destinationLoc.setLongitude(39.826206); //kaaba longitude setting 
    float bearTo=userLoc.bearingTo(destinationLoc); 

bearingToは-180〜180の範囲を指定しますが、これは少し混乱させます。正しい回転を得るには、この値を0から360の範囲に変換する必要があります。

これは私たちがbearTo

// If the bearTo is smaller than 0, add 360 to get the rotation clockwise. 

    if (bearTo < 0) { 
    bearTo = bearTo + 360; 
    //bearTo = -100 + 360 = 260; 
} 

後にこのコードを追加する必要がありますがSensorEventListenerを実装する必要がbearingToは私たちに

+-----------+--------------+ 
| bearingTo | Real bearing | 
+-----------+--------------+ 
| 0   | 0   | 
+-----------+--------------+ 
| 90  | 90   | 
+-----------+--------------+ 
| 180  | 180   | 
+-----------+--------------+ 
| -90  | 270   | 
+-----------+--------------+ 
| -135  | 225   | 
+-----------+--------------+ 
| -180  | 180   | 
+-----------+--------------+ 

を与えるものに比較して、私たちは本当に何をしたいのテーブルで、その機能(onSensorChanged、onAcurracyChabge)とonSensorChanged内のすべてのコードを書きます

完全なコードはここにありますo Fキブラコンパス

public class QiblaDirectionCompass extends Service implements SensorEventListener{ 
public static ImageView image,arrow; 

// record the compass picture angle turned 
private float currentDegree = 0f; 
private float currentDegreeNeedle = 0f; 
Context context; 
Location userLoc=new Location("service Provider"); 
// device sensor manager 
private static SensorManager mSensorManager ; 
private Sensor sensor; 
public static TextView tvHeading; 
    public QiblaDirectionCompass(Context context, ImageView compass, ImageView needle,TextView heading, double longi,double lati,double alti) { 

    image = compass; 
    arrow = needle; 


    // TextView that will tell the user what degree is he heading 
    tvHeading = heading; 
    userLoc.setLongitude(longi); 
    userLoc.setLatitude(lati); 
    userLoc.setAltitude(alti); 

    mSensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE); 
    sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 
    if(sensor!=null) { 
     // for the system's orientation sensor registered listeners 
     mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);//SensorManager.SENSOR_DELAY_Fastest 
    }else{ 
     Toast.makeText(context,"Not Supported", Toast.LENGTH_SHORT).show(); 
    } 
    // initialize your android device sensor capabilities 
this.context =context; 
@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    Toast.makeText(context, "Started", Toast.LENGTH_SHORT).show(); 
    mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME); //SensorManager.SENSOR_DELAY_Fastest 
    super.onCreate(); 
} 

@Override 
public void onDestroy() { 
    mSensorManager.unregisterListener(this); 
Toast.makeText(context, "Destroy", Toast.LENGTH_SHORT).show(); 

    super.onDestroy(); 

} 
@Override 
public void onSensorChanged(SensorEvent sensorEvent) { 

float degree = Math.round(sensorEvent.values[0]); 
float head = Math.round(sensorEvent.values[0]); 
float bearTo; 
Location destinationLoc = new Location("service Provider"); 

destinationLoc.setLatitude(21.422487); //kaaba latitude setting 
destinationLoc.setLongitude(39.826206); //kaaba longitude setting 
float bearTo=userLoc.bearingTo(destinationLoc); 

    //bearTo = The angle from true north to the destination location from the point we're your currently standing.(asal image k N se destination taak angle) 

    //head = The angle that you've rotated your phone from true north. (jaise image lagi hai wo true north per hai ab phone jitne rotate yani jitna image ka n change hai us ka angle hai ye) 



GeomagneticField geoField = new GeomagneticField(Double.valueOf(userLoc.getLatitude()).floatValue(), Double 
     .valueOf(userLoc.getLongitude()).floatValue(), 
     Double.valueOf(userLoc.getAltitude()).floatValue(), 
     System.currentTimeMillis()); 
head -= geoField.getDeclination(); // converts magnetic north into true north 

if (bearTo < 0) { 
    bearTo = bearTo + 360; 
    //bearTo = -100 + 360 = 260; 
} 

//This is where we choose to point it 
float direction = bearTo - head; 

// If the direction is smaller than 0, add 360 to get the rotation clockwise. 
if (direction < 0) { 
    direction = direction + 360; 
} 
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees"); 

RotateAnimation raQibla = new RotateAnimation(currentDegreeNeedle, direction, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
raQibla.setDuration(210); 
raQibla.setFillAfter(true); 

arrow.startAnimation(raQibla); 

currentDegreeNeedle = direction; 

// create a rotation animation (reverse turn degree degrees) 
RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 

// how long the animation will take place 
ra.setDuration(210); 


// set the animation after the end of the reservation status 
ra.setFillAfter(true); 

// Start the animation 
image.startAnimation(ra); 

currentDegree = -degree; 
} 
@Override 
public void onAccuracyChanged(Sensor sensor, int i) { 

} 
@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

XMLコードは、それが最終的に

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/flag_pakistan"> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/heading" 
    android:textColor="@color/colorAccent" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="100dp" 
    android:layout_marginTop="20dp" 
    android:text="Heading: 0.0" /> 
<RelativeLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/heading" 
android:scaleType="centerInside" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true"> 

<ImageView 
    android:id="@+id/imageCompass" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="centerInside" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/images_compass"/> 

<ImageView 
    android:id="@+id/needle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:scaleType="centerInside" 
    android:src="@drawable/arrow2"/> 
</RelativeLayout> 
</RelativeLayout> 
+0

tvHeading.setText( "見出し:" + Float.toString(度)+ "度");学位は何ですか?あなたはどこにでも宣言していない! – Husnain

+0

コードが編集されました。 –

+0

Kashif Bro、どこでQiblaDirectionCompasを呼び出しましたか?何かお見逃しですか?または他のclas ??もしそうなら..それではこちらも投稿してください。 –

関連する問題