title: Map v2 SupportMapFragment inside Viewpager
body: <p><strong>Background</strong></p>
  
  <p>I have successfuflly added the new map api v2.0 to a ViewPager and the map loads fine. I am using the FragmentViewPager. I run into trouble however when trying to add markers as I cannot get the Fragment by tag as the ViewPager I am using is from the compatibility library and defined in XML, and this "hides" the tag from me.</p>
  
  <p><strong>The Question</strong></p>
  
  <p>How can I load the new Map v2 with markers <code>SupportMapFragment</code> into a <code>ViewPager</code> that uses a <code>FragmentPagerAdapter</code>.</p>
  
  <p><strong>My Attempt</strong></p>
  
  <p>So in my nievety I thought could create an elegant solution by extending the SupportMapFragment into a new class that does the following:</p>
  
  <ol>
  <li>Add a new constructor that takes <code>GoogleMapOptions</code> and <code>List&lt;MarkerOptions&gt;</code> . This effectively calls the existing constructor that just takes <code>GoogleMapsOptions</code> and then store my list of markers inside a private property of the new class.</li>
  <li>Override <code>GetMap()</code> - check if it is null and if it is not add the markers to the map that we loaded earlier on to the map and finally return it.</li>
  </ol>
  
  <p>My thoughts were that my extension could be highly reusable and abstracts anything map related to one place and allows my activities to create the new MapFragment by <code>newInstance(myGoogleMapOptions, myMarkers)</code>.</p>
  
  <p>Ok so the issue. Part 1 seems to work fine however during debugging I cannot get the overridden GetMap() method to be called. In fact I have tried overriding most of the usual methods that fragments have and none of them get called. Is there something weird going on with the SupportMapFragmemt, I tried to find the source for it to no avail to check for myself.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I realised why I wasn't getting my overridden methods called. It was because I was returning the standard <code>SupportMapFragment</code> rather than <code>MyMapFragment</code>. That said I cannot call super from a static method and I cannot cast from a base class to a derived class without exception.</p>
  
  <p><strong>Edit</strong>
  Here is the class</p>
  
  <pre><code>public class MyMapFragment extends SupportMapFragment {
  
      private static List&lt;MarkerOptions&gt; mMarkers;
  
      public static SupportMapFragment newInstance(GoogleMapOptions options, List&lt;MarkerOptions&gt; markers)
      {
          SupportMapFragment fragment = newInstance(options);
  
          mMarkers = markers;
  
          return fragment;
  
      }
  
              //have tried Overriding several methods including getMap()...
  
      @Override
      public void onResume ()
      {
          GoogleMap mMap = super.getMap();
          //add the markers
          if(mMap != null)
          {
              for(MarkerOptions marker : mMarkers)
              {
                  mMap.addMarker(marker);
              }
          }
  
      }
  
  }
  </code></pre>