android - 使用 fragment 更改为横向时 map 行为会发生变化

标签 android google-maps tabs android-fragments

我能够使用 fragment 和 viewpager 在选项卡布局中加载 Google map 。它适用于纵向和横向。当我第一次加载应用程序时,当我切换到 map 选项卡时, map 加载,相机在我的标记上进行动画处理,该标记具有预定义的纬度和经度,并且我的当前位置已启用。但是,如果我更改选项卡,然后返回到 map 选项卡,位置查找器按钮消失了(左上按钮)相机不会根据我设置的标记设置动画,因为标记消失了, map 显示非洲。

这是我的 map 标签代码。

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);
    }
}

}

需要帮助。谢谢!

最佳答案

map 显示非洲 = map 显示 GPS (0,0)。到处,总是。 :)

您的 setUpMapsIfNeeded 有问题。您在 UI 上拥有的 map 对象不是您在 map 变量中拥有的 map 。您必须始终在 onCreateView

中重新填充所有 UI 变量

Android 会在 fragment 未显示时重新创建您的 View 。 View 上的 map 对象(并且对用户可见)不是您在变量中拥有的对象。

如果你不打电话

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

每次您在 onCreateView 中创建新 View 时,您都不会获得正确的 map ,并且您的所有设置代码都将不起作用。

关于android - 使用 fragment 更改为横向时 map 行为会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19396895/

相关文章:

javascript - 搜索引擎会在 Bootstrap 选项卡中看到我动态创建的内容吗?

c++ - 没有选项卡的 MFC 选项卡控件?

java - 图片上传到服务器 android httppost

android - 更改 Android kitkat 上的状态栏颜色

android - Android 上的 Cardview 大小

java - 在android中将.pdf写入SD卡

google-maps - Google 地点自动完成有时在 Xamarin Android 项目中不起作用

google-maps - 相当于 Google Map "idle"事件的传单 map 事件

cordova - ionic 侧边菜单和原生谷歌地图

jquery - 添加对 Bootstrap 选项卡的滑动支持