在使用片段时,地图行为在更改为横向方向时发生变化

4
我能够使用碎片和视图翻页器在选项卡布局中加载谷歌地图。它适用于纵向和横向方向。当我第一次加载应用程序时,切换到地图选项卡,地图加载,相机在我的标记上进行动画处理,该标记具有预定义的纬度和经度,并启用了我的当前位置。然而,如果我更改选项卡,然后返回地图选项卡,则位置查找按钮消失(左上角按钮),相机不会动画显示我设置的标记,因为标记已经消失,地图显示非洲。

这是我地图选项卡的代码。

TabTwo.java

public class TabTwo extends Fragment {

private static View view;
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */

private static GoogleMap map;
private static Double latitude, longitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }
    view = inflater.inflate(R.layout.activity_tab_two, container, false);
    // Passing harcoded values for latitude & longitude. Please change as per your need. This is just used to drop a Marker on the Map
            latitude = 14.6353475;
            longitude = 121.0327501;

            setUpMapIfNeeded(); // For setting up the MapFragment

    return view;
}

/***** Sets up the map if it is possible to do so *****/
public static void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (map == null) {
        // Try to obtain the map from the SupportMapFragment.
        map = ((SupportMapFragment) MainActivity.fragmentManager
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (map != null)
            setUpMap();
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the
 * camera.
 * <p>
 * This should only be called once and when we are sure that {@link #mMap}
 * is not null.
 */
private static void setUpMap() {
    // For showing a move to my loction button
    map.setMyLocationEnabled(true);
    // For dropping a marker at a point on the Map
    map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Estuar Building").snippet("Work Place"));
    // For zooming automatically to the Dropped PIN Location
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
            longitude), 12.0f));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    if (map != null)
        setUpMap();

    if (map == null) {
        // Try to obtain the map from the SupportMapFragment.
        map = ((SupportMapFragment) MainActivity.fragmentManager
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (map != null)
            setUpMap();
    }
}

/**** The mapfragment's id must be removed from the FragmentManager
 **** or else if the same it is passed on the next time then 
 **** app will crash ****/
@Override
public void onDestroyView() {
    super.onDestroyView();
    try {
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void onDetach() {
    super.onDetach();
    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

}

需要帮助。谢谢!

我还发现当我锁定设备时,位置查找按钮会消失。 - Jeongbebs
你如何获取当前位置?我没有看到你的代码中有适当的代码。这就是为什么你看到海洋的原因。另外,当你第二次点击选项卡时,你确定你的方法被调用了吗?你设置了断点吗? - fasteque
我使用这个函数来检查Google地图是否知道我的位置:“map.setMyLocationEnabled(true);” - Jeongbebs
在每个片段方法中设置断点,这样你就可以看到第二次按地图选项卡时执行的方法。我觉得第二次进入该选项卡时缺少某些东西,可能是因为片段已经被分配了。 - fasteque
好的,我会尽快回复您。 - Jeongbebs
显示剩余3条评论
1个回答

1

地图显示非洲 = 地图显示GPS(0,0)。无处不在,时刻存在。:)

你的setUpMapsIfNeeded有问题。你在UI上有的地图对象并不是你在地图变量中拥有的地图。你必须onCreateView中重新填充所有UI变量。

当片段没有显示时,Android会重新创建你的视图。在视图上(对用户可见)的地图对象不是你在变量中拥有的地图对象。

如果你不调用

map = ((SupportMapFragment)MainActivity.fragmentManager.findFragmentById(R.id.map)).getMap();

每次在 onCreateView 中创建新视图时,您将无法获得正确的地图,所有设置代码都将无法正常工作。

那么在 onDestroy 中,我需要再次调用 setUpMapIfNeeded 吗? - Jeongbebs
我尝试调用setUpMapIfNeeded,但不起作用。有什么建议吗? - Jeongbebs
那我就把 setUpMapIfNeededonCreate 中移除,放到哪里呢? - Jeongbebs
Android会重新创建你的视图。在视图上的地图对象(对于用户可见)不是你在变量中拥有的对象。如果你在onCreateView里创建一个新的视图时没有每次调用map = ((SupportMapFragment) MainActivity.fragmentManager.findFragmentById(R.id.map)).getMap();,你就得不到正确的地图,所有设置代码都不会起作用。 - meredrica
所以每次都要调用它吗?抱歉,我是新手对于Fragment和所有这些重新创建的东西还不太熟悉。 - Jeongbebs
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接