在此指南针上的箭头显示从您的位置到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"/>

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

  1. android与ios ui切图关系,iOS、Android 开发单位换算及 UI 切图要求

    在移动端 UI 设计中,经常会用到的单位有 4 种:px.pt.dp 和 sp,很多人分辨不清这几种单位及其换算关系,以及 iOS 和 Android 的切图要求,我在这里做下简单的介绍,希望大家读完 ...

  2. 百度android定位 602 key mcode不匹配,我的Android进阶之旅------百度地图学习:BDLocation.getLocType ( )值分析...

    BDLocation类,封装了定位SDK的定位结果,在BDLocationListener的onReceive方法中获取.通过该类用户可以获取error code,位置的坐标,精度半径等信息.具体方法 ...

  3. android技术分享方向,android开发分享在Android中计算指南针方位/标题到位置

    在这罗盘上的箭头显示从你的位置到天房的方向( 目的地位置 ) 你可以简单的使用bearingTo,这样就可以给你从你的位置到目的地的直接angular度 Location userLoc=new Lo ...

  4. android定位:获取当前位置的经纬度

    Android定位主要使用的是基于位置服务(Location Based Service)技术,有了 Android 系统作为载体,我们可以利用定位出的位置进行许多丰富多彩的操作,比如定位城市,根据我 ...

  5. 一台无线路由器通过网线连接台式计算机最多可以连几台计算机,如图所示,无线路由器是一种支持有线和无线连接的网络设备.可以通过后排接口同时连接多台电脑实现有线连接,则各接口之间是...

    题目所在试卷参考答案: 2016年湖北省咸宁市中考物理试卷 参考答案与试题解析 一.选择題(本大题共10小题,每小题2分,共20分) 1.某校组织学生参加体育中考.以下记录的部分体检数据中,不合理的是 ...

  6. 基于百度地图实现Android定位功能实现

    基于百度地图实现Android定位功能实现 初始化 一>获取SHA1 二>Android studio 配置 (1)下载百度地图sdk (2)解压安装包 (3)回到刚刚配置的jar包,右击 ...

  7. android博客导航总结,以及个人常用android免费学习干货(文章,视频,矢量图,字体等)资源分享?

    android博客导航总结与资源分享 1.博客总结 1.mob平台的使用 2.QMUI部分功能实现 3.XUI部分功能实现 4.其他功能 5.Flutter博客总结: 2.资源分享 1.文章类型资源分 ...

  8. 基于Android平台的多分辨率解决方案[图]

    为什么80%的码农都做不了架构师?>>>    摘要:近年来,智能手机的功能越来越强大,移动终端应用程序层出不穷,移动互联网改变人们的生活.Android系统是开放手机联盟推出的一款 ...

  9. 设无向图g如图所示_阿里重磅发布大规模图神经网络平台 AliGraph,架构算法解读...

    图神经网络 (GNN) 主要是利用神经网络处理复杂的图数据,它将图数据转换到低维空间,同时最大限度保留结构和属性信息,并构造一个用于训练和推理的神经网络.在实际应用中,为了加速 GNN 训练和新算法的 ...

最新文章

  1. Java Script 第四节课 Java Script的隐式转换
  2. Android消息推送完美解决方案全析
  3. 中国平安:杀进智能合约,你怕不怕?
  4. 史上比较用心的纯代码实现 AutoLayout
  5. 学的php毫无兴趣,培训班学PHP,感觉兴趣越来越低,哎,真的很迷茫了!
  6. java jsp if else if_jsp页面使用if else语句 | 学步园
  7. 那些不是秘密的微信earning方法
  8. Rendering Linear lighting and color
  9. java jtextfield 事件_java – 处理JTextField中的编辑事件
  10. linux文件定时备份到windows,Linux和windows定时备份数据到百度云盘
  11. powershell excel 导入 sqlserver
  12. 20165237 2017-2018-2 《Java程序设计》第8周学习总结
  13. CMMI3认证升级CMMI5认证需满足的基本条件
  14. LiquidCrystal_I2C 显示不正常 只显示第一个首字符!
  15. Base-calling for next-generation sequencing platforms (译文)
  16. PHP小白编程学习——第三方登录功能设计思维
  17. 爬虫中requests高级用法(带上cookie做数据请求)
  18. 只有程序员才能读懂的西游记
  19. Windows7下Foxmail不能使用问题解决一例
  20. 中国香茅醇行业研究与投资预测报告(2022版)

热门文章

  1. kmeans算法及其改进算法K-means++,ISODATA和Kernel K-means
  2. 《人力资源管理概论》
  3. TrustZone 基本信息介绍大全
  4. js数组常用方法(19种)|你会的到底有多少呢?
  5. python数值类型有哪四个直辖市_博文分类
  6. 迟了太久,就不必到了
  7. docker-compose部署xxl-job-admin 分布式任务调度平台
  8. 金仓数据库KingbaseES行列转换
  9. CPU渲染与GPU渲染的优劣,教你选择合适的渲染方式
  10. 计算机主机系统管理员口令,电脑管理员密码忘了怎么办_忘记了电脑管理员密码怎么办...