Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Juan Manuel Amoros Avatar asked Jan 07 '23 19:01

Juan Manuel Amoros


1 Answers

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

}
}
like image 160
Zirk Kelevra Avatar answered Jan 21 '23 06:01

Zirk Kelevra