0
  • On Which scale does location.getBearing()returns the angle ,is it from 0 to 360 in degrees of east of ture north or it is in some else scale.

  • Does My phone's heading with true north need to match this bearing if my phone is placed parallel to the direction of motion ?

I calculate the azimuth with SensorManager.getOrientation(rotationMatrix,vals) function and added the GPS declination to it to get the device heading with true north.

2 Answers2

0

This what i have done you can refer from this code.

onSensorChanged

@Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
                mGeomagnetic = event.values;
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                mGravity = event.values;

            if (mGravity != null && mGeomagnetic != null) {
                float R[] = new float[9];
                float I[] = new float[9];
                boolean success = SensorManager.getRotationMatrix(R, I,
                        mGravity, mGeomagnetic);
                if (success) {
                    float orientation[] = new float[3];
                    SensorManager.getOrientation(R, orientation);
                    azimut = orientation[0]; // orientation contains:
                                                // azimut, pitch and roll
                    rotateCompass(azimut);
                }
            }
        }

Calculate bearing comapss

private void rotateCompass(final float azimut) {

    float azimuth = (float) Math.round(Math.toDegrees(azimut));
    Location currentLoc = new Location("");
    currentLoc.setLatitude(curr_lat);
    currentLoc.setLongitude(curr_long);
    Location target = new Location("");
    target.setLatitude(dest_lat);
    target.setLongitude(dest_lng);

    float bearing = currentLoc.bearingTo(target); // (it's already in degrees)
    if (bearing < 0) {
        bearing = bearing + 360;
    }
    float direction = (float) (bearing - azimuth);

     // If the direction is smaller than 0, add 360 to get the rotation clockwise.
    if (direction < 0) {
        direction = direction + 360;
    }

    showToast("" + direction);
    rotateImageView(imgCompass, R.drawable.pin_finder, direction);
}

Rotate Image according to direction in Degree

private void rotateImageView(ImageView imageView, int drawable, float rotate) {

    // Decode the drawable into a bitmap
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
            drawable);
    // Get the width/height of the drawable
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();

    // Initialize a new Matrix
    Matrix matrix = new Matrix();

    // Decide on how much to rotate
    rotate = rotate % 360;

    // Actually rotate the image
    matrix.postRotate(rotate, width, height);

    // recreate the new Bitmap via a couple conditions
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
            height, matrix, true);
    // BitmapDrawable bmd = new BitmapDrawable( rotatedBitmap );

    // imageView.setImageBitmap( rotatedBitmap );
    imageView.setImageDrawable(new BitmapDrawable(getResources(),
            rotatedBitmap));
    imageView.setScaleType(ScaleType.CENTER);
}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
0

Yes, bearing is measured (and delivered) in degrees from True North, clockwise. Some describe that as degrees East of north. Range is 0 - 359.99999. So 0 degrees is North.

2) Note that mathematic angles have 0° at east, and increase counter clockwise. Depending on the code you have to convert between geographical angles to mathemtical ones.

Further explanation to 2): If you look on a map, then north is up, east is right, this corresponds to our 2d cartesian coordinate system we used always in school:

The x,y space:

In that space positive x is in direction right, which is east on most maps, positive y in Up, or North on maps, negative are west and south.

When transforming latitdue longitude coordinates to cartesian x,y in order to use simpler school mathematics, like angle calculation, distances, etc. then you have to consider that all mathematical operations are based on cartesian x,y world. And in that world, 0° is in direction positive x-axis (east on a map). +90° (mathematical angle) is in direction y achsis. while +90 geograpic bearing is in direction east which is x-achsis. So matjematic angles raise counter clockwise, and the compass rose (=geoigrapohical angles) raise clockwise.

When transforming between these wolrds, you have to consider that.

However for some applictaions you don't have to transform between the spherical and cartesian world, but maybe you have to, when using acceleration and gyro sensors.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • @AlecWien Yes I have understood the first part that the bearing is from 0 to 359.9999 from true north clockwise but am still confused in the second ...can you elaborate please. – Paras Dhawan May 09 '15 at 08:34