6

I've been looking for a while to find a good tutorial with code example for a MapView in Fragment for ICS.

Anyone have any links?

hermann
  • 6,237
  • 11
  • 46
  • 66

2 Answers2

8

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
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    thanks for the answer... however, does my activity have to be a mapactivity? i curently have one activity with three fragments, one of which id like to have show a map – hermann Mar 31 '12 at 23:45
  • 1
    @hermann: "however, does my activity have to be a mapactivity?" -- yes, `MapView` requires that its hosting activity be a `MapActivity`. "i curently have one activity with three fragments, one of which id like to have show a map" -- just because it is a `MapActivity` does not mean that the *only* thing it can show is a `MapView`. This should work fine. – CommonsWare Apr 01 '12 at 10:41
  • Am getting the following error when i try to implement the same 12-03 03:22:36.903: E/dalvikvm(12866): Could not find class 'com.google.android.maps.MapView', referenced from method com.m7.nomad.MapsFragment.onActivityCreated – suresh cheemalamudi Dec 03 '12 at 11:42
  • 1
    @SureshC: Most likely, you do not have the `` element in the right place in your manifest. – CommonsWare Dec 03 '12 at 11:45
  • Thanks. but now am getting the following 12-03 04:07:03.723: E/AndroidRuntime(13715): java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity. Here is the Code i am using - http://pastebin.com/De6gKG4f looks like i need to have a Map view some where i didnt understand why u have created NooYawk.java when its not being calling from any where. – suresh cheemalamudi Dec 03 '12 at 12:14
  • @SureshC: "why u have created NooYawk.java when its not being calling from any where" -- it is the activity listed in the manifest for the `MAIN`/`LAUNCHER` ``. – CommonsWare Dec 03 '12 at 12:46
  • cool.. so the frame is embedded into the activity ? in my app i am using a FragmentActivity which has tabs which are two fragment. like this http://i.stack.imgur.com/RJPoI.png – suresh cheemalamudi Dec 03 '12 at 13:00
  • 1
    @SureshC: I would recommend that you spend time learning about fragments independently of maps first, then (and only then) worry about using them with maps. – CommonsWare Dec 03 '12 at 13:08
  • @CommonsWare i am confused please differentiate which one to use i have a viewpager in one of the tab fragment i need to show a map with the user's current location so which approach do i use do i use your answer or do i follow this http://www.tutorialspoint.com/android/android_google_maps.htm – Sagar Devanga Dec 26 '14 at 13:31
  • @SagarDevanga: This answer is for the Maps V1 API. You are not using the Maps V1 API, as that has been deprecated for about two years and you no longer can get API keys for it. Anything related to Google Maps on Android older than November 2012 should be ignored. The tutorial you link to shows the Maps V2 API. However, it does not show using it for pages in a `ViewPager`, let alone for pages in a `ViewPager` where the `ViewPager` itself is in a fragment (and therefore requires nested fragments). – CommonsWare Dec 26 '14 at 13:38
  • @CommonsWare could you please link me up to any tutorial that displays map in a fragment of a viewpager – Sagar Devanga Dec 26 '14 at 13:46
  • @SagarDevanga: Here is a sample app that shows a `ViewPager` with `MapFragments`, but the `ViewPager` is managed by an activity directly, not by some other fragment: https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Pager – CommonsWare Dec 26 '14 at 13:51
  • @CommonsWare thank you i'll check it out and yup the scenario you said is same in my case my fragment is also controlled by an activity – Sagar Devanga Dec 26 '14 at 13:56
0

I have answered to the same question here MapView in a Fragment (Honeycomb)

Community
  • 1
  • 1
Paul Annekov
  • 3,193
  • 3
  • 19
  • 31