androidgoogle-mapsmapsfragment

Loading Google Maps is too slow in Android Application


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.


Solution

  • 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));
    
    }
    }