简体   繁体   English

Fragment中的getMapAsync()

[英]getMapAsync() in Fragment

I having trouble implementing Google Map in Fragment. 我在Fragment中实现Google Map时遇到了麻烦。

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

I am getting an error in getMapAsync(this). 我在getMapAsync(this)中收到错误。 I tells Incompatible type. 我告诉不兼容的类型。

it says: 它说:

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

found: void 发现:无效

BTW here is my xml: 顺便说一句,这是我的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>

In your XML layout you should do like this 在您的XML布局中,您应该这样做

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

inside your fragment implement this 在你的片段里面实现这个

private MapView mapView;
private GoogleMap googleMap;

override onCreateView if there is no onCreateView just like the code below 如果没有onCreateView ,则覆盖onCreateView ,就像下面的代码一样

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

override onViewCreated() inside that onViewCreated() put this 覆盖onViewCreated()onViewCreated()内把这个

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

when you already implement OnMapReadyCallback in your fragment put this/code like this 当你已经在你的片段中实现OnMapReadyCallback时 ,将这个/代码放在这样的代码中

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

hope this helps you. 希望这能帮助你。

After a few overheats in my head, thanks to this post and https://stackoverflow.com/a/34732804/3925554 , I would say that currently there are two ways of adding a MapFragment to your app: 在我的头脑中出现几点过热之后,感谢这篇文章和https://stackoverflow.com/a/34732804/3925554 ,我想说目前有两种方法可以将MapFragment添加到你的应用中:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
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"/>

By this way, you have to use a standard fragment and implement this inside. 通过这种方式,您必须使用标准片段并在其中实现。 Actually you are inserting a fragment into another: 实际上你是在另一个片段中插入一个片段:

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);
}

The other way would be using the real SupportMapFragment and implement it in this manner: 另一种方法是使用真正的SupportMapFragment并以这种方式实现它:

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getMapAsync(this);
}

And they work without having to set a layout in onCreateView neither call mapView.onCreate(savedInstanceState); 而且他们无需在onCreateView设置布局就可以调用mapView.onCreate(savedInstanceState); and mapView.onResume(); mapView.onResume(); manually inside onViewCreated() , what is not recommended anyway. 手动在onViewCreated()内部,无论如何都不推荐。

Hope it helps! 希望能帮助到你!

 SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
 FragmentTransaction fragmentTransaction =
 getChildFragmentManager().beginTransaction();
 fragmentTransaction.add(R.id.flMap, mMapFragment);
 fragmentTransaction.commit();
 mMapFragment.getMapAsync(this);

Your code is returning this error because getMapAsync() does not return anything. 您的代码返回此错误,因为getMapAsync()不返回任何内容。 If you look at the documentation for this function: 如果您查看此功能的文档:

public void getMapAsync (OnMapReadyCallback callback) public void getMapAsync(OnMapReadyCallback callback)

Sets a callback object which will be triggered when the GoogleMap instance is ready to be used. 设置一个回调对象,该对象将在准备好使用GoogleMap实例时触发。

Note that: 注意:

This method must be called from the main thread. 必须从主线程调用此方法。 The callback will be executed in the main thread. 回调将在主线程中执行。 In the case where Google Play services is not installed on the user's device, the callback will not be triggered until the user installs it. 如果用户的设备上未安装Google Play服务,则在用户安装回调之前不会触发回调。 In the rare case where the GoogleMap is destroyed immediately after creation, the callback is not triggered. 在创建后立即销毁GoogleMap的极少数情况下,不会触发回调。 The GoogleMap object provided by the callback is non-null. 回调提供的GoogleMap对象为非null。

OnMapReadyCallback is an interface that needs implemented and passed to through this function. OnMapReadyCallback是一个需要实现并通过此函数传递的接口。 Nothing is currently assigned to your googleMap variable you should instead set its value in this block of code which implements OnMapReadyCallback 目前没有任何内容分配给您的googleMap变量,您应该在implements OnMapReadyCallback的代码块中设置其值

@Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;


    }

This code should be in your Fragment somewhere. 这段代码应该在你的片段中。 Since your Fragment already implements this interface you do pass it in as this . 由于您的片段已经实现了这个接口,你在把它作为this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM