1

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?

1

getMap() is deprecated. Use callback setOnMapLoadedCallback which is called when the map finishes loading.

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {

//Add markers here
                    googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));

                }
            });
0

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

0

I'd the same error here. Instead to use setContentView(), I've used intent.

I call the Navigator activity from main_activity, using a button.

Try this:

Intent intent = new Intent(this, Navigator.class); startActivity(intent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.