Pregunta

I have added a map fragment to my application and added the following code to place a marker on the fragment.But I'm getting errors in the code snippet.I have included all relevant imports.

The errors are compile time and are as follows:

1.MapFragment cannot be resolved to a type.

2.GoogleMap cannot be resolved to a type.

3.LatLng cannot be resolved to a type.

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Hello world"));

Anyone have any ideas where the error is in the snippet? Thanks

¿Fue útil?

Solución

I think you should check two things first

  1. Is the Google Play Service project or library added to your project ? If not, check this link http://developer.android.com/google/play-services/setup.html
  2. If you compile for android version < 11, you need to use supportFragmentManager instead of the normal FragmentManager :

             mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

In the last case, you also need to update the class used in your XML to class="com.google.android.gms.maps.SupportMapFragment"

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top