谷歌地图罗盘错误,0.67dp视图

时间:2015-11-30 05:09:06

标签: java android google-maps

我有一个带罗盘的视图和谷歌地图片段。

地图和指南针工作正常。问题是,它正在移动。搬到北方。 我只获得谷歌地图的相机位置,并设置方位,并将此类设置为谷歌地图以设置地图的方位(旋转)。

我的第一个想法是,罗盘是这个问题的原因。但事实并非如此。移除轴承()后,地图仍然平稳地向北移动。

最后,我找到了原因。谷歌地图上方的0.67dp视图是问题所在。将0.67dp更改为1dp后,地图不会移动。

但我无法理解为什么会发生这种情况。 0.67dp的高度是真的有问题吗?为什么?或者还有其他秘密原因?

有人可以帮我解释为什么会发生这种事吗?

以下是我的所有代码。

MapActivity.java

public class MapActivity extends FragmentActivity implements SensorEventListener {
    private LatLng currentPoint = new LatLng(37.49349, 127.013958);
    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;
    private GoogleMap gmap = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        initGoogleMap();
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void initGoogleMap() {
        SupportMapFragment supportMapFragment = new SupportMapFragment();
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.flGoogleMap, supportMapFragment).commitAllowingStateLoss();

        supportMapFragment.getMapAsync(new OnMapReadyCallback() {

            @Override
            public void onMapReady(GoogleMap map) {
                gmap = map;
                showMyPosition();
            }
        });
    }

    public void showMyPosition() {
        gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPoint, 18));
    }

    private float[] mGravity;
    private float[] mGeomagnetic;

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            mGeomagnetic = 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);
                float azimut = orientation[0]; // orientation contains: azimut, pitch and roll
                // float pitch = orientation[1];
                // float roll = orientation[2];

                float destAngle = (float) Math.toDegrees(azimut);
                showMapDirection(destAngle);
            }
        }
    }

    private void showMapDirection(float bearing) {
        if (gmap == null) {
            return;
        }
        CameraPosition oldPos = gmap.getCameraPosition();
        if (oldPos == null) {
            return;
        }
        //CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing).build();
        // FIXME: After remove bearing, it still moving
        CameraPosition pos = CameraPosition.builder(oldPos).build();
        gmap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
    }

}

activity_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/view_divider"
        android:layout_width="match_parent"
        android:layout_height="0.67dp"
        android:background="#CCCCCC" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/view_divider" >

        <FrameLayout
            android:id="@+id/flGoogleMap"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

</RelativeLayout>

0 个答案:

没有答案