MapView只有在触摸时才会刷新

时间:2015-07-23 17:22:06

标签: java android android-fragments android-mapview

我在片段中有一个MapView,我想显示用户的当前位置。 我的问题是它只是刷新(在图层中,首先是地形,然后是 触摸MapView时街道的名称...)它显示了一个前置摄像头 预览也。这是我的代码。 片段布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/otherRelative"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="Some text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView2"
            android:gravity="center"
            android:text="Some text"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/otherRelative" >

        <FrameLayout
            android:id="@+id/camPreview"
            android:layout_width="163dp"
            android:layout_height="211dp" >

        </FrameLayout>

        <com.google.android.gms.maps.MapView
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.08"
            app:cameraZoom="15"
            map:mapType="normal" >
        </com.google.android.gms.maps.MapView>

    </LinearLayout>

</RelativeLayout>

和片段代码

public class MarcacionFragment extends Fragment {
    // Graphics
    private Camera mCam;
    private MirrorView mCamPreview;
    private FrameLayout mPreviewLayout;
    private MapView ViewMap;
    private GoogleMap map;

    // Size of the camera preview
    private int size = 500;
    private GPSTracker gps;
    private int mCameraId = 0;
    private CameraUpdate cameraUpdate;

    public MarcacionFragment() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // retain this fragment
        setRetainInstance(true);
    }

    public void onResume() {
        super.onResume();
        if (mCam == null && mPreviewLayout != null) {
            mPreviewLayout.removeAllViews();
            startCameraInLayout(mPreviewLayout, mCameraId, getView());
        }
    }

    @Override
    public void onPause() {
        if (mCam != null) {
            mCam.release();
            mCam = null;
        }
        super.onPause();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.marcacion_layout, container, false);
        mCameraId = findFirstFrontFacingCamera();
        mPreviewLayout = (FrameLayout) rootView.findViewById(R.id.camPreview);
        ViewMap = (MapView) rootView.findViewById(R.id.map);
        ViewMap.onCreate(savedInstanceState);

        map = ViewMap.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.getUiSettings().setAllGesturesEnabled(true);
        map.setMyLocationEnabled(true);
        mPreviewLayout.removeAllViews();
        startCameraInLayout(mPreviewLayout, mCameraId, rootView);
        MapsInitializer.initialize(this.getActivity());

        // Start GPS service
        gps = new GPSTracker(rootView.getContext());
        if (gps.canGetLocation()) {
            LatLng latLng = new LatLng(gps.getLatitude(), gps.getLongitude());
            cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);

            map.getUiSettings().setMapToolbarEnabled(false);
            map.getUiSettings().setZoomControlsEnabled(true);
            map.animateCamera(cameraUpdate);
            map.addMarker(new MarkerOptions().position(latLng).draggable(true));
            ViewMap.setClickable(false);
        } else {
            gps.showSettingsAlert();
        }
        ViewMap.invalidate();
        return rootView;
    }

    @SuppressLint("NewApi")
    private int findFirstFrontFacingCamera() {
        int foundId = -1;
        int numCams = Camera.getNumberOfCameras();
        for (int camId = 0; camId < numCams; camId++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(camId, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                foundId = camId;
                break;
            }
        }
        return foundId;
    }

    private void startCameraInLayout(FrameLayout layout, int cameraId, View rootView) {
        mCam = Camera.open(cameraId);
        if (mCam != null) {
            mCamPreview = new MirrorView(rootView.getContext(), mCam);
            mCamPreview.getHolder().setFixedSize(size, (size / 4) * 3);
            mCamPreview.setCameraDisplayOrientationAndSize(getActivity(), cameraId);
            layout.addView(mCamPreview);
        }
    }
}

如何在不触碰的情况下刷新mapView?

0 个答案:

没有答案