在这罗盘上的箭头显示从你的位置到天房的方向( 目的地位置 )
你可以简单的使用bearingTo,这样就可以给你从你的位置到目的地的直接angular度
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的范围以获得正确的旋转。
这是我们真正想要的东西,比较什么方向给我们
+-----------+--------------+ | bearingTo | Real bearing | +-----------+--------------+ | 0 | 0 | +-----------+--------------+ | 90 | 90 | +-----------+--------------+ | 180 | 180 | +-----------+--------------+ | -90 | 270 | +-----------+--------------+ | -135 | 225 | +-----------+--------------+ | -180 | 180 | +-----------+--------------+
所以我们必须在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及其函数(onSensorChanged,onAcurracyChabge)并将所有代码写入onSensorChanged
完整的代码是在这里为朝拜指南针的方向
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) { 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代码在这里