12

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!

raluca
  • 458
  • 3
  • 10
  • I couldn't find a workaround this so I went for a mapFragment and it works like a charm. What I want to point out is that both the credentials and the versions of the APIs used are not the one that are causing the issue..it would be nice to know what might be causing this issue. – raluca Oct 05 '15 at 19:58
  • Very similar question unanswered one year ago: http://stackoverflow.com/questions/22144439/android-google-map-not-loading-in-activity-small-layout – raluca Oct 05 '15 at 20:10
  • 1
    `Mapview` will deprecated and `MapFragment` is the way. – bjiang Oct 05 '15 at 22:36

2 Answers2

24

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.

Pei
  • 11,452
  • 5
  • 41
  • 45
raluca
  • 458
  • 3
  • 10
  • 2
    *It is counterintuitive to me why this methods might affect showing the map* onResume is called before the view is visible, there is probably some magic happening in mapView.onResume – Tim Oct 06 '15 at 13:55
5

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
            }
        }
    }
}
Punpuf
  • 2,042
  • 1
  • 19
  • 30
  • thank you :) itemMap.onResume(); mapView.onEnterAmbient(null); This worked for me in recyclerview's adapter while initialising mapview – Apurva Kolapkar Apr 19 '17 at 06:23