当前位置: 首页 > 知识库问答 >
问题:

如何在片段中获取Google Maps对象

慕容越泽
2023-03-14

我在我的应用程序中使用片段,我是新手。有一个片段,我想在其中显示Google Map并想要获取它的对象,为此我在我的XML中有一个片段,我想在片段本身中膨胀它。当我试图膨胀地图视图时,它显示getSupportFragmentManager()的错误。

如何获取贴图对象,这是主要问题。我的XML如下所示:-

 <fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                class="com.google.android.gms.maps.SupportMapFragment" />

我想获取Google Map对象的片段如下:-

public class FindMyCar extends Fragment {

    private TextView mTvFind;
    private TextView mTvPark;
    private EditText mEtParkHint;

    private GoogleMap map;

    @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.findmycar, null);

        initViews(view);

        Typeface type = Typeface.createFromAsset(getActivity().getResources().getAssets(),"Multicolore.otf"); 
        mTvFind.setTypeface(type);
        mTvPark.setTypeface(type);
        mEtParkHint.setTypeface(type);

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void initViews(View view){


        mTvPark = (TextView) view.findViewById(R.id.tvFindCarPark);
        mTvFind = (TextView) view.findViewById(R.id.tvFindCar);
        mEtParkHint = (EditText) view.findViewById(R.id.etParkHint);
    }

有人能帮我获取我的地图对象,以便我可以显示当前位置并在那里绘制标记。

任何帮助都是值得的。提前谢谢。

共有3个答案

水瀚漠
2023-03-14

试试这个我觉得很好如何使用ViewPager将Google Maps V2放在一个片段上

<Fragment
            android:id="@+id/map"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
阳狐若
2023-03-14

试试这个

 GoogleMap map = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

xml

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
韦高谊
2023-03-14

getMap()方法已弃用

getMap()方法已弃用,但在play-service 9.2之后将其删除,因此最好使用getMapAsync()。仅当您不愿意为您的应用更新play-service 9.2时,您仍然可以使用getMap()。

要使用getMapAsync(),请在您的活动或片段上实现OnMapReadyCallback接口:

对于片段:

public class MapFragment extends android.support.v4.app.Fragment
      implements OnMapReadyCallback { /* ... */ }

然后,在初始化映射时,使用getMapAsync()而不是getMap():

//call this method in your onCreateMethod
 private void initializeMap() {
  if (mMap == null) {
    SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
    mapFrag.getMapAsync(this);
  }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();// do your map stuff here
}

对于活动:

public class MapActivity extends FragmentActivity
      implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():


//call this method in your onCreateMethod
 private void initializeMap() {
  if (mMap == null) {
    SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
    mapFrag.getMapAsync(this);
  }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();// do your map stuff here
}

XML中的片段:

   <fragment  xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 类似资料:
  • 我被告知使用FragmentStateAdapter的createFranie覆盖来按索引获取当前片段。 这就是我的适配器的外观。 在我们在此活动/片段上强制使用onSaveInstance/OnRestoreInstance之前,这一切正常。 ViewPager2重新加载已经存在的片段,我们需要一种访问方法。FragmentStateAdapter类中的以下LOC证明了这一点 在活动/片段重新创

  • 似乎有很多与这个主题相关的问题。当我阅读大部分时,我有一个问题。通常人们试图制作一个片段,然后从那里生成一个对话片段。所以对话片段在片段中。 在我的例子中,我创建了一个按钮,用于打开工具栏上的dialogfragment。然后我从导航栏打开我的主要片段。因此dialogfragment和我的主片段通过相同的主活动被调用,只是在不同的地方。我无法从片段内部调用对话框片段,因为它在选择菜单选项时被调用

  • 我遇到了一个如何在对话框片段中更新片段的问题。 当我单击过滤器菜单按钮时,会显示一个新的对话框片段,其中包括一个无线电组。 我想在单击ok按钮时更新包含位置列表的片段。 它是PlaceActive的代码,其中包含PlaceFraank: 公共类PlaceActive扩展AppCompatActive{ } 以下是PlaceFragment类的代码: 公共类PlaceFragment扩展了片段{ }

  • 我想在收到一些HTTP请求的响应后,更新几个<code>片段 。-- 活动 适配器 碎片样本 日志显示位置0,1处的片段的<code>updateData</code>工作良好。但是对于位置2处的片段,它会导致错误。 我发现的区别是适配器没有为位置2调用,这意味着没有调用的。因此不存在。 我的直觉告诉我,问题一定来自适配器的工作流或fragmentManager事务。但是作为一个Android初学

  • 我有一个DialogFragment,它使用FragmentPagerAdapter来显示选项卡。每个选项卡都有一个使用RecyclerView的不同片段。我可以获取单击后传递给片段的项目,但如何才能将数据从片段获取到DialogFragment,以便将其传递给调用活动? TabDialog扩展DialogFraank: TabAdapter扩展了FragmentPagerAdapter 在Fra

  • 我无法通过使我的片段相互通信,该活动使用作为一个帮助器类,该类实现对选项卡的管理以及将与相关联的连接的所有细节。我已经实现了,就像Android示例项目Support4Demos提供的一样。 主要的问题是,当我既没有Id也没有标记时,如何从获得特定的片段?创建片段并自动生成Id和标记。