onMapReady not calling even though the map display without any error

45,735

Solution 1

AFAIK, onMapReady() is triggered by a call to getMapAsync() on your MapFragment, and I do not see where you are calling that.

Solution 2

To call OnMapReady(GoogleMap map), append the following to your onCreate(...) method :

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

then, add:

// This calls OnMapReady(..). (Asynchronously)
mapFragment.getMapAsync(MapDisplay.this); 
Share:
45,735
SAN
Author by

SAN

Updated on July 22, 2022

Comments

  • SAN
    SAN almost 2 years

    I'm trying to display a simple map using Google map API v2 in an Android application. I'm following the Map API Documentation instructions. But I think the onMapReady is not calling for some reason. I'm using google-play-services_lib version 6587000. My phone has google-play-services_lib verion 6587038, I believe.

    Google map is working with initial controls. Can someone help me to correct this error?

    public class MapDisplay extends FragmentActivity
    implements OnMapReadyCallback {
    
    private GoogleMap mMap;
    private Location mCurrentLocation;
    private MarkerOptions mMarkerOptions ;
        private MapFragment mMapFragment;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.location_map);
    
        /** not needed
                mMapFragment = MapFragment.newInstance();
                FragmentTransaction fragmentTransaction =
                        getFragmentManag
    
    er().beginTransaction();
                fragmentTransaction.add(R.id.map, mMapFragment);
                fragmentTransaction.commit();*/
    
        /**corrected code*/
                MapFragment mapFragment = (MapFragment) getFragmentManager()
                        .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(this);
    
    }
    
    
    @Override
    public void onMapReady(GoogleMap map) {
    
        toast("Map ready");
        Log.d("--***** MAP  ","::Map ready");
    
        LatLng sydney = new LatLng(-33.867, 151.206);
    
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
    
        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    
    
        }
    
     private void toast(String text){
            Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
            toast.show();
         }
    
       }
    

    location_map.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.MapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>