Mapview in a Fragment (Honeycomb)

MapView in a Fragment (Honeycomb)

As of 03.12.2012 Google released Google Maps Android API v2. Now you can forget about these problems.
https://developers.google.com/maps/documentation/android/

Example using new API - https://developers.google.com/maps/documentation/android/start#add_a_map

This API will work for at least Android API 8, so use it ;).

So now you can simply use "com.google.android.gms.maps.MapFragment" fragment class. It will display the map in your Activity. Layout example from the link above:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>

MapView in Fragment ( Android 4.0 or higher)

Here is a book's sample application showing how to have a MapView in a Fragment in an API Level 11+ app. It's mostly just a MapActivity. Here are the key bits of the fragment that loads the MapView:

public class MapFragment extends Fragment {
private MapView map=null;
private MyLocationOverlay me=null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return(new FrameLayout(getActivity()));
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

map=new MapView(getActivity(), "0mjl6OufrY-tHs6WFurtL7rsYyEMpdEqBCbyjXg");
map.setClickable(true);

map.getController().setCenter(getPoint(40.76793169992044,
-73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);

Drawable marker=getResources().getDrawable(R.drawable.marker);

marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());

map.getOverlays().add(new SitesOverlay(marker));

me=new MyLocationOverlay(getActivity(), map);
map.getOverlays().add(me);

((ViewGroup)getView()).addView(map);
}

// rest of fragment here
}

fragments and mapView

I'm trying to do the same thing and have found this solution so far, though I haven't tried it myself yet.

MapView in a Fragment (Honeycomb)

Android Maps V2 MapView inside custom fragment (NPE)

I succeeded in including a MapView (v2) in a custom Fragment itself embedded in a ViewPager. In my case, the MapView is included in the Fragment layout file. I have had to call lifecycle methods on MapView (onCreate() called onCreateView() from the Fragment), and to call manually MapsInitializer.initialize(context) to avoid a NullPointerException from class BitmapDescriptorFactory (to get the bitmap for markers). This last trick is strange, and I don't know why the Map system isn't properly initialized itself without this call, maybe it's just a bug in the current version ...

In my case I haven't had any NullPointerException in onResume() or onDestroy().

Android : Access Mapview Inside Fragment With ViewPager

You can use LocalActivityManager to host a Activity inside a fragment. It is a deprecated class, but it offers the simplest solution.

Here is the code for a fragment (MyFilterFragment in your code). YourMapActivity is a basic activity that extends MapActivity.

public class MapFragment extends Fragment {
private static final String KEY_STATE_BUNDLE = "localActivityManagerState";

private LocalActivityManager mLocalActivityManager;

protected LocalActivityManager getLocalActivityManager() {
return mLocalActivityManager;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle state = null;
if (savedInstanceState != null) {
state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
}

mLocalActivityManager = new LocalActivityManager(getActivity(), true);
mLocalActivityManager.dispatchCreate(state);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//This is where you specify you activity class
Intent i = new Intent(getActivity(), YourMapActivity.class);
Window w = mLocalActivityManager.startActivity("tag", i);
View currentView=w.getDecorView();
currentView.setVisibility(View.VISIBLE);
currentView.setFocusableInTouchMode(true);
((ViewGroup) currentView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
return currentView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(KEY_STATE_BUNDLE,
mLocalActivityManager.saveInstanceState());
}

@Override
public void onResume() {
super.onResume();
mLocalActivityManager.dispatchResume();
}

@Override
public void onPause() {
super.onPause();
mLocalActivityManager.dispatchPause(getActivity().isFinishing());
}

@Override
public void onStop() {
super.onStop();
mLocalActivityManager.dispatchStop();
}

@Override
public void onDestroy() {
super.onDestroy();
mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
}
}

Edit: Since this answer was posted, Google has released Google Maps Android API with fragment support with an official MapFragment class.

Change layout on Honeycomb with fragments

You can choose an inner container to hold the fragments
Refer to the sample layout below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout android:id="@+id/fragment_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Android Render Google Map inside a Fragment

Take a look at the new Google Maps Android API v2 that was just released. It contains support for fragments (including the support library) and much more.

MapView in Fragment Memory Leak

Ok, i figured out what was the problem (i think i did) as i am not seeing leaks on the map anymore.

I added this to the fragment onDestroyView

@Override
public void onDestroyView() {
googleMap.clear()
mMapView?.onDestroy()
mMapView = null
super.onDestroyView()
}

I have read in one of the post that googleMap.clear() and MapView onDestroy() should be in onDestroyView() and not in onDestroy() method because that callback is not called. That got rid of the error but the memory problem in replacing fragments still existed. Once i added mMapView = null there from what i could tell there where not any leaks(i will still better debug this and if i find anything new i will post here).



Related Topics



Leave a reply



Submit