[GIS] Google maps marker is not getting displayed

androidgoogle mapsmarkers

I am trying to learn google map API. I see a map but I am unable to get a marker. Here is my code

Activity

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_page);
        GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.addMarker(new MarkerOptions()
                .position(new LatLng(37.7750, 122.4183))
                .title("San Francisco"));
    }

}

home_page.xml

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

What am I missing out?

Best Answer

Probably map is null after getMap(). it needs some time to get initialised. Try to wait somehow, or put addMarker into onResume().