Blank google map when using it inside multiple fragments on a viewpager - android

I have a viewpager where each "page" is a fragment. Each of the "page fragments" have a map fragment (google maps sdk for android) inside, added programatically using getSupportFragmentManager (If I use getFragmentManager it doesn't work either). Each added map is a new instance of SupportMapFragment (MapFragment doesn't work either).
On the first fragment, the one that is first added to the viewpager, the map works. At the other fragments, the non-interactive map just show itself as a grey square.
I'm using the 'com.google.android.gms:play-services-maps:7.5.0' dependency.
All the added maps are using the lite version. I'm doing this with:
GoogleMapOptions options = new GoogleMapOptions();
options.liteMode(true);
options.mapToolbarEnabled(false);
SupportMapFragment mapFragment = SupportMapFragment.newInstance(options);
I've read on some blogspots and here on Stackoverflow that maybe an app can only have one instance of google maps per process. Have anybody faced the same problem? Is my guess correct?
Thanks.

If you want to put a fragment into an other fragment, use getChildFragmentManager() instead of getSupportFragmentManager()
getChildFragmentManager
Return a private FragmentManager for placing and managing Fragments
inside of this Fragment.

Related

Android fragments communication with maps

I have in my project an activity (that extends FragmentActivity), and two fragments (extends v4.fragment and one of them has a map fragment).
I made my project for tablet, and both fragments are shown on screen.
the problem is, I tried communicating between them (adding a marker on map from another fragment) , by using an Interface that is implemented in the activity.,
and for some reason my googleMap object returns null.
when I'm trying to use method inside the fragment
(e.g: options.mapType(GoogleMap.MAP_TYPE_SATELLITE)) using my googleMap object
it's working.
any idea what did i do wrong?
My bad. rookie mistake- I did'nt call the right fragment id from xml.... ( :

How to load a fragment over mapfragment in Android?

Map fragment will be the default fragment and on a click a new fragment should be loaded and a value has to be entered in the fragment and this value has to be passed to the map fragment
To use MapFragment in Fragment Follow this link
using the Support Library to work (SupportMapFragment instead of MapFragment), so that the MapFragment can even work on below Android 3.0. You can replace the SupportMapFragment with the MapFragment if you are working on Android 3.0 or above.

FragmentActivity crashes because of a GoogleMap inside one of the fragments

I have a fragment activity as seen in my last question:
App crashing when using fragments
It crushes because of a GoogleMap inside one of my fragments (MainActivity).
right now thats the code:
private GoogleMap map;
map = ((MapFragment) getActivity().getFragmentManager() .findFragmentById(R.id.map)).getMap();
I cant change GoogleMap. it causes other problems in my code as im using it.
What should I do?
Thanks for your assistance!
Ive tried to relocate that piece of code to onCreateView. Doesn't work either.

How to swap between map fragment and list fragment in android while updating both fragments with AsyncTask

My strategy for doing this was very simple while using a MapView, but I am now transitioning over to Android Maps v2 and have to use a MapFragment. My scenario involves getting a list of events, and then I need to Geodecode these in the background to get their latitude and longitude to calculate distance from user and also to place onto the map. I have an actionbar button that allows the user to switch between the list and map fragment, but my issue is how do I keep both fragments in memory and avoid their lifecycle so that I can continually add markers to the map fragment while updating the list adapter in the list fragment?
Currently I replace the FrameLayout by FragmentTransaction.replace with each Fragment, but this causes the other one to get destroyed, whereas I simply want it to be hidden (before I would simply set the MapView visibility to View.GONE).
EDIT
My code ended up being:
#Override
public void swapFragments() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (listFragment.isVisible()) {
transaction.hide(listFragment);
if (mapFragment.isAdded()) {
transaction.show(mapFragment);
} else {
transaction.add(R.id.root, mapFragment);
}
} else {
transaction.hide(mapFragment);
transaction.show(listFragment);
}
transaction.commit();
}
I've been working on something similar. What I do is create both fragments in the onCreate() portion of my main FragmentActivity --- then based on the onclick action, I show and hide the appropriate fragments. I supplement this by adding to the back stack before I commit my hiding of the listFragment so that when a user presses back, they'll get the list again and not the previous activity.
However, at the moment, I'm having trouble with getting the GoogleMap to not be null in my MapFragment for some reason.
I solved this issue -- you can find a similar answer here MapFragment Error - CameraUpdateFactory is not initialized

Android: How to create a map by java code rather than by xml in google map api v2?

In my android project, I would like to create a Google Map using java code rather than useing xml, how should I do?
You can add a Map using MapFragment to an Activity in code. To do this, create a new MapFragment instance, and then call FragmentTransaction.add() to add the Fragment to the current Activity.
mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.my_container, mMapFragment);
fragmentTransaction.commit();
For more details check out the GoogleMaps, you will have more idea of implementing the maps.

Categories

Resources