【问题标题】:Android: Use of MapFragment within one of a number of FragmentsAndroid:在多个片段之一中使用 MapFragment
【发布时间】:2012-12-10 22:39:16
【问题描述】:

我找了又找,没有人一一回答。

我有一个项目需要在 Android 项目的多个片段之一中使用新的 MapFragment。

到目前为止,我拥有它就像一个 FB 移动应用程序。它在左侧显示一个菜单,用户可以选择一个项目并显示一个片段。到目前为止,我搜索和搜索。没有完整和可行的答案。

我喜欢查看示例代码和/或示例项目,它们可以在 Android 应用中的许多片段的片段中显示新的 Google MapFragment。

提前致谢。

【问题讨论】:

  • 您需要相当多的代码。使用getChildFragmentManager() 的哪一部分不适合您?
  • 我刚刚对 getChildFragmentManager() 做了一些研究,你必须使用 IMPORT android.support.v4.app.Fragment 和其他类似的,而不是使用 IMPORT android.app.Fragment 和其他。 IMPORT 语句及其支持的功能有很多不一致之处。我在这里使用这个项目开发我的应用程序:github.com/lexs/android-delicious 尝试将 MapFragment 合并到该应用程序的众多片段之一中。如果您可以提供建设性的帮助,请在编辑项目时提供一些帮助。肿瘤坏死因子。 :)

标签: java android google-maps android-nested-fragment


【解决方案1】:

我刚刚对 getChildFragmentManager() 做了一些研究,你必须使用 IMPORT android.support.v4.app.Fragment 和其他类似的,而不是使用 IMPORT android.app.Fragment 和其他

不一定,尽管 API 级别 17 中的 getChildFragmentManager() was only added to the native Fragment implementation

欢迎您使用 SupportMapFragment 与 Android 支持包的片段反向移植一起使用。这应该允许您在 Maps V2 中使用 getChildFragmentManager()

【讨论】:

  • 所有这些不同的东西都是荒谬的。我更改了一个片段文件以扩展 SupportMapFragment 并且整个事情都很糟糕。我不知道你指的是什么与什么一起使用。如果有人足够好心地知道如何更新我上面列出的示例项目以正确使用 MapFragment 并在其中一个片段中工作,请这样做以帮助每个对新记录不充分的 MapFragments 感到好奇的人。 tnx :)
【解决方案2】:

以编程方式添加 SupportMapFragment 有效。在您的片段实现中:

...
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
...

@Override
public void onStart() {
    super.onStart();

    // add fragment 
    mMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.game_map_container, mMapFragment);
    fragmentTransaction.commit();

    // add a marker
    map = mMapFragment.getMap();

    if (map != null) {
        // The Map is verified. It is now safe to manipulate the map.
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        map.addMarker(new MarkerOptions().position(target));
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(target, 10));
    } else {
        Log.e(TAG, "map is not available");
    }
}

其实这个想法是从Initialize MapFragment programmatically with Maps API v2偷来的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多