raluca
raluca

Reputation: 458

Android Map loading very slow/not loading at all in started activity until clicking on it

When starting a new map activity the map loads really slow and doesn't start the loading until clicking the screen.

The Layout is the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.android.momintuition.DirectionsActivity">

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mapView"
        android:paddingTop="62px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        map:uiCompass="true"
        map:zOrderOnTop="true"
        map:uiZoomControls="true"
        android:background="#00000000" />
</RelativeLayout>

The new activity looks like this:

public class DirectionsActivity extends AppCompatActivity implements OnMapReadyCallback {


    GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_directions);
        MapView mv = (MapView) findViewById(R.id.mapView);
        mv.onCreate(savedInstanceState);
        mMap = mv.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
         this.map = googleMap;
         CameraUpdate cameraUpdate =
                                CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
         map.animateCamera(cameraUpdate);
    }
}

I am new to Android. Am I missing something? Thank you!

Upvotes: 12

Views: 7097

Answers (2)

raluca
raluca

Reputation: 458

The problem in this case was that I didn't implement the following methods(even if it was specified in the documentation):

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

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

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

It is counterintuitive to me why this methods might affect showing the map but it does fix the problem.

Upvotes: 24

Punpuf
Punpuf

Reputation: 2144

TL;DR These lines made my map load.

itemMap.onResume();
mapView.onEnterAmbient(null);

For people having the same problem:

I'm using maps inside a RecyclerView, and I was having the same problem, so I implemented racula's answer by having an ArrayList of type MapView, and I would add the mapView to the list when it was initialized.

And in the Activity Lifecycle methods I would get the mapViewList from the adapter, and call the appropriate method. And now the map was loading when I resumed the application.

    //In the Activity
    @Override
    protected void onResume() {
        super.onResume();
        for(MapView mapView : adapter.getMapViewList()){
            if (mapView != null){                   
                mapView.onResume();
            }              
        }
    }

So finally I tested a few things, and added the "itemMap.onResume();" and the "mapView.onEnterAmbient(null);" methods, and the map started loading and working just fine. Here's a simple version of my Adapter class

//RecyclerView Adapter and inside it the ViewHolder
class ReminderAdapter extends RecyclerView.Adapter<ReminderAdapter.MyViewHolder> {

    private ArrayList<MapView> mapViewList;

    @Override
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ReminderAdapter.MyViewHolder holder, int position) {
        (...)
        holder.initializeMapView(new LatLng(getLat(), getLng()));
    }   

    ArrayList<MapView> getMapViewList(){
        return mapViewList;
    }     

    (...)

    class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback{

    @Override
        public void onMapReady(GoogleMap googleMap) {
            MapsInitializer.initialize(context);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
            googleMap.addMarker(new MarkerOptions().position(latLng));
            this.googleMap = googleMap;
            itemMap.onResume();           //The methods
            itemMap.onEnterAmbient(null); // that made my map work
        }

        void initializeMapView(LatLng latLng){
            if(itemMap != null){             
                this.latLng = latLng;
                itemMap.onCreate(bundle);
                itemMap.getMapAsync(this);
                mapViewList.add(itemMap); // adds the MapView to the list
            }
        }
    }
}

Upvotes: 5

Related Questions