0

What is my best option to get google map in a fragment with custom lay out?

I am trying to use GoogleMapsV2 for one of the fragment(tab). The fragment would have some buttons and the google map in the background with markers. So the view would look some thing like http://www.androidtapp.com/wp-content/uploads/2009/08/CardioTrainer-Map.jpg (Mine would be lot simpler than that though)

After googling ,overflowing the stack extensively and experimenting this is what I understood 1.I can use SupportMapFragment in my fragment (I tried it works. But too bad I am not able to get my custom buttons)

2.I can use MapFragment with higher SDK (sdk 12). I dont mind loosing backward compatibility. But this option looks wrong as my FragmentPagerAdapter expects that I return a Fragment and not MapFragment

Looks like there is no easy/standard method to get this done I need some help in moving in the right direction. I am a new self taught android developer. So bear with me if I miss the basics here. I can post the code if that helps. However I am looking for help in understanding the direction to go in rather than help in direct code. Thanks for looking

Hari G
  • 5
  • 2
  • 4
  • 1
    Can you not just have a fragment that has an embedded MapFragment or SupportMapFragment within it? – Selecsosi Nov 15 '13 at 16:08
  • I tried having a fragment inside my relativeView But I saw an exception saying 'can not inflate view. Error in Binary xml'. Thanks for looking though – Hari G Nov 15 '13 at 17:37
  • @HariG why not ask about your original problem instead then? You were most likely doing something wrong, like putting fragment into other fragment's layout xml. – MaciejGórski Nov 15 '13 at 17:41
  • @MaciejGórski I am not sure if I can have my custom layout with SupportMapFragment that is the reason why I did not pursue that route. As I said earlier I need a few pointers in the right way to do this. I would post my code after I get home if that helps. Thanks for taking time to resond – Hari G Nov 15 '13 at 17:47

1 Answers1

3

Sounds like you want something like this:

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

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

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="0px" >

<!-- Lots of fancy layout -->   

</RelativeLayout>
</RelativeLayout>

And then in your SupportMapFragment something like this:

public class MyFragment extends Fragment {

private SupportMapFragment fragment;
private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle       savedInstanceState) {
return inflater.inflate(R.layout.layout_with_map, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
    fragment = SupportMapFragment.newInstance();
    fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}

@Override
public void onResume() {
super.onResume();
if (map == null) {
    map = fragment.getMap();
    map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
}

Also, worth mentioning... There appears to be a bug with reloading the fragment for a second time. This is something of a work around for the SupportMapFragment. See here: The work around

There is also a possible better fix to this here, but I haven't tried it yet: Option 2

Community
  • 1
  • 1
Kaleb
  • 1,855
  • 1
  • 18
  • 24
  • Thank a lot for the response. It looks like it should work. I would give it a try and post the results back here. – Hari G Nov 15 '13 at 18:12
  • Keep in mind you should not add nested fragments via xml: http://developer.android.com/about/versions/android-4.2.html#NestedFragments The bug you mention are simply because of not following the docs. – MaciejGórski Nov 15 '13 at 18:39
  • As far as I know, it is a bug. https://code.google.com/p/android/issues/detail?id=42601 If you have a better fix, that doesn't throw exceptions. Please do share. – Kaleb Nov 15 '13 at 18:43
  • @Kaleb Finally my app is working after following your suggestion. Thanks a lot – Hari G Nov 27 '13 at 06:54