不推荐使用getMap()

时间:2016-06-22 09:05:29

标签: android google-maps android-mapview

我有一个应用程序,在一个recyclerview中需要两个不同的布局。我已经这样做了。第一个布局是map.I我正在使用mapview。第二个是地方清单。

我的问题是getMap();已被弃用。我可以使用任何选项吗?

他们说如果我想要一个片段,我应该使用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);
                    }
                }
            }

对于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));

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您的实际问题:

您的Holder课程中已经有GoogleMap个对象:

class MyGoogleMap extends MyNearbyHolder implements OnMapReadyCallback {

     // See this 
     GoogleMap gMap;

    //Your code 
}

那你为什么要做myGoogleMap.map.getMap() ??? 只需使用GoogleMap上的现有对象,如下所示:

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

考虑更换已弃用的getMap()。你已经完成了   那。在您的Holder课程中使用map.getMapAsync(this);。因为   getMapAsync替代getMap

有关详细信息,请参阅此问题:Replace getMap with getMapAsync