I have a fragment which is a part of Viewpager, and I want to use Google Map V2 inside that fragment. This is what I have tried so far,
In my fragment,
private SupportMapFragment map;
private GoogleMap mMapView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
map = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (map == null) {
map = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, map).commit();
}
}
@Override
public void onResume() {
super.onResume();
if (mMapView == null) {
mMapView = map.getMap();
Marker hamburg = mMapView.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = mMapView.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
mMapView.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
mMapView.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
and in my layout subfragment_info.xml , I have,
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tableLayout1" />
I can see the map now. But the markers are not showing. I think my Google map mMapView is null. Please help me to get this problem solved. Thanks in advance.
For MapFragment
In your Fragment Class
For SupportMapFragment
In your Fragment Class
Create a frame for map in which it will be added in your xml layout
Don't include class="com.google.android.gms.maps.SupportMapFragment" in xml either in your fragment class do get it manually inside onActivityCreated
if you guys face any problem Illegal State Exception Occur just write below code
/* * This issue Is tracked in (Google Bugs) * http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064 Added * Code Due to Illegal State Exception Occur when reclicking on tab * * A short-term workaround that fixed it for me is to add the following to * onDetach() of every Fragment which you call */
//If you want to show map in activity just extend your Activity by //FragmentActivity write code mention below
In onCreate
In onResume
Additional help can get from these links
https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1 https://developers.google.com/maps/documentation/android/map
This is how I did it
in layout:
in code:
Took me a lot of fiddling, you need just the right combination of things, make sure your class extends FragmentActivity
You have to decide if you create your fragment in code
new SupportMapFragment()
or inflate from xmlclass="com.google.android.gms.maps.SupportMapFragment"
.Actually you cannot have a
Fragment
in xml for anotherFragment
. Read about nestedFragment
s.You may follow this comment on how to add nested
SupportMapFragment
.I had more or less the same problem. I had a Navigation Drawer and I wanted to put the map in a fragment.
I followed this thread (but it is in Spanish): https://groups.google.com/forum/#!msg/desarrolladores-android/1cvqPm0EZZU/Q801Yvb2ntYJ
The clue was (in my opinion) to change the layout.xml file: Instead of a "fragment" inside your "layout subfragment_info.xml" you would need to change to this:
Here it is the code inside the thread (you could use it and adapt): https://groups.google.com/d/msg/desarrolladores-android/1cvqPm0EZZU/9srw_9feamUJ
For Load map into fragment:
For .java: