【问题标题】:getMap() is deprecatedgetMap() 已弃用
【发布时间】:2016-06-22 09:05:29
【问题描述】:

我有一个应用程序需要在一个回收站视图中使用两种不同的布局。我已经这样做了。第一个布局是用于地图的。我为此使用地图视图。第二个是地点列表。

我的问题是 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));

            }
        }
    }

【问题讨论】:

    标签: android google-maps android-mapview


    【解决方案1】:

    您的实际问题:

    您的 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);。因为 getMapAsyncgetMap 的替代品。

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

    【讨论】:

    • 我试过后回复你。我的 genymotion 谷歌地图有问题。谢谢
    • 你检查了吗?? @CharlesGalvez
    • 很抱歉。我正在检查它。
    • 它不工作。我的 if 语句在 onBindViewHolder 内。
    • 它现在可以工作了。我将代码放在 OnMapReady 中。如何简化这个? MyGoogleMap myGoogleMap = (MyGoogleMap) 持有者;我只把它放在我的 if 语句中的债券持有人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多