片段中的 getMapAsync() [英] getMapAsync() in Fragment

查看:34
本文介绍了片段中的 getMapAsync()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Fragment 中实现 Google 地图时遇到问题.

I having trouble implementing Google Map in Fragment.

这是我的片段类的一部分:

This is my the part of my fragment class:

public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {

// Google Map
private GoogleMap googleMap;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(
              R.id.map)).getMapAsync(this);

我在 getMapAsync(this) 中遇到错误.我告诉不兼容的类型.

I am getting an error in getMapAsync(this). I tells Incompatible type.

它说:

必需:com.google.android.gms.map.GoogleMap

required: com.google.android.gms.map.GoogleMap

发现:无效

顺便说一句,这是我的 xml:

BTW here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

推荐答案

在你的 XML 布局中你应该这样做

In your XML layout you should do like this

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</FrameLayout>

在你的片段中实现这个

private MapView mapView;
private GoogleMap googleMap;

覆盖 onCreateView 如果没有 onCreateView 就像下面的代码

override onCreateView if there is no onCreateView just like the code below

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.your_layout, container, false);
}

覆盖 onViewCreated() 里面的onViewCreated() 把这个

override onViewCreated() inside that onViewCreated() put this

mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment

当你已经在你的片段中实现了 OnMapReadyCallback 时,把这个/代码像这样

when you already implement OnMapReadyCallback in your fragment put this/code like this

@Override
public void onMapReady(GoogleMap map) {
    googleMap = map;
}

希望对你有帮助.

这篇关于片段中的 getMapAsync()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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