Google Map:onMapReadyCallback不能应用于此活动 [英] Google Map:onMapReadyCallback can not be applied to this activity

查看:93
本文介绍了Google Map:onMapReadyCallback不能应用于此活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是尝试使用Google地图的初学者,并且已经按照此处给出的说明进行操作:

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

但是我被困在 mapFragment.getMapAsync(this)上,无法找到答案.

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

这是代码:

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" />

推荐答案

您需要使Activity实现 OnMapReadyCallback ,并重写方法 void onMapReady(GoogleMap googleMap).

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

}

这篇关于Google Map:onMapReadyCallback不能应用于此活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆