Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using orientation sensor to point towards a specific location

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?

Thanks in advance.

like image 371
Flávio Faria Avatar asked Mar 29 '11 22:03

Flávio Faria


People also ask

How does an orientation sensor work?

The system computes the orientation angles by using a device's geomagnetic field sensor in combination with the device's accelerometer. Using these two hardware sensors, the system provides data for the following three orientation angles: Azimuth (degrees of rotation about the -z axis).

Which sensor is used to identify the position of the device during a call?

Explanation: A proximity sensor is a sensor able to detect the presence of nearby objects without any physical contact. A proximity sensor often emits an electromagnetic field or a beam of electromagnetic radiation, and looks for a change in the return signal. 2.

What is screen orientation sensor?

The screenOrientation is the attribute of activity element. The orientation of android activity can be portrait, landscape, sensor, unspecified etc. You need to define it in the AndroidManifest. xml file.

What is position sensor in Android?

Position sensors are useful for determining a device's physical position in the world's frame of reference. For example, you can use the geomagnetic field sensor in combination with the accelerometer to determine a device's position relative to the magnetic North Pole.


1 Answers

I solved it.

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;

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.

like image 75
Flávio Faria Avatar answered Sep 30 '22 19:09

Flávio Faria