Detect if device has taken a turn using location s

2019-06-10 06:43发布

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.

2条回答
smile是对你的礼貌
2楼-- · 2019-06-10 07:09

If you are tracking location coordinates, you can also track shifts between the angle from previous locations.

angle = arctan((Y2 - Y1) / (X2 - X1)) * 180 / PI

See this answer for calculating x and y.

Decision to use sensor values is based on an unrealistic assumption that the device is never rotated with respect to the vehicle.

查看更多
smile是对你的礼貌
3楼-- · 2019-06-10 07:11

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;

}

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.

查看更多
登录 后发表回答