Android的地图在片段 [英] android map in fragment

查看:184
本文介绍了Android的地图在片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建具有一个活动和多个片段一个Android应用程序。
每个片段会按观测整个屏幕时,它是在图和用替换交易应当切换到另一个片段

I am trying to create a android app that has one activity and multiple fragments. Each fragment will ocupy the entire screen when it is in view and with a replace transaction it should be switched to another fragment.

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是对碎片的容器。
问题是,我想用一个片段是一个GoogleMap的。这是我的code的地图片段:

This is the container for the fragments. The problem is that a fragment that I want to use is a GoogleMap. This is my code for the map fragment:

public class Map extends Fragment{
private final static String TAG = "MAP Fragment";
public GoogleMap googleMap = null;

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment, null);
    return view;
}

private void setUpMapIfNeeded()
{
    if(googleMap == null)
    {
        googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
        if(googleMap != null)
        {
            GoogleMapOptions options = new GoogleMapOptions();
            options.mapType(GoogleMap.MAP_TYPE_NORMAL)
                .camera(new CameraPosition(new LatLng(25f, 47f), 13f, 0f, 0f));
        }
        else
        {
            Log.d(TAG, "googleMap is null !!!!!!!!!!!!!!!");
        }
    }
}

}

和它的布局:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.google.android.gms.maps.MapFragment"/>

而在主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
Map test = new Map();
getSupportFragmentManager().beginTransaction()
            .add(R.id.content_frame, test)
            .commit();
}

我得到一个空指针异常:(我相信它找不到R.id.map)

I get a null pointer exception at: (I belive it cannot find the R.id.map)

googleMap = ((MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map)).getMap();

如果我不使用getActivity()。它说,它不能从片段转换为MapFragment。

If I don't use "getActivity()." it says that it cannot cast from Fragment to MapFragment.

我需要GoogleMap的在地图上创建标记。

I need "googleMap" for creating markers on the map.

当我做出同样的应用程序,但没有片段(我的主要活动diplays地图,我没有其他碎片)everithing工作正常。

When I make the same application but without fragments (my main activity diplays the map and i don't have other fragments) everithing works fine.

我发现这个类似的主题:<一href=\"http://stackoverflow.com/questions/14458398/google-maps-api-v2-custom-mapfragment-simplefragment\">Google利用地图中的片段地图API V2定制MapFragment + SimpleFragment 和错误。
但是,如果我延长FragmentActivity或SupportMapFragment我不能与其他使用事务替换片段。

I have found this similar topics: Google Maps API v2 Custom MapFragment + SimpleFragment and error using maps in fragment . But if I extend FragmentActivity or SupportMapFragment I cannot replace the fragment with another using transactions.

结论:我想,当一个动作发生(点击或东西)我只需要更换的片段(fragments)是这样的:

Conclusion: I would like that when an action occurs (click or something) I only have to replace the fragmens like this:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);

我是什么做错了吗?或者我应该采取不同的解决问题的方法?

What am I doing wrong? Or should I take a different approach to the problem?

(P.S。我realtively新至Android)

(P.S. I am realtively new to android)

推荐答案

的问题是,我是想在显示之前改变地图元素 setUpMapIfNeeded()是叫的onCreate()(视图充气仅在 onCreateView())。

The problem is that I was trying to change the map element before it was displayed setUpMapIfNeeded() is called in onCreate() (the view is inflated only in onCreateView()).

如果我移动呼叫 setUpMapIfNeeded() onResume()一切正常,因为这样的观点进行膨胀,它可以使用地图并修改它。

If I move the call setUpMapIfNeeded() in onResume() everything works because then the view is inflated and it can use the map and modify it.

这是我找到了答案

这篇关于Android的地图在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆