在location.getBearing()
返回角度的范围内,是从北到东的0到360度,还是在某个其他范围内。
如果我的手机与动作方向平行放置,那么我的手机是朝向真正的北方是否需要匹配此轴承?
我使用SensorManager.getOrientation(rotationMatrix,vals)
函数计算方位角并将GPS偏差添加到其中以使设备朝向真北。
答案 0 :(得分:0)
这就是我所做的你可以参考这段代码。
<强> 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);
}
}
}
计算轴承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);
}
根据度数方向旋转图像
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);
}
答案 1 :(得分:0)
是的,轴承是从正北方向顺时针测量(和交付)度数。 有人形容为北方以东的度数。 范围是0 - 359.99999。 所以0度是北方。
2) 请注意,数学角度在东方为0°,逆时针方向增加。 根据代码,您必须在地理角度与数学角度之间进行转换。
对2)的进一步解释: 如果你看一张地图,那么北方向上,东方向右,这对应于我们在学校总是使用的二维笛卡尔坐标系:
x,y空格:
在那个空间中,正x是正确的方向,在大多数地图上是东方,在向上是正y,在地图上是北,负在西方和南方。
按顺序将latitdue经度坐标转换为笛卡儿x,y 使用更简单的学校数学,如角度计算,距离等。 那么你必须考虑所有数学运算都是基于笛卡儿x,y世界。在那个世界中,0°是正x轴方向(地图上的东)。 + 90°(数学角度)是方向y achsis。 +90地理位置在东方向,即x-achsis。 因此,数学角度逆时针方向上升,罗盘(= geoigrapohical angle)顺时针上升。
在这些wolrds之间进行转换时,你必须考虑到这一点。
然而,对于某些应用,您不必在球形和笛卡尔世界之间进行转换,但也许您必须使用加速度和陀螺仪传感器。