用自定义支持片段

时间:2018-04-22 22:16:00

标签: android google-maps google-maps-api-2

尝试将我的Google依赖项从9.0.0更新为15.0.0。但我必须更新

getMap();

getMapAsync();

因为getMap();在最新版本的Google图书馆中已弃用。

问题是我使用自定义片段加载视图。我这样做是因为我有3种不同的地图类型,可以根据用户选择加载到地图视图中。但它并没有让我毫无错误地进行getMapAsync。

这是我的代码:

private void setUpMapIfNeeded() {
    if (mMapFragment == null) {
        mMapFragment = new CustomSupportMapFragment(); 

        mMapFragment.setOnMapCreatedListener(new CustomSupportMapFragment.MapViewCreatedListener() {

            @Override
            public void onMapCreated() {
                boolean needUpdate = mMap == null;
                mMap = mMapFragment.getMapAsync();

                if (mMap != null)
                    setUpMap(needUpdate);
            }
        });

    }

    FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.replace(R.id.map, mMapFragment);
    ft.commit();
}

但我收到错误:

getMapAsync (OnMapReadyCallback) in SupportMapFragment cannot be applied to ()

所以我尝试像这样添加 this getContext()

getMapAsync(this);

getMapAsync(getContext));

但两人都有错误,但无法应用。

有什么想法吗?对不起,我对java的理解是新的,我正在学习。我尝试搜索StackOverflow和谷歌已经解决了这个问题,但只能在使用常规supportmapfragment而不是自定义supportmapfragment时找到解决方案。

这是我的setUpMap方法,如果它有助于你理解:

private void setUpMap(boolean needUpdate) {
    boolean location_permission_granted = (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
    if (!location_permission_granted) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
    } else {
        mMap.setMyLocationEnabled(true);
    }

    mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {

            if (LOAD_PARTICULAR_MAP == 1) {
                if (cameraPosition.zoom > AppConstants.MAX_ZOOM) {
                    CameraUpdate upd = CameraUpdateFactory.newLatLngZoom(cameraPosition.target, AppConstants.MAX_ZOOM);
                    mMap.moveCamera(upd);
                }


                float maxZoom = 4f;
                if (cameraPosition.zoom > maxZoom) {
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
                    return;
                }
                float minZoom = 0f;
                if (cameraPosition.zoom < minZoom) {
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(minZoom));
                    return;
                }
            }
        }
    });
    mMap.setOnMapLongClickListener(this);
    mMap.setOnMyLocationButtonClickListener(this);
    mMap.setOnMapClickListener(this);
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMarkerClickListener(this);
//        mBtnMapType.setVisibility(View.VISIBLE);

    mMap.getUiSettings().setScrollGesturesEnabled(true);

    //mBtnLegend.setVisibility(View.VISIBLE);
    //updateLegendButton();
    updateMapType(mSettings.getMapType(GoogleMap.MAP_TYPE_TERRAIN));
    /*mTileProvider = new TranslucentUrlTileProvider(AppConstants.SERVER_URL, AppConstants.SERVER_URL_FREE);
//        mTileProvider.setPro(!mIsPro.showPurchaseDialog());
    //mTileProvider.setOpacity(mOpacity);
    mTileOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mTileProvider));*/
    if (needUpdate)
        if (LOAD_PARTICULAR_MAP != 4) {
            gotoMyLocation();
        }
//        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()       {
//            @Override
//            public boolean onMarkerClick(Marker marker) {
//                if (marker != null) {
//                    openGoogleMap(marker);
//                    return true;
//                }
//                return false;
//            }
//        });

    switch (LOAD_PARTICULAR_MAP) {

        case 1:

            auroraOverMap();
            mMap.animateCamera(CameraUpdateFactory.zoomTo(0f));

            break;

        case 2:

            lightPollutionMap();

            break;

        case 3:

            loadCloudMap();

            break;

        case 4:

            highestValueView();
//                mMap.animateCamera( CameraUpdateFactory.zoomTo( 12f )    );

            break;
    }
}

1 个答案:

答案 0 :(得分:0)

您无法通过调用

获取GoogleMap个对象
mMap = mMapFragment.getMapAsync();

你应该使用这样的代码:

mMapFragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }
});

其中mMap是全局变量:

public class MainActivity extends AppCompatActivity {

    private GoogleMap mMap;
    ...

在您获得mMap之后,您可以使用它来设置您的地图:

mMapFragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        if (mMap != null)
                setUpMap(needUpdate);
    }
});

另外,请查看Official Tutorial