Here maps view goes black when app go in background - android

I have recently started using here maps api, and i have encountered some difficulties implementing maps. When app goes in background and i come back to app, map view screen is black. I should mention that i'm using MapView and not MapFragment. MapView is inside fragment view. Beside that map is showing clearly.
mapView.setMap(mMap);
mMap = mapView.getMap();
mMap.setZoomLevel(16);
mapMarker = new MapMarker();
mapMarker.setTitle("Neki naslov");
mapMarker.setDescription("Neki opis");
mapMarker.setDraggable(true);
mapView.setMapMarkerDragListener(mOnDragListener);
mapMarker.setCoordinate(PositioningManager.getInstance().getLastKnownPosition().getCoordinate());
mMap.addMapObject(mapMarker);
mapMarker.showInfoBubble();
mMap.setCenter(PositioningManager.getInstance().getLastKnownPosition().getCoordinate(),
Map.Animation.NONE);

Most likely, you did not call MapView#onResume() and MapView#onPause() as part of the activity / fragment life cycle. If it is not called, the underlying rendering thread will not be resumed.
#Dusan, your code does not have an image for the marker, so there is no marker to show.

Related

Reinitialising Fragment when closing activity

I have an Activity with a fragment, this fragment has a map in it and some markers.
I run a service in the background that changes the position of the markers.
Each Marker has a boolean "isDrawn" and whenever the boolean is set to false the map is updated by adding drawing the marker ( I use LiveData to observe the markers)
Whenever I close the Activity that contains the fragment I call onDestroyView, which sets isDrawn of every marker to false. That way when I open the Activity again, the markers get drawn one more time. All of this works fine.
The problem is this: in the Fragment I can tap on the markers, which opens a view that has a button which opens another Activity, when I close this Activity (With BackButton) and if a marker changed its position (through the service) when I was in said Activity, I find that there are two of the same marker on the map.
Any idea what I could do? Should I remove the fragment and create it again when I close the activity? is that possible? if so how should I proceed?
Here you can try 2 options
Try creating a static variable and and store some flag values.
check whether the Fragment is visible with the help of isVisible() or getUserVisibleHint()
You must be using this method to add a marker to the map.
public final Marker addMarker (MarkerOptions options);
take one variable of type Marker and store the reference of it which is returned by addMarker (MarkerOptions options);
like
if(marker!=null){
marker.remove(); //will clear the previous one, it might be null at first.
}
marker=addMarker(yourOptions);//will add the latest one

How to load Google map in background in android?

I have fragment for map in XML file as below,
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</fragment>
In which activity I am displaying the Google map, that is not a main activity. I am redirecting to map activity from my main activity on button click. There is no any problem with map. But when I click on button of main activity then the process stops for sometime and suddenly black screen arrives in front and after sometime, map activity displays the map. It is not expected behaviour of application. Is there any way to load that map after map activity loaded? I mean loading map in background?
Use getMapAsync() (introduced in Google Play Services v6.5.87) instead of getMap() then Implement OnMapReadyCallback on your fragment:
#Override
public void onMapReady(GoogleMap map) {
// do something with map
}

Map of MapFragment gets loaded with lag when returning from another activity

As far as I can see, MapFragment has an issue with transition animations. All views on the layout are getting shown immediately, including the MapFragment's own views (like zoom buttons). But the map itself gets loaded with a lag only after the animation is completed.
In order to illustrate the problem, I did the following:
I changed one of the Activities in the Google maps android API examples slightly. It opens a blank activity via an Action Item. When I click back button, the map gets loaded, but only after the transition is completed.
I exaggerated the transition effect a little bit, so that you can see the problem better. I set the transition animation speed in Developer Options to 5x. Even on 1x speed, this lag is disturbing though.
See this video: http://www.youtube.com/watch?v=12SEotktlXI
Do you have any suggestion to prevent this lag? Why do all views get loaded immediately but the map itself doesn't?
Testing environment: Nexus 5, Android 4.4.2, unrooted
Edit: This problem also occures when MapView is used instead of MapFragment.
Reason: Its because as soon as you settings activity will be shown, the map activity will be on its onpause() state; thus, I assume android management reclaimed the memory from the map activity.
Solution:
Make a static class and declare you map statically there, to avoid android from reclaiming the memory used by your map.
Ex.
//your static class
public class MapData{
public static GoogleMap map;
}
//your map activity
public class MapActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(MapData.map != null)
MapData.map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}

Android: Google Maps API v2 rendering issue with markers and camera animation

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);
}

Destroy MapView

I have a MapActivity where you can switch between MapView (Google Maps) and OfflineMapView (my class, shows a map previously downloaded to SD card). When switching between maps I want to completely destroy and create the map views so that only one map view is in the memory. I want this for 2 reasons:
My OfflineMapView takes most of available memory for caching tiles.
Google MapView has some attached threads which I don't want running when OfflineMapView is visible.
I tried to remove the MapView from layout and null my reference to it but when I want to show it again I get an exception saying that MapActivity can have only one MapView.
EDIT:
The presence of Google MapView (visibility is set to GONE) doesn't have any effect on OfflineMapView FPS. I didn't get any OutOfMemoryErrors either.
Use ActivityGroup as your Activity class and have it start and stop sub-activities for each type of map. For example, to get the view for the Google Map:
Intent intent = new Intent(this, GoogleMapActivity.class);
Window window = getLocalActivityManager().startActivity("google-map", intent);
View googleMapView = window.getDecorView();
container.addView(googleMapView);
to destroy it:
container.removeView(googleMapView);
getLocalActivityManager().removeAllActivities();
and do the same with your offline map. This should completely stop the MapActivity and it's threads.
Note that I have found LocalActivityManager.destroyActivity() to be buggy, so I used LocalActivityManager().removeAllActivities() in the example since it does work for this case.

Categories

Resources