使用方向传感器指向特定位置 [英] Using orientation sensor to point towards a specific location

查看:141
本文介绍了使用方向传感器指向特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个使用方向传感器的箭头,以指向特定位置. Google地方信息会在找到的每个位置的ListView中实现此箭头.

I'm trying to implement an arrow that uses the orientation sensor in order to point towards a specific location. Google Places implements this arrow in a ListView for each place it finds.

我已经设法获得了方位角,但是在给定位置的情况下,我不知道如何继续计算所需的角度.此外,我需要从实北和磁北进行转换.有人有这样的实现示例吗?

I've managed to get the azimuth, but given a location, I don't know how to proceed to calculate the angle I need. Moreover, I need to make the conversions from real north and magnetic north. Does anybody have an example of such implementation?

提前谢谢.

推荐答案

我解决了.

float azimuth = // get azimuth from the orientation sensor (it's quite simple)
Location currentLoc = // get location from GPS or network
// convert radians to degrees
azimuth = Math.toDegrees(azimuth);
GeomagneticField geoField = new GeomagneticField(
             (float) currentLoc.getLatitude(),
             (float) currentLoc.getLongitude(),
             (float) currentLoc.getAltitude(),
             System.currentTimeMillis());
azimuth += geoField.getDeclination(); // converts magnetic north to true north
float bearing = currentLoc.bearingTo(target); // (it's already in degrees)
float direction = azimuth - bearing;

如果要绘制箭头或其他指向该方向的箭头,请使用canvas.rotate(-direction).我们传递一个否定参数,因为画布旋转是逆时针方向.

If you're going to draw an arrow or something else to point to the direction, use canvas.rotate(-direction). We pass a negative argument because canvas rotations are anti-clockwise.

这篇关于使用方向传感器指向特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆