Android 如何在 Fragment 中使用 SupportMapFragment?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25051246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use SupportMapFragment inside a Fragment?
提问by chkm8
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.
这会给我重复的错误,因为 R.id.map_con 是我的片段中的一个片段。
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.
所以我寻找解决方法,这一次,R.id.map_con 是一个框架布局,在运行时我为它创建了 SupportMapFragment。
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?.
虽然这不会在每次关闭和打开片段时给我一个副本。但我的错误是 mSupportMapFragment.getMap 始终为空。我不明白为什么它为空?。
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
chkm8
采纳答案by 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
归功于这个旧帖子
回答by lpfx
getMap()
is deprecated
getMap()
已弃用
The code should be something like this in a Fragment
:
代码应该是这样的Fragment
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_location, container, false);
mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
return v;
}
回答by Jawad Zeb
I have used it in the given manner and it's working fine.
我以给定的方式使用它,它工作正常。
It's also working with getChildFragmentManager()
它也与 getChildFragmentManager()
MapyFragment
映射片段
public class MapyFragment extends Fragment implements OnMapReadyCallback {
private Context mContext;
private SupportMapFragment supportMapFragment;
private GoogleMap map;
private MarkerOptions currentPositionMarker = null;
private Marker currentLocationMarker;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getActivity();
return inflater.inflate(R.layout.fragment_mapy, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = getActivity();
FragmentManager fm = getActivity().getSupportFragmentManager();/// getChildFragmentManager();
supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, supportMapFragment).commit();
}
supportMapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
map.animateCamera(CameraUpdateFactory.zoomTo(15));
/*map.setOnMapLongClickListener(MapyFragment.this);
map.setOnMapClickListener(MapFragment.this);*/
}
public void updateCurrentLocationMarker(Location currentLatLng){
if(map != null){
LatLng latLng = new LatLng(currentLatLng.getLatitude(),currentLatLng.getLongitude());
if(currentPositionMarker == null){
currentPositionMarker = new MarkerOptions();
currentPositionMarker.position(latLng)
.title("My Location").
icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue));
currentLocationMarker = map.addMarker(currentPositionMarker);
}
if(currentLocationMarker != null)
currentLocationMarker.setPosition(latLng);
///currentPositionMarker.position(latLng);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
and fragment_mapy.xml
和fragment_mapy.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map_container"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:uiZoomControls="true" />
回答by Juanes30
In kotlinit could be done as follows:
在kotlin 中,它可以按如下方式完成:
class NameFragment: Fragment(){
// other code ...
private lateinit var googleMapsFragment: SupportMapFragment
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
googleMapsFragment = childFragmentManager.findFragmentById(R.id.map_fragment) as SupportMapFragment
// other code ...
}
}