Jean Allen
Jean Allen

Reputation: 63

Get Degree From current location to the destination location Android

Good day.I have been told to make an app which will show some logo on the camera surface as soon as the camera is 100 metres away from the destination.I did it and now the problem is that if i turn backward to the building,the logo will still appear as im 100 metres away,and camera can`t guess whether im facing the right company or not.So i thought about bearing and degree of two locations in android.The idea is to have the angle between my location and the destination location and as soon as angle reaches 0.0 it means that user facing the right location so i could show it.But looking at LOCATION class code with bearingTo method i could not really understand what does it gives back.My question is how can i implement such logic?Does bearingTo gives the angle i should rotate myself from my current location up to destination location to face it straight,or it is giving somehow different logic?If so what should i use?

Upvotes: 0

Views: 1525

Answers (1)

Markus Kauppinen
Markus Kauppinen

Reputation: 3235

The bearingTo() method just returns the bearing (compass direction) between two locations: the one on which you are calling it and the one you give as the parameter. Actually nothing in the Location object or LocationManager knows anything about your devices orientation i.e. where the camera is pointing.

You'll need use the compass (magnetometer together with the accelerometer) to know the device's orientation in relation to the earth's surface i.e. to know where the camera is pointing.

So with bearingTo() you can calculate the "right direction" and with the compass you get the "current direction" and you can then compare those two or even tell the user which way to turn if he/she is supposed to point at some specific building. (Just handle the 359.999 ... 0.000 degree border correctly.)

Getting the compass heading is a little bit elaborate so it's better to try to find a nice tutorial to follow. Here's is one decent implementation. Or you can dig deeper into Android's sensors and some matrix calculations and plow through the Android documentation.

Or you could of course search Stack Overflow for older discussions about getting a compass heading and find for example this code snipped from a mathematician.

Upvotes: 1

Related Questions