Hari G
Hari G

Reputation: 5

Google Map inside a fragment with custom layout

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

Upvotes: 0

Views: 4632

Answers (1)

Kaleb
Kaleb

Reputation: 1885

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

Upvotes: 3

Related Questions