谷歌地图加载缓慢的内部片段

时间:2016-10-31 13:56:09

标签: android android-fragments android-mapview

我有片段的主要活动,其中一个片段是Map Fragment。现在,当我点击那里时,每次打开都很慢。我正在使用片段的单例实例,但它仍然无法按预期工作。这是一个很好的课程:

    public MapsFragment() {
            gson = new Gson();
        }

        public static MapsFragment getInstance() {
            if (mInstance == null)
                mInstance = new MapsFragment();
            return mInstance;
        }    


        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
            final View v = inflater.inflate(R.layout.fragment_maps, container, false);
            mOnSavedinstance = savedInstanceState;
            mMapView = (MapView) v.findViewById(R.id.map);
            mMapWrapperLayout = (MapWrapperLayout) v.findViewById(R.id.map_relative_layout);
            mMapView.getMapAsync(MapsFragment.this);
            mMapView.onCreate(savedInstanceState);
            mMapView.requestFocus();
            MapsInitializer.initialize(getActivity());
            return v;
        }

 @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        // MapWrapperLayout initialization
        // 39 - default marker height
        // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge
        mMapWrapperLayout.init(mGoogleMap, Constants.MARKER_HEIGHT);

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            mGoogleMap.setMyLocationEnabled(false);
        }
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            setUpMap();

    }

 private void setUpMap() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final ArrayList<MarkerOptions> markerOptionses = new ArrayList<MarkerOptions>();
                int counter = 0;
                for (final Places places : Model.getInstance().getPlaces()) {
                    LatLng location = new LatLng(Double.parseDouble(places.getLat()), Double.parseDouble(places.getLon()));
                    final MarkerOptions options = new MarkerOptions();
                    options.snippet(gson.toJson(places));
                    options.position(location);
                    options.title(String.valueOf(counter));
                    markerOptionses.add(options);
                    counter++;
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (MarkerOptions markerOptionse : markerOptionses) {
                            mGoogleMap.addMarker(markerOptionse).setIcon(BitmapDescriptorFactory.fromResource(Model.getInstance().getPlaces()
                                    .get(Integer.parseInt(markerOptionse.getTitle())).getMapsRes(getActivity())));
                        }
                    }
                });


                final CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(45.4654, 9.1859))      // Sets the center of the map to location user
                        .zoom(Constants.CAMERA_ZOOM_LOCATION)          // Sets the zoom
                        .build();                   // Creates a CameraPosition from the builder

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        mGoogleMap.setOnMapClickListener(MapsFragment.this);
//                            mGoogleMap.setOnInfoWindowClickListener(mInfoListener);
                    }
                });
            }
        }).start();
    }

 @Override
    public void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mGoogleMap.clear();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试仅在可见时添加标记吗?我的意思是:首先移动相机开始位置并设置zoomLevel,然后添加标记,但仅限于它在可见区域。并在相机上更新标记移动。