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

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

问题描述



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

pre $ public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {

// Google Map
private GoogleMap googleMap;
private int i;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
$ b googleMap =((SupportMapFragment)getActivity()。getSupportFragmentManager()。findFragmentById(
R.id.map))。getMapAsync(this);

我在getMapAsync(this)中出错。我告诉Incompatible类型。



它表示:



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



found:void



这里是我的xml:

 <?xml version =1.0encoding =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>


解决方案

p>

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

在您的片段内执行此操作

 私人MapView地图查看; 
私人GoogleMap google地图;

如果没有像下面的代码那样的onCreateView,则覆盖onCreateView

  @Override 
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundled savedInstanceState){
//为此布局膨胀布局片段
返回inflater.inflate(R.layout.your_layout,container,false);
}

覆盖onViewCreated()中的onViewCreated()
把这个

  mapView =(MapView)view.findViewById(R.id.map); 
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this); //当你已经在片段中实现了OnMapReadyCallback时

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

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

希望这可以帮到你。


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

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

it says:

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

found: void

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>

解决方案

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>

inside your fragment implement this

private MapView mapView;
private GoogleMap googleMap;

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

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

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

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

hope this helps you.

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

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