onMapReady not calling even though the map display without any error
Asked Answered
R

3

43

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"/>
Roofing answered 17/12, 2014 at 17:10 Comment(0)
S
90

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

Sixteenmo answered 17/12, 2014 at 17:12 Comment(2)
@SAN: "getMapAsync() throw and error" -- then perhaps you should consider asking a Stack Overflow question to get assistance with whatever problem you had there. "so I had to use" -- so? AFAIK, you still need to call getMapAsync(). Moreover, now you have two MapFragment instances. Here is a sample project that demonstrates the use of getMapAsync() with a static MapFragment: github.com/commonsguy/cw-omnibus/tree/master/MapsV2/NooYawkSixteenmo
It will be helpful for someOne else if in xml your <fragment name="com.google.android.gms.maps.SupportFragment" then use SupportMapFragment instead of MapFragmentAmie
F
13

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); 
Fryer answered 17/12, 2014 at 17:23 Comment(0)
D
-2

Add:

mMap = map

above onMapReady

Diplopia answered 19/10, 2018 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.