MapView.onMapReady从未在Fragment中调用Map Map中的Map Map [英] MapView.onMapReady never called in Fragment for load Google Map in MapView

查看:505
本文介绍了MapView.onMapReady从未在Fragment中调用Map Map中的Map Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尝试在名为 RoeteFragment 的片段中的Android应用程序中显示地图。如果我调试我的代码,我发现方法 onMapReady 永远不会调用,因此映射不会加载。



这个片段实现 OnMapReadyCallback 就像需要的一样,在 onCreateView 中获得 MapView 并调用 getMapAsync 。如果我在该方法上放置了一个断点,它总是会被命中, onMapReady 中的断点从未被命中。没有异常抛出。



我还在 res / values / google_maps_api.xml 文件中使用了有效的Google Maps API密钥c $ c $。

以下是我的代码:

 < ;?xml version =1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =match_parent >

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

< / RelativeLayout>





  public class RoeteFragment extends Fragment实现了OnMapReadyCallback {

私有MapView mapView;
private static Roete _roete;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_roete,容器,假);
mapView =(MapView)view.findViewById(R.id.map);
mapView.getMapAsync(this);
返回视图;
}

@Override
public void onMapReady(GoogleMap googleMap){

if(_roete!= null&& _roete.getAankomstLocatie() != null&& _roete.getVertrekLocatie()!= null){
LatLng aankomst = new LatLng(_roete.getAankomstLocatie()。getLatitude(),_roete.getAankomstLocatie()。getLongitude());
googleMap.addMarker(new MarkerOptions()。position(aankomst).title(aankomst));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(aankomst));

LatLng vertrek = new LatLng(_roete.getVertrekLocatie()。getLatitude(),_roete.getVertrekLocatie()。getLongitude());
googleMap.addMarker(new MarkerOptions()。position(vertrek).title(vertrek));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(vertrek));



public static Fragment newInstance(){
return new RoeteFragment();
}

public static Fragment newInstance(Roete roete){
_roete = roete;
return newInstance();






$ b

你可以在我的代码中填写错误吗?

解决方案

阅读 MapView 文档。尤其是:


此类的用户必须从 Activity 转发所有生命周期方法>或者 Fragment 包含这个视图到这个类中相应的视图。特别是,您必须转发以下方法:

$ ul $ $ $ $ $ $ c $ onCreate(Bundle)

  • onResume()

  • onPause() onDestroy()

  • onSaveInstanceState )

  • onLowMemory()



  • I'll try to show a map in my Android application on a fragment named RoeteFragment. If I debug my code I see that the method onMapReady is never called so that the map will not load.

    The fragment implements OnMapReadyCallback like needed and in the onCreateView I get the MapView and call getMapAsync. If I place a breakpoint on that method, it will always been hit, the breakpoint inside onMapReady never been hit. There is no exception thrown.

    I've also a valid Google Maps API key in the file res/values/google_maps_api.xml.

    Here is my code:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.gms.maps.MapView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/map" />
    
    </RelativeLayout>
    

    public class RoeteFragment extends Fragment implements OnMapReadyCallback {
    
        private MapView mapView;
        private static Roete _roete;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_roete, container, false);
            mapView = (MapView) view.findViewById(R.id.map);
            mapView.getMapAsync(this);
            return view;
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
    
            if (_roete != null && _roete.getAankomstLocatie() != null && _roete.getVertrekLocatie() != null) {
                LatLng aankomst = new LatLng(_roete.getAankomstLocatie().getLatitude(), _roete.getAankomstLocatie().getLongitude());
                googleMap.addMarker(new MarkerOptions().position(aankomst).title("aankomst"));
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(aankomst));
    
                LatLng vertrek = new LatLng(_roete.getVertrekLocatie().getLatitude(), _roete.getVertrekLocatie().getLongitude());
                googleMap.addMarker(new MarkerOptions().position(vertrek).title("vertrek"));
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(vertrek));
            }
        }
    
        public static Fragment newInstance() {
            return new RoeteFragment ();
        }
    
        public static Fragment newInstance(Roete roete) {
            _roete = roete;
            return newInstance();
        }
    }
    

    Could you file the bug in my code?

    解决方案

    Read MapView documentation. Especially:

    Users of this class must forward all the life cycle methods from the Activity or Fragment containing this view to the corresponding ones in this class. In particular, you must forward on the following methods:

    • onCreate(Bundle)
    • onResume()
    • onPause()
    • onDestroy()
    • onSaveInstanceState()
    • onLowMemory()

    这篇关于MapView.onMapReady从未在Fragment中调用Map Map中的Map Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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