1

Im making an app that shows map in card view with recycler view. Finally i get the result what i want. But i have some problems. I need some help to resolve it

I dont know where i have to initialize the google map. - so options and map location not applied directly. when app launch after scroll it one or twice

Here is my Recycler Adapter Code

public class EqListAdapter extends RecyclerView.Adapter<EqListAdapter.MyViewHolder>{
Context mContext;
EqData eqData;
View convertView;

public EqListAdapter(Context mContext, EqData eqData) {
    this.mContext = mContext;
    this.eqData = eqData;
}

@Override
public EqListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(EqListAdapter.MyViewHolder holder, int position) {
    final float str = eqData.result.get(position).strength;
    final LatLng latlng = new LatLng(eqData.result.get(position).lat,eqData.result.get(position).lon);
    final String loc = eqData.result.get(position).location;
    final String time = eqData.result.get(position).date;
    GoogleMap thismap = holder.gMap;
    if (thismap != null){
        thismap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 13));
        thismap.addMarker(new MarkerOptions().position(latlng));
        thismap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {

            }
        });
        thismap.getUiSettings().setMapToolbarEnabled(false);
    }
    holder.locationText.setText(loc);
    holder.strText.setText(""+str);
    holder.timeText.setText(time);
}

@Override
public void onViewRecycled(MyViewHolder holder) {
    super.onViewRecycled(holder);
    // Cleanup MapView here?
  /* if (holder.gMap != null)
    {
        holder.gMap.clear();
        holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    }
}*/

@Override
public int getItemCount() {
    return this.eqData.result.size();
}

class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback
{
    TextView strText;
    TextView locationText;
    TextView timeText;
    MapView map;
    GoogleMap gMap;
     MyViewHolder(View itemView) {
        super(itemView);
         map = (MapView)itemView.findViewById(R.id.mapFragment);
        strText = (TextView)itemView.findViewById(R.id.strText);
        locationText = (TextView)itemView.findViewById(R.id.locationText);
        timeText = (TextView)itemView.findViewById(R.id.timeText);

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

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(mContext);
        gMap = googleMap;
    }
}

Where i have to initialize and set location to solve this problem

whtjs
  • 49
  • 6

1 Answers1

0

You may want to start by calling MapsInitializer. As mentioned in the documentation, it must be called because some classes such as BitmapDescriptorFactory and CameraUpdateFactory need to be initialized.

However, if you are using MapFragment or MapView and have already obtained a (non-null) GoogleMap by calling getMapAsync() on either of these classes and waiting for the onMapReady(GoogleMap map) callback, then you do not need to worry about this class.

Additionally, if you're referring to Google Maps set in lite mode, you can follow these ways:

  • Either as an XML attribute for a MapView or MapFragment
  • Or in the GoogleMapOptions object

You can use intents to launch a map view and life cycle events wherein, as discussed in Google Maps Android API documentation,

When using the API in fully interactive mode, users of the MapView class must forward all the activity life cycle methods to the corresponding methods in the MapView class.

When using the MapView class in lite mode, forwarding lifecycle events is optional, except for the following situations:

  • It is mandatory to call onCreate(), otherwise no map will appear.
  • If you wish to show the My Location dot on your lite mode map and use the default location source, you will need to call onResume() and onPause(), because the location source will only update between these calls. If you use your own location source, it's not necessary to call these two methods.

You may find link to sample codes here.

Lastly, please check solution in this SO post. It might also help.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22