Loading Google Maps is too slow in Android Application

12,469

Solution 1

You are synchronously loading the map, try doing it asynchronously, Google maps has a Map Ready callback that you can use.

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class MapPane extends Activity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_activity);

    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

}
}

Solution 2

Adding lifecycle methods and calling mapView's lifecycle methods worked for me!

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

@Override
protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}

@Override
public void onLowMemory() {
    mMapView.onLowMemory();
    super.onLowMemory();
}
Share:
12,469

Related videos on Youtube

Juan Manuel Amoros
Author by

Juan Manuel Amoros

Updated on June 04, 2022

Comments

  • Juan Manuel Amoros
    Juan Manuel Amoros almost 2 years

    In my Android Application I have a Fragment with a button. Once a the button is clicked I load another Fragment with a MapView. It all works good but the problem is that the Fragment with Google Maps lasts at least 0.5 seconds to launch once the button of the previous Fragment is clicked. Do you know another way to load google maps without stucking the Fragment transaction?

    Here is the fragment that loads Google Maps

    public class DetalleRuta extends android.support.v4.app.Fragment {
    
    private GoogleMap googleMap;
    private MapView mapView;
    
    public DetalleRuta() {
        // Required empty public constructor
    }
    
    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View v = inflater.inflate(R.layout.fragment_detalle_ruta, container, false);
    
        //Inicio el mapa
        mapView = (MapView)v.findViewById(R.id.mapa);
        mapView.onCreate(savedInstanceState);
    
        googleMap = mapView.getMap();
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
    
        return v;
    }
    
    }
    

    Maybe my smartphone is not good enough, it is a BQ aquaris E4.

    • Lal
      Lal over 8 years
      Your smartphone is the culprit..try testing your app in an other device..
    • Juan Manuel Amoros
      Juan Manuel Amoros over 8 years
      Okey, thank you for your fast reply
  • Juan Manuel Amoros
    Juan Manuel Amoros over 8 years
    I have loaded the way you have told me and now it seems to be faster. I accept your answer. Thank you very much.