如何找到朝拜方向?任何简单的示例代码?

时间:2019-06-20 13:46:40

标签: java android

我想应用Qibla Direction,但找不到任何好的答案。我想在片段中应用的主要内容。我有一个QiblaDirectionCompass类,但是我不知道如何在Fragment中使用。

公共类QiblaDirectionCompass扩展了Service实现的SensorEventListener {     公共静态ImageView图像,箭头;

// 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;

    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) {

        mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);//SensorManager.SENSOR_DELAY_Fastest
    } else {
        Toast.makeText(context, "Not Supported", Toast.LENGTH_SHORT).show();
    }

    this.context = context;
}

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

@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);
    destinationLoc.setLongitude(39.826206); 
    bearTo=userLoc.bearingTo(destinationLoc);

    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;
    }

    float direction = bearTo - head;

    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;

    RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    ra.setDuration(210);


    ra.setFillAfter(true);

    image.startAnimation(ra);

    currentDegree = -degree;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}

请告诉我如何在Fragment中找到Qibla(Kaaba)方向。

0 个答案:

没有答案