3

I'm trying to use a MapFragment from the Google Maps Android API v2 in conjunction with a Camera preview. I need to be able to switch between the camera preview and the MapFragment, but I can’t make it work.

For the Camera preview, I’ve copied the CameraPreview class from the example guide. When I want to see the Camera preview, I add an instance of the CameraPreview class to my activity using

CameraPreview mPreview = new CameraPreview(this); 
addContentView(mPreview, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

This works fine when when I’m not using the MapFragment.

For the MapFragment, I’ve added it into my activity’s layout via

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <fragment
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</LinearLayout> 

It works fine without the CameraPreview. I can hide and unhide the MapFragment using (e.g. for hide):

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.hide(map_fragment);
ft.commit();

However, the problem comes when I try and use the two together, i.e. hide the MapFragment and then add a CameraPreview instance into my activity. The hide doesn’t work, and it seems that the MapFragment somehow hijacks the CameraPreview and takes precedence. One strange feature is that if I force the screen to sleep and then wake it up, when it wakes up the CameraPreview is there. If I do it the other way round, i.e. add the CameraPreview first and then hide the MapFragment, the behavior is the same.

FYI: I'm testing the app on a Samsung Galaxy Note 2 LTE running Android Version 4.1.1.

Can anyone tell me what I’m doing wrong?

gisking
  • 290
  • 2
  • 3
  • 8
  • Since they both use a `SurfaceView`, and AFAIK you can only have one `SurfaceView` in an activity, you may need to have these be in separate activities. – CommonsWare Mar 12 '13 at 20:50
  • I’m not sure that’s right, CommonsWare, for two reasons: (1) I’m upgrading an app that previously used a MapActivity, before Google Maps Android API v2 existed. I didn’t have this problem before. (2) I had a look at the view hierarchy. The MapFragment is associated with a FrameLayout, and as far as I can tell my CameraPreview is the only SurfaceView. However, I’m a bit of a novice in this area, so I’ll investigate your suggestion further :-). – gisking Mar 12 '13 at 23:28
  • (1) I am not aware that the original `MapView` used a `SurfaceView`, though I suppose it is possible. (2) Many Android experts, myself included, have done our own analysis and confirmed that the map is rendered via a `SurfaceView`. – CommonsWare Mar 12 '13 at 23:33
  • Using two activities does seem to work, CommonsWare, so thank you very much :-). Of course, it makes the app quite a bit more complicated, so any other suggestions would be welcome. It's also rather annoying that nothing in the single activity implementation gave us any warnings about the SurfaceView issue (assuming that that is the cause of the problem). – gisking Mar 13 '13 at 12:43

1 Answers1

0

Use setZOrderOnTop(boolean) on your camerapreview like this mPreview.setZOrderOnTop(true);. It works for me.

Francesco verheye
  • 1,574
  • 2
  • 14
  • 32