Google Map:onMapReadyCallback无法应用于此活动

时间:2016-02-11 13:37:17

标签: android google-maps supportmapfragment

我是初学者,尝试使用谷歌地图,我已按照此处给出的说明进行操作:https://developers.google.com/maps/documentation/android-api/map?hl=zh-tw

但是我坚持mapFragment.getMapAsync(this)并且无法找到答案。

以下是代码:

public class GoogleMapPage extends FragmentActivity   {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.googlemap);
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this)<---it said "onMapReadyCallback can not be applied to this activity;
}

}

这是XML:

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">


<fragment
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    tools:layout="@layout/googlemap" />

1 个答案:

答案 0 :(得分:4)

您需要使您的活动实施OnMapReadyCallback并覆盖方法void onMapReady(GoogleMap googleMap)

public class GoogleMapPage extends FragmentActivity implements OnMapReadyCallback   {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.googlemap);
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this) // <---it said "onMapReadyCallback can not be applied to this activity;
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        // Do stuff with the map here!
    }

}