0

In my app i want to calculate the angle of the specific location(lat and longs are given) from the north.is there any way to calculate this.Actually i have find put the direction of the phone but i want to get angle of location from north.here is my code.please suggest any related solution.Thankx

   myAzimuth=Math.round(event.values[0]);
            myPitch=Math.round(event.values[1]);
            myRoll=Math.round(event.values[2]);
         Toast.makeText(this, "Value"+myAzimuth, Toast.LENGTH_SHORT).show();        
        if(myAzimuth<22){
        Toast.makeText(this, "North Direction", Toast.LENGTH_SHORT).show();     
        }

          else if (myAzimuth >= 22 && myAzimuth < 67)
              Toast.makeText(this, "North East", Toast.LENGTH_SHORT).show();        
          else if (myAzimuth >= 67 && myAzimuth < 112)
              Toast.makeText(this, "East Direction", Toast.LENGTH_SHORT).show();        
          else if (myAzimuth >= 112 && myAzimuth < 157)
              Toast.makeText(this, "South east Direction", Toast.LENGTH_SHORT).show();      
          else if (myAzimuth >= 157 && myAzimuth < 202)
              Toast.makeText(this, "South Direction", Toast.LENGTH_SHORT).show();       
          else if (myAzimuth >= 202 && myAzimuth < 247)
              Toast.makeText(this, "South west Direction", Toast.LENGTH_SHORT).show();      
          else if (myAzimuth >= 247 && myAzimuth < 292)
              Toast.makeText(this, "west Direction", Toast.LENGTH_SHORT).show();        
          else if (myAzimuth >= 292 && myAzimuth < 337)
              Toast.makeText(this, "North west Direction", Toast.LENGTH_SHORT).show();      
          else if (myAzimuth >= 337)
              Toast.makeText(this, "North Direction", Toast.LENGTH_SHORT).show();       
picaso
  • 713
  • 2
  • 14
  • 26
  • [Relevant question](http://stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android) – st0le Jan 19 '12 at 08:26
  • You do realise that latitude and longitude are measured from the equator and Greenwich Meridian? The North pole has a latitude of 90 degrees north and 0 degrees longitude. Are you just after calculating which direction the phone user is travelling in? – ChrisBD Jan 19 '12 at 08:30
  • what would be direction if myAzimuth lies in (0 - 21)? – Khawar Jan 31 '13 at 09:42
  • @Khawar it would be North again – IIRed-DeathII Aug 02 '16 at 18:40

1 Answers1

1

Depending on what you mean by "angle of location from the north" there are several possible solutions. One is this:

final float[] results= new float[3];
// The computed distance in meters is stored in results[0].
// If results has length 2 or greater, the initial bearing is stored in results[1].
// If results has length 3 or greater, the final bearing is stored in results[2].
Location.distanceBetween(refLat, refLong, 90.0f, 0.0f, results);
final float bearing = results[1];

You get the bearing for a course from your reference location to the north pole. The bearing/heading changes while following the course on a least distance course.

Or even better as suggested by Konstantin Pribluda (see comment below)

final float bearing = 0.0f;
Stefan
  • 4,645
  • 1
  • 19
  • 35
  • 1
    Bearing from every location to the north pole is 0 ;) And it would not change. – Konstantin Pribluda Jan 19 '12 at 08:42
  • Of course, you are right, shortest path to the north pole is the rhumb line (or loxodrome) which intersects every meridian of longitude at 90 degrees). So the code is easily refactored to final bearing = 0.0f; // :-) However, you may choose any initial bearing between and not including -90 and 90 degree and get to north pole eventually by following the rhumb line ... ;-) – Stefan Jan 19 '12 at 09:44
  • Thanks for answer. `distanceBetween` is a userful method – Vlad Oct 13 '19 at 06:14