如何在片段内使用SupportMapFragment? [英] How to use SupportMapFragment inside a Fragment?
问题描述
我知道在使用嵌套片段时存在一个问题。但我的应用程序被设计为运行碎片,如果我将使用活动的地图,我的铸造函数将有错误。
我想问你的帮助关于如何实现这一点。我一直在互联网上搜索,但我找不到最好的解决方案。
我试过这段代码:
private void setUpMapIfNeeded ){
//做一个空检查来确认我们还没有实例化地图。
if(mMap == null){
//尝试从SupportMapFragment获取映射。
mMap =((SupportMapFragment)myFragmentActivity.getSupportFragmentManager()。findFragmentById(R.id.map_con))
.getMap();
//检查我们是否成功获取地图。
if(mMap!= null){
mMap.setMyLocationEnabled(true);
$ / code $ / pre
这会给我重复的错误因为R.id.map_con是我片段中的一个片段。
所以我寻找解决方法,这次,R.id.map_con是一个框架布局,在运行时我创建了SupportMapFragment。
SupportMapFragment mSupportMapFragment = new SupportMapFragment();
myFragmentActivity.getSupportFragmentManager()。beginTransaction()
.replace(R.id.map_con,mSupportMapFragment).commit();
虽然这并不会在每次关闭并打开片段时都会有重复。但我的错误是,mSupportMapFragment.getMap始终为空,我不明白为什么它的空值。
mMap = mSupportMapFragment.newInstance()的GetMap();
if(mMap!= null){
Log.e(ReportFragment,mMap is not empty);
} else {
Log.e(ReportFragment,mMap is empty);
}
我真的很感谢你们的任何意见,或者你有其他工作围绕但仍然在这个过程中,即片段内碎片
谢谢
chkm8
$ b $ >我已经使用过这个:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
查看v = inflater.inflate(R.layout.fragment_location,container,false);
mMapFragment = new SupportMapFragment(){
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mMap = mMapFragment.getMap();
if(mMap!= null){
setupMap();
}
}
};
getChildFragmentManager()。beginTransaction()。add(R.id.framelayout_location_container,mMapFragment).commit();
return v;
}
感谢此旧文章
I know that there has been an issue in using a nested fragment. But my application was designed to run on fragments and if i will be using activity for the map, my casting functions will have error.
I would like to ask help from you on how to achieve this. I've been searching in the internet but i couldn't find best solution.
I've tried this code:
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) myFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map_con))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
}
this would give me duplicate error because of R.id.map_con is a fragment inside my fragment.
So I look for a work around, this time, R.id.map_con is a frame layout and in the run time I created the SupportMapFragment to it.
SupportMapFragment mSupportMapFragment = new SupportMapFragment();
myFragmentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.map_con, mSupportMapFragment).commit();
though this does not give me a duplicate every time a close and open the fragment. but my error is that mSupportMapFragment.getMap is always null., I don't get it why its null?.
mMap = mSupportMapFragment.newInstance().getMap();
if (mMap != null){
Log.e("ReportFragment","mMap is not empty");
}else{
Log.e("ReportFragment","mMap is empty");
}
I would really appreciate any inputs from you guys, or do you have another work around but still in this process, i.e Fragment inside fragment
Thanks
chkm8
解决方案 I just met my luck, while making this post,I found what Im looking for.
I have used this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_location, container, false);
mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mMap = mMapFragment.getMap();
if (mMap != null) {
setupMap();
}
}
};
getChildFragmentManager().beginTransaction().add(R.id.framelayout_location_container, mMapFragment).commit();
return v;
}
Credit to this Old post
这篇关于如何在片段内使用SupportMapFragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!