【问题标题】:How to implement Google Maps within a Fragment and use the GoogleMap object?如何在 Fragment 中实现 Google Maps 并使用 GoogleMap 对象?
【发布时间】:2015-01-07 22:04:54
【问题描述】:

如何在 Fragment 中实现 GoogleMap?

我正在尝试开发相同的东西,但是 getMap() 无法执行,因为 SupportMapFragment (执行 (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(‌​R.id.map) 行时)返回一个空对象。

我尝试过扩展 MapFragment 和 SupportMapFragment 而不是 Fragment,但它们不起作用...

我只是不想创建一个新活动来显示地图:我只想显示一个地图而不是我的主要活动。我可以实现我的目标吗?

我有一个扩展 ActionBarActivity 的主要活动,它替换容器内的片段(取决于抽屉上的用户选择,X 或 Y 片段被替换并显示)。我拥有 2 个正在工作的片段,但我无法实现在片段中操作 GoogleMap 对象的目标。

这是我的 mapa.xml

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

这是我的 FragmentMapa 类:

  import com.google.android.gms.maps.GoogleMap;
   import com.google.android.gms.maps.SupportMapFragment;
   import com.ingartek.bizkaimove.R;

   import android.app.Activity;
   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;

 public class FragmentMapa extends Fragment {

private GoogleMap mMapa;
    
public FragmentMapa() {
    // TODO Auto-generated constructor stub
}

@Override
public void onAttach (Activity activity) {
    super.onAttach(activity);
}

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

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    View rootView = inflater.inflate(R.layout.mapa, container, false);
    
    mMapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapaPrincipal)).getMap();      
    
    return rootView;
    
}

突出显示的行不起作用,因为 SupportMapFragment 对象为空,因此 getMap() 崩溃。

有什么帮助吗?提前致谢。

我的问题的解决方案

对于遇到像我这样的问题的任何人:您不应该使用 getSupportFragmentManager(),而应该使用 getChildFragmentManager()。应该保持这种状态:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    View rootView = inflater.inflate(R.layout.mapa, container, false);
    
    setupMapIfNeeded();
    
    return rootView;
    
}

private void setupMapIfNeeded() {

    if( mMapa == null ){
        SupportMapFragment smf = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);
    
        if( smf != null ){
            Toast.makeText(getActivity(), "Let's go!!", Toast.LENGTH_SHORT).show();
            mMapa = smf.getMap();
        }else{
            Toast.makeText(getActivity(), "SMF is null...", Toast.LENGTH_SHORT).show();
        }
        
        if( mMapa != null ){
            
            setupMap();
            
        }
    
    }
    
}

private void setupMap() {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), "Configure the map", Toast.LENGTH_SHORT).show();
}

【问题讨论】:

标签: java android google-maps android-fragments google-maps-android-api-2


【解决方案1】:

您好,可以参考我的回答here

你需要这样调用...

SupportMapFragment  fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);

mMapa = fragment.getMap();

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2015-07-10
    • 2021-10-31
    • 2018-02-26
    • 2019-10-25
    相关资源
    最近更新 更多