开发者

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜