-1

I'm brand new to coding apps. I have an idea. Does anyone have a code snip for pointing the user towards a pre programed geographical point?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

You can calculate the bearing between two locations by the following code:

double x = Math.cos(lat2) * Math.sin(lng1-lng2);
double y = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lng1-lng2);
double bearing = Math.atan2(x, y);

The problem is, that you need know the current heading of the phone, too. The codesnipped only calculates the bearing relative to the geographic north.

You could use my CompassAssistant class. It has the bearing functionality you need. The howto is in the readme. Do not hesitate to ask me if you need help.

If you wan´t to do it by yourself: I have descriped the solution for a compass in this post. Simple add the negative value the compass gives you to the value you got by calculation the bearing with the code above.

Hope this helps you.

Community
  • 1
  • 1
Artur Hellmann
  • 315
  • 4
  • 21