使用定位服务android检测设备是否已转向 [英] Detect if device has taken a turn using location service android

查看:90
本文介绍了使用定位服务android检测设备是否已转向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测用户在使用Android手机上的传感器行驶时是否在转弯处.我该如何编码?我正在从所有传感器(加速度计,位置,旋转,地磁)实时收集数据,并将其存储在SD卡中.所以现在我只想知道用户是否已转过弯,以及他转过的方向.

I want to detect if the user has taken a turn on the road while driving using the sensors on the android phone. How do I code this? I am collecting data live from all the sensors(accelerometer,location,rotation,geomagnetic) and storing them on the sd card. So now i just want to know whether the user has a taken a turn and in which direction he has turned.

推荐答案

我假定传感器的注册正确完成.您可以通过以下方式使用方向传感器(不推荐使用)来检测方向:

I assume the registration of the sensor is done properly. You can detect the direction by using the orientation sensor (deprecated) as follows:

@Override
public void onSensorChanged(SensorEvent event) {

    float azimuth_angle = event.values[0];
    int precision = 2;

    if (prevAzimuth - azimuth_angle < precision * -1)
        Log.v("->", "RIGHT");

    else if (prevAzimuth - azimuth_angle > precision)
        Log.v("<-", "LEFT");

    prevAzimuth = azimuth_angle;

}

注意:变量"prevAzimuth"被声明为全局变量.您可以将精度"值更改为所需的任何值.我们需要此值,因为我们不希望在每次琐碎的方位角变化后都看到输出.但是,太大的精度将导致不精确的结果.对我来说,"2"是最佳选择.

Note: The variable of "prevAzimuth" is declared as global. You can change "precision" value to whatever you want. We need this value because we do not want to see output after each trivial change in azimuth angle. However, too large precision gives imprecise results. To me, "2" is optimum.

这篇关于使用定位服务android检测设备是否已转向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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