1

我在我的应用程序中添加了一个地图片段,并添加了以下代码以在片段上放置一个标记。但我在代码片段中遇到了错误。我已经包含了所有相关的导入。

错误是编译时的,如下所示:

1.MapFragment 无法解析为类型。

2.GoogleMap 无法解析为类型。

3.LatLng 无法解析为类型。

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

任何人都知道代码段中的错误在哪里?谢谢

4

1 回答 1

3

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"

于 2013-03-19T21:36:45.070 回答