Google Map:onMapReadyCallback can not be applied to this activity

北慕城南 提交于 2021-01-28 05:01:57

问题


I am a beginner trying to use google maps, and I have followed the instructions given here: https://developers.google.com/maps/documentation/android-api/map?hl=zh-tw

But I'm stuck at mapFragment.getMapAsync(this) and have been unable to find an answer.

Here is the code:

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;
}

}

And here is the 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:


You need to make your Activity implement OnMapReadyCallback and override the method 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!
    }

}


来源:https://stackoverflow.com/questions/35340848/google-maponmapreadycallback-can-not-be-applied-to-this-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!