Google Maps Lite Mode causes jank in RecyclerView –

Development issue/problem:

I have a RecyclerView which is a vertically scrolling list of items. Each item in the list contains Google Maps V2 MapView in Lite mode. I’m using this new feature, which returns bitmaps instead of a scaled map, to replace the Google Static Maps API.

For MapView, you must invoke OnCreate(), onResume(), onPause(), onDestroy(), etc. of the corresponding Activity/Fragment parent method. Where should it be called from RecyclerView.Adapter and/or RecyclerView.ViewHolder?

How can I delete recycled MapViews so that the memory doesn’t run out and the list stays free?

According to Google, the Lite mode can be used in lists:

ListItem.xml.

RecyclerView.adapter and ViewHolder

public class NearbyStopsAdapter extends RecyclerView.Adapter {

Last Closed mContext ;

The ViewHolder public class extends RecyclerView.ViewHolder implements View.OnClickListener {

MapView ;

public ViewHolder(view) {
super(view);
map = (MapView) view.findViewById(R.id.mapImageView);
// Should it be created here?
folder.onCreate(null);
folder.onResume();
}
}

public NearbyStopsAdapter(Context c) {
this.mContext = c;
}

@Public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_nearby_stop, viewGroup, false);
return new ViewHolder(itemView);
}

@ Cancel public blank onBindViewHolder(ViewHolder holder, int position) {
//Async Map call here?
holder.getMapAsync(this);
}

@Public Void Review onViewRecycled(viewer holder) {
// clearing mapSee here?
// if (holder.map != null) {
// holder.map.onPause();
// holder.map.onDestroy();
// }
}

@Define public void onViewAttachedToWindow(holder ViewHolder) {
// set folderView here ?
// holder.folder.onCreate(null);
// holder.folder.onResume();
}

@ViewDetachedFromWindow(holder ViewHolder) {
// delete mapView here ?
// if (holder.map != null) {
// holder.map.onPause();
// holder.map.onDestroy();
// }
}

// …
}

Logkat:

I/Google Maps PI﹕ Version of the Google Play Service Pack : 659943W/Google Maps PI﹕ Downloaded Map Callback is not supported in LiteW/Google Maps PI﹕ Buildings are not supported in LiteW/Google Maps PI﹕ Indoor calls are not supported in LiteW/Google Maps PI﹕ Change modes are not supported in LiteW/Google Maps PI﹕.

Update : (June 8, 2018) Google has released sample code for using Lite Maps in the ListView application. See here.

How can I solve this problem?

Solution 1:

The solution is this:

  1. Implement OnMapReadyCallback in the ViewHolder class.
  2. Call MapsInitializer.initialize in onMapReady so that the Gaurant functions can be used before the map is retrieved.

Use this class to initialize the Google PI Map when you need to use features before retrieving the map. It needs to be named because some classes like BitmapDescriptorFactory and CameraUpdateFactory need to be initialized.

  1. Recycling card with onViewRecycled.

public class NearbyStopsAdapter extends RecyclerView.Adapter {

@Override
public void opBindViewHolder(ViewHolder holder, int position)
{
//get ‘location’ by ‘position’ from data list
//get GoogleMap
GoogleMap thisMap = holder.gMap ;
//Move map to ‘location’
if(thisMap != invalid)
//Move map to ‘location’
thisMap.moveCamera(…) ;
}

// GoogleMap recycling for list item
@Override
public void onViewRecycled(ViewHolder holder)
{
// Clear MapView here?
if (holder.gMap != null)
{
holder.gMap.clear();
holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}

The ViewHolder public class extends RecyclerView.ViewHolder implements the OnMapReadyCallback option {

GoogleMap gMap;
MapView map;
… …

public ViewHolder(view) {
super(view) ;
map = (MapView) view.findViewById(R.id.mapImageView) ;

if (map != null)
{
map.onCreate(null);
map.onResume();
map.getMapAsync(this);
}

}

@General
public void onMapReady(GoogleMap googleMap) {
//Initialize the Google Maps PI if you need to use features before getting the map
MapsInitializer.initialize(getApplicationContext());
gMap = googleMap ;

// You can move the map here to location
int pos = getPosition();
//get location by pos from the data list
// then move to location
gMap.moveCamera(….) ;

… …
}

}
}

Solution 2:

According to Google:

When the API is used in fully interactive mode, users of the MapView class must redirect all activity lifecycle methods to the corresponding methods of the MapView class. Examples of lifecycle methods are onCreate(), onDestroy(), onResume(), and onPause().
When using the MapView class in Lite mode, the transfer of lifecycle events is optional, except in the following situations:
Make sure you call onCreate(), otherwise the map will not be displayed.
If you want to display a My Location point on the map in Lite mode and use the default location source, you must make calls toResume() and toPause() because the location source is updated only between these calls. If you are using a custom location source, it is not necessary to call both methods.

So in Lite mode, you don’t have to worry about the onDestroy(), onResume() and onPause() functions.

Solution 3:

Example of PI Lite mode of Google Maps Search (source code)

Google Map Lite - github.com

Solution 4:

Google Maps offers a Lite mode

The card SDK for Android can be used as a bitmap image and offers the user limited interactivity. It’s called a simple fashion card.

simple manner

Follow LiteListDemoActivity : Efficient mapping in ListViews using Lite mode as an example.

Solution no. 5:

There is a need for a separate class for viewers. The RecyclerView adapter class only has the OnCreateViewHolder() and OnBindViewHolder() functions.

Your layout file should look like this:

And as usual, onCreate(), onDestroy() are called in the activity class.

Follow this guide for a complete overview.

Solution no. 6:

I removed this workaround because it returns a blank card every time I test it, even though it works fine in my RecyclingerView.

@Recycled
public void onViewRecycled(Viewer holder)
{
// Clear MapView here?
if (holder.gMap != invalid)
{
holder.gMap.clear();
holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}

You can also try it if the above code does not work for you.

Good luck!

Related Tags:

 google maps lite mode androidgoogle maps in recyclerviewgoogle maps api lite modegoogle maps android litegoogle maps sdk lite modehow to load google map faster in androidandroid mapview xml attributesgoogle earth lite

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *