I have an activity which creates a new fragment containing SupportMapFragment, using FragmentTransaction. In this new fragment I am creating the google maps object in onMapReady method, and also retrieving the user location.
In the activity I will need to create a new fragment, that also contains a SupportMapFragment, but with different layout than the previous one. The flow will look like this:
Activity -> MapFragment1 -> get google maps and location
-> MapFragment2 -> get google maps and location
I will need something like this:
Activity -> get google maps and location -> MapFragment1
-> MapFragment2
Is there a way to actually create the google maps and have the user location in the activity, and then somehow pass it too all the fragments derived from the activity?
Thank you for your answers!
Related
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.... ( :
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.
I have an application in which I am attaching different fragment with activity. and in that activity container I am replacing different fragment.
So at the start of the activity I replace the MapFragment in the activity container. Later on I replace other fragment and then from main activity I replace again the map fragment. Although I think I have wrong implementation of map in fragment but I wanted to know and wanted to do some different things and I think as I have no better understanding of fragment so I think that is why I am getting null pointer exception
What I have done SO far
Case 1.
AS I said On App start I am replacing map fragment but doing nothing more except showing user current location by just setting user location enable in google map v2. so on the button click of the map let say show other location , fetch location information from server and draw pins on the map. this is working good as far as the map fragment is there.
Case2
if user click button let say "Show location" while there is other fragment in the container let say About Us fragment which contains app info , so now on this button it is supposed that map fragment should be replaced again and then the function in that fragment which is going to download locations from the server and to show them on map should start working as I am calling on button click
fragmentTransaction1.replace(R.id.frame, myMapFragment, "MyMapFragment").commit();
fragmentManager.executePendingTransactions();
myMapFragment.GetAllLocations("friendsLocation");
so this GetLocations() should run , but at this stage the app crash saying null pointer exception. and in this function myMapFragment.GetAllLocations("friendsLocation")
there is a need of context which I taking as getActivity(), the context is needed as I have to make a call to web api using ION library which needs context.
So these are my basic questions
How can I get to know that fragment is started and then the function which I have called from MainActivity should run after the mapFragment is loaded and started so that I can not get the null context in the getActivity() ?
Is there any other way of doing that , I hope you understand my need that is , I want to replace the map fragment and I want to call the fragment method on the same time by passing the parameters to fragment method from the main activity , but How can it be done ?
An idea is in my mind and that is while replacing back the map fragment through Mainactivity button , i should set some static value in the mapFragment let say
public static boolean isLocationShouldDownload== flase //in map
fragment
I should set it true while replacing fragment
and then when the fragment is started after it is in running condition it should check this variable if it is true (set by the main activity) then it should download and start plotting pins. but I have no idea how can I get to know that fragment is running .
I have no idea how and where I have to implement my idea which I have stated in point no 3.
Please share your idea with me and give me suggestion or share me a source code that how can I achieve this task through main activity that fragment should replace and download the locations and to show them on map without getting app crash.
I have both A and B activities.
A uses GoogleMaps for adding markers, B to adding lines.
But A and B opens their own map.
How can i use same map on both activities? I don't want to pass marker and line datas again and again for every load.
You can use EventBus https://github.com/greenrobot/EventBus to pass any complex object around in Android.
Example passing the map from one Activity to another:
Add your GoogleMap on the EventBus:
EventBus.getDefault().postSticky(googleMap);
startActivity(new Intent(getActivity(), SomeActivity.class));
Anywhere in SomeActivity:
GoogleMap googleMap = EventBus.getDefault().getStickyEvent(GoogleMap.class);
This examle is just to show that it works across Activities, you can call getStickyEvent to get your map anywhere in your code.
EDIT
I've found a better STR:
Make sure to set "Do not keep activities" in Developer options in Settings.
Open the app with a SupportMapFragment as a child fragment of another fragment.
Switch to another app
Open your app again
Notice you can't interact with the map and no animations work.
Open another screen within the app
Notice there's a single frame or so of the map with the markers drawn on screen.
I have an issue with Google Maps API v2.
I am animating the camera to zoom to a set of custom marker bitmaps rendered on a MapFragment.
On selecting one of these marker tooltips I open a geo: intent (for the Google Maps app etc.)
When the user presses back it reopens my activity with the fragment back stack rebuilt.
My issue is that it doesn't render camera animations or the markers on coming back, though there is a brief display of those markers when the user presses back to go to the previous fragment.
The GoogleMap instance has the markers, but it doesn't render them - I'm guessing because the MapFragment/MapView thinks it doesn't need to be rendered.
How do I force a render of the MapView? Failing that, how do I get the MapView to recognise that the model has changed?
The issue was to do with the way I was handling my child fragments.
Every time the parent fragment would call onCreate the children fragments would get recreated.
I did the following to handle my child fragments, but there may be a better way:
private static final String TAG_FRAGMENT_MAP = "TagFragmentMap";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
if (savedInstanceState == null) {
// create the fragments for the first time
ft.add(R.id.view_flip, new SupportMapFragment(), TAG_FRAGMENT_MAP);
ft.commit();
}
}
// ...
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
mMapFragment = (SupportMapFragment)findFragmentByTag(TAG_FRAGMENT_MAP);
}