0

I have an application which required a Two different layouts in one recyclerview. I already done that. The first layout is for map.I am using mapview for that. And the second one is the lists of places.

My problem is the getMap(); is deprecated.Is there any option I could use?

They said if I want to have inside a fragment, I should use mapview.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="160dp"
          android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/mapHolder"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        >
        <com.google.android.gms.maps.MapView
            android:id="@+id/mapImageView"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            map:liteMode="false"
            map:mapType="normal"
            map:cameraZoom="15" />
    </RelativeLayout>
</LinearLayout>

 if (position == 0) {
                MyGoogleMap myGoogleMap = (MyGoogleMap) holder;
                GoogleMap googleMap = myGoogleMap.map.getMap();
                if (googleMap != null) {
                    for (int i = 0; i < mModelNearbies.size(); i++) {
                        MarkerOptions markerOptions = new MarkerOptions();
                        LatLng latLng = new LatLng(Double.parseDouble(mModelNearbies.get(i).getShop_lat()), Double.parseDouble(mModelNearbies.get(i).getShop_long()));
                        markerOptions.position(latLng);
                        markerOptions.title(mModelNearbies.get(i).getShop_name() + " : " + mModelNearbies.get(i).getShop_address());
                        googleMap.addMarker(markerOptions);
                    }
                }
            }

And for the OnMapReadyCallback.

class MyGoogleMap extends MyNearbyHolder implements OnMapReadyCallback {
        GoogleMap gMap;
        MapView map;

        public MyGoogleMap(View itemView) {
            super(itemView);
            map = (MapView) itemView.findViewById(R.id.mapImageView);

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

        @Override
        public void onMapReady(GoogleMap googleMap) {
            MapsInitializer.initialize(mContext);
            this.gMap = googleMap;
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                this.gMap.setMyLocationEnabled(true);
                LatLng latLng = new LatLng(14.5500, 121.0300);
                this.gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                this.gMap.animateCamera(CameraUpdateFactory.zoomTo(12));

            }
        }
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Charles Galvez
  • 1,100
  • 5
  • 19
  • 41

1 Answers1

0

Your Actual Problem :

You already have GoogleMap object in your Holder class:

class MyGoogleMap extends MyNearbyHolder implements OnMapReadyCallback {

     // See this 
     GoogleMap gMap;

    //Your code 
}

Then why you are doing myGoogleMap.map.getMap() ??? Just use the existing object on GoogleMap like below :

if (position == 0) {
        MyGoogleMap myGoogleMap = (MyGoogleMap) holder;
        GoogleMap googleMap = myGoogleMap.gMap;
        if (googleMap != null) {
            for (int i = 0; i < mModelNearbies.size(); i++) {
                MarkerOptions markerOptions = new MarkerOptions();
                LatLng latLng = new LatLng(Double.parseDouble(mModelNearbies.get(i).getShop_lat()), Double.parseDouble(mModelNearbies.get(i).getShop_long()));
                markerOptions.position(latLng);
                markerOptions.title(mModelNearbies.get(i).getShop_name() + " : " + mModelNearbies.get(i).getShop_address());
                googleMap.addMarker(markerOptions);
            }
        }
    }

Taking about replacing deprecated getMap(). You have already done that. by using map.getMapAsync(this); in your Holder class. Because getMapAsync is the replacement of getMap.

See this question for more info : Replace getMap with getMapAsync

Community
  • 1
  • 1
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59