I am struggling with animateCamera() method of the Google Maps. It only works on first launch of activity and if the activity is destroyed and created again, camera animations doesn't work but map is loaded fine. I have tried debugging the code, everything gets executed but map doesn't animate without any error or log. Although animation works when same mapFragment is used in the fragment but in activity it doesn't seem to work.
The solution mentioned in this this question is deprecated now and I am unable to fix this issue.
Map Fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapFragment.setRetainInstance(true);
#Override
public void onMapReady(GoogleMap googleMap) {
if (mMap == null) mMap = googleMap;
}
Code for animating camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_LEVEL));
Making entire project again from old code fixed the issue. But this issue re-appeared in another app. I found out that application was using old reference to mMap. This can be fixed by either getting reference from fragment manager or just setting mMap = null when activity is destroyed.
Related
Hello I am using a support map fragment for my Android map. Other than using default settings (no customisation), i noticed out of the box that the map seems to provide location updates automatically i.e. a dot on the map which tells where the user is and periodically updates.
My map fragment is apart of a layout whereby the map fragment visibility can be set to GONE and some other content is shown in its place.
My question is, how do i programmatically stop these location updates? I noticed when the activity which hosts the map fragment goes in a stop state, the location updates stop.
This is the behaviour i want to mimic but for when the visibility of the fragment changes.
In XML
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Initialised in code
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
Some settings set in onMapReady callback
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.getUiSettings().setZoomControlsEnabled(false);
mGoogleMap.setOnMyLocationButtonClickListener(this);
mGoogleMap.setOnMarkerClickListener(this);
Try this code
mGoogleMap.setMyLocationEnabled(false);
How do I load google map in async task. My app requires that when a user clicks a dialog box, it loads a google map activity. However it takes a lot of time for the app to load the map activity. I would like the loading of the map to be a bit faster. or possibly notify the user that google map is loading. I feel async task will be the best way to do this. But how do i achieve this through async task? Or is there a better of doing this? thank you.
I hope this helps.
make your activity implement OnMapReadyCallback
mapFragment = (MapFragment) getFragmentManager().findFragmentById(
R.id.map);
//start a progress dialog here
mapFragment.getMapAsync(this);
#Override
public void onMapReady(GoogleMap arg0) {
mMap = arg0;
//cancel the progress dialog here
}
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.
When the application opens I create a new instance of a google map during onAttachedToWindow(), this works fine while the application is open. When I close the application and re-open it the google map does not load correctly, it just shows grey tiles.
#Override
public void onAttachedToWindow() {
if (hasCreated()) {
uiGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_content_map_fragment)).getMap();
}
}
When should the google map be initialised to ensure it is loaded when the application is closed and re-opened?
EDIT:
I have tried initialising the map in onResume() and onCreate() and get the same problem
on Google API2 this hassle has been eliminated.
What you need to do is just initialize the map you created on your xml.
uiGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_content_map_fragment))
.getMap();
So when you close and re-open it it will be available. no need to override anything.
I think that the best place would be to create a class that extends SupportMapFragment, and initialise the map in:
#Override
public void onAttach(Activity activity)
Besides that though, I think if you initialise the map in onResume of the container activity it should be ok. But if you by initialising you mean adding the mapfragment to the activity, I would suggest doing it in the xml or in your activity's onCreate. Just my two pennies.
I am using Google Maps v2 in my Android application. Everything works fine, but if I leave the application (using the home button), use the device for other stuff and then return to my app, the maps just reset themselves and discard all my settings and listeners.
I think it's because my phone doesn't have that much RAM so when it needs it for running other apps, something that's vital for my app gets lost... If I don't use other apps in the meantime, it resumes with all the settings and listeners intact.
So I have a member variable private GoogleMap map = null;
I need to create the map programatically because I am putting it into a LinearLayout (fragment_wrapper) that I also want to use for other fragments. So I am doing this in my onCreate() method:
mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_wrapper, mapFragment);
fragmentTransaction.commit();
The map doesn't get initialized very quickly, so I need to set its settings and listeners in the onResume() method:
if (map == null) {
map = mapFragment.getMap();
UiSettings settings = map.getUiSettings();
settings.setMyLocationButtonEnabled(false);
settings.setZoomControlsEnabled(false);
(...)
map.setOnMapClickListener((...));
(...)
}
I originally put the if block there so that the app wouldn't have to set the map's settings and listeners again on resuming. But then I noticed it forgets the settings. So I thought that if I deleted the if block, it would just set up the map again since onResume() gets called when the app is resumed. But it doesn't work either... So all the map's settings and listeners are gone which renders the map useless and my app needs to be restarted in order to set up the map again.
Can anyone please tell me what the problem could be and how to solve it? Thank you.
I was finally able to find the solution in the Google Maps v2 sample code, which is located at <android-sdk-folder>\extras\google\google_play_services\samples\maps\. See https://developers.google.com/maps/documentation/android/intro#sample_code. What helped me was the file src\com\example\mapdemo\ProgrammaticDemoActivity.java.
What was happening was that the application got shutdown completely, so its onCreate() method was called again. There you have to somehow find out whether you have already created the map fragment or not. The solution is to try to find a tag of your map fragment. If it can't be found, it means the activity is running for the first time. You create your fragment and add the tag to it. Then the next time the onCreate() gets called, your fragment can be found by the tag and all the settings and listeners are preserved.
So you can for example set a final static variable in your activity: private static final String MAP_FRAGMENT_TAG = "map";
and then use this code in your onCreate() method:
mapFragment = (MapFragment) getFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_wrapper, mapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}
setUpMapIfNeeded();
This in the onResume() method:
setUpMapIfNeeded();
And the setUpMapIfNeeded() method could look like this:
if (map == null) {
map = mapFragment.getMap();
if (map != null) {
setUpMap();
}
}
(So the map sets up already in onCreate() and if not, then in onResume().)
Hope this helps someone.