android定位的速度方向如图所示,在Android中计算罗盘方位/前往位置

在此指南针上的箭头显示从您的位置到Kaaba的方向(目的地位置)

你可以通过这种方式简单地使用bearing.bearing将给你从你的位置到目的地的直接角度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 

bearTo = bearTo + 360;

//bearTo = -100 + 360  = 260;}

你需要实现SensorEventListener及其函数(onSensorChanged,onAcurracyChabge)并写入onSensorChanged中的所有代码

完整的代码在这里为Qibla指南针方向public class QiblaDirectionCompass extends Service implements SensorEventListener{

public static ImageView image,arrow;// record the compass picture angle turnedprivate float currentDegree = 0f;private float currentDegreeNeedle = 0f;Context context;Location userLoc=new Location("service Provider");// device sensor managerprivate 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 capabilitiesthis.context =context;@Overridepublic 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();}@Overridepublic void onDestroy() {

mSensorManager.unregisterListener(this);Toast.makeText(context, "Destroy", Toast.LENGTH_SHORT).show();

super.onDestroy();}@Overridepublic void onSensorChanged(SensorEvent sensorEvent) {Location destinationLoc = new Location("service Provider");destinationLoc.setLatitude(21.422487); //kaaba latitude settingdestinationLoc.setLongitude(39.826206); //kaaba longitude settingfloat 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 northif (bearTo 

bearTo = bearTo + 360;

//bearTo = -100 + 360  = 260;}//This is where we choose to point itfloat direction = bearTo - head;// If the direction is smaller than 0, add 360 to get the rotation clockwise.if (direction 

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 placera.setDuration(210);// set the animation after the end of the reservation statusra.setFillAfter(true);// Start the animationimage.startAnimation(ra);currentDegree = -degree;}@Overridepublic void onAccuracyChanged(Sensor sensor, int i) {}@Nullable@Overridepublic IBinder onBind(Intent intent) {

return null;}

xml代码在这里<?xml  version="1.0" encoding="utf-8"?>

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值