在这罗盘上的箭头显示从你的位置到天房的方向( 目的地位置 )

你可以简单的使用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代码在这里

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

  1. 分享一些Java开发人员在编程中最容易踩雷的地方!

    身为一名Java从业人员,其职场生涯就是一边踩"坑",一边上升的过程.这个过程中不仅要学会修改无数bug,也要学会越过很多"坑".今天,小千为大家分享一些Jav ...

  2. “消失”的Android技术博主们现在如何!Android开发者前路在哪?

    作者:qing的世界 链接:https://juejin.im/post/5e93e305f265da48076dfce3 概述 想了很久怎么样可以让文章的标题不那么悲观,但是各种文案都在我脑海里面不 ...

  3. 学习Android从0开始之开发工具篇-Android studio详解

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android studio详解 我们古人又云:工欲善其事,必先利其器. 1.android studio的背景 Android Studio 是 ...

  4. android音乐播放器的开发与设计,Android音乐播放器的设计与实现

    内容简介: Android音乐播放器的设计与实现,毕业论文,共21页,7729字. 摘要:本文主要介绍了一个基于Andriod的音乐播放器的设计与实现.主要包括可行性分析,需求分析,App功能设计及实 ...

  5. 物联网技术 android,基于Android技术的物联网应用开发

    本书从Android开发一般过程与方法出发,以Java语言为媒介,重点讲述Android开发的核心技术和在物联网上的应用.本教材的特色是以项目实践作为主线贯穿其中,提供了完整的物联网项目案例,力求使读 ...

  6. 先进技术android,React Native实战(JavaScript开发iOS和Android应用)/计算机科学先进技术译丛...

    导语 内容提要 本书作者Nader Dabit是AWS Mobile开发人员.React Native Training创始人和React Native Radio播客主持人.本书旨在帮助iOS.An ...

  7. 基于Android技术的物联网应用开发-Android studio

    2021.10.31 清空草稿箱

  8. 数据库开发技术java方向_Java开发工程师(Web方向) - 03.数据库开发 - 第5章.MyBatis...

    第5章--MyBatis MyBatis入门 Abstract: 数据库框架的工作原理和使用方法(以MyBatis为例) 面向对象的世界与关系型数据库的鸿沟: 面向对象世界中的数据是对象: 关系型数据 ...

  9. android项目添加图片,安卓开发:往项目中添加并使用图片

    一 往项目中添加图片文件 (1)iOS项目 在iOS项目中,如果往项目中添加的图片是png格式的,就是往Assets.xcassets文件夹中添加. 如果不是png格式,比如jpg格式的图片,就不能往 ...

最新文章

  1. CCNP路由实验---12、配置分发列表和被动接口
  2. c++判断一个数字是否为buzz number的算法(附完整源码)
  3. hashmap(1.8)
  4. RHEL5下的DHCP服务和DHCP中继
  5. Eigen入门之密集矩阵 2-- Matrix及Vector的计算方法
  6. C语言第三次博客作业---单层循环结构
  7. Android对性能要求,Android性能管理
  8. SQL Server 2014如何提升非在线的在线操作
  9. 区块链安全 - DAO攻击事件解析
  10. Unity(TransForm)
  11. mysql 增删改查操作笔记
  12. 班级抽签小程序——项目总结
  13. 优启通如何写入linux启动盘,U盘启动盘制作工具|优启通图文详细使用教程,优启通使用教程_东坡下载手机版...
  14. 微信小程序项目实例SSM在线考试管理系统+后台
  15. 字节跳动内推码 春招 社招
  16. BIP与Siebel系统集成
  17. 东莞塘厦适合团建的农家乐|团建出游好去处|休闲游玩好地方
  18. win产品密钥大搜集
  19. 2021-11-15 入门前端VScode必装的常用插件, HTML+CSS常用属性速记.
  20. 2022年,我加入了微软MVP大家庭

热门文章

  1. C语言之素数判断及输出(1~100所有的素数)
  2. JAVA程序之选举班长
  3. 区块链+:颠覆互联网+模式不是事!
  4. PPC r0-r31寄存器介绍
  5. 数据库设计之实施与维护
  6. 前端使用sockJs进行聊天通讯的功能
  7. 中学计算机教育论文,【中学计算机教育互动式培养策略计算机教育论文材料】...
  8. c语言中1120是什么错误,C++编译常见问题汇总(二)fatal error LNK1120 错误
  9. Java实现数据库敏感信息脱敏方案
  10. ppt图片设计素材下载网站搭建模板