片段中的地图

问题描述 投票:0回答:1

我想在片段中实现地图视图,这是我的代码(我使用自定义地图sdk)

public class MapFragment extends Fragment {


    private Location userLocation;
    private MainActivity mainActivity;
    private MapView map;
    private VectorElementLayer userMarkerLayer;
    private SharedPreferences sharedPreferences;



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_map, container, false);
        map = root.findViewById(R.id.mainMap);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initMap();
    }

    private void initMap() {
        // Creating a VectorElementLayer(called userMarkerLayer) to add user marker to it and adding it to map's layers
        userMarkerLayer = NeshanServices.createVectorElementLayer();
        map.getLayers().add(userMarkerLayer);
        // add Standard_day map to layer BASE_MAP_INDEX
        map.getOptions().setZoomRange(new Range(4.5f, 18f));
        mainActivity = (MainActivity) getActivity();
        Layer baseMap = NeshanServices.createBaseMap(NeshanMapStyle.STANDARD_DAY, mainActivity.getCacheDir() + "/baseMap", 10);
        map.getLayers().insert(BASE_MAP_INDEX, baseMap);
        sharedPreferences = mainActivity.getSharedPreferences(Constants.SHARED_PREFERENCES_PATH, MODE_PRIVATE);
        double y = sharedPreferences.getFloat("x",53.5f);
        double x = sharedPreferences.getFloat("y",35.2f);
        map.setFocalPointPosition(new LngLat(x, y), 0);
        map.setZoom(16, 0);
    }

}

我的问题是initMap()需要巨大的过程,(我认为是因为有getCacheDir()和insert层),并且每次我在片段之间切换时,它都需要很长时间。我有什么办法可以一次打电话给initMap()吗?

android android-studio android-fragments android-fragmentactivity
1个回答
0
投票

只需删除不需要的代码并使用它

 map?.onCreate(null)
 map?.onResume()
 map?.getMapAsync(this)

并在onMapReady()中设置所有地图条件

 override fun onMapReady(googleMap : GoogleMap?) {
        googleMap?.setMaxZoomPreference(19f)
        googleMap?.uiSettings?.isMapToolbarEnabled = false
        googleMap?.uiSettings?.isMyLocationButtonEnabled = false
        googleMap?.uiSettings?.isCompassEnabled = false
        googleMap!!.setOnCameraIdleListener(this)
    }
© www.soinside.com 2019 - 2024. All rights reserved.