android mapfragment 以编程方式
android mapfragment programmatically
我想插入一个 Android 映射 v2 到片段中,但我的限制是我不能使用 .xml 文件。当我尝试实例化 MapFragment 时出现问题。这是代码:
public class LocationFragment extends Fragment{
private static View view;
private static GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
view = (RelativeLayout) inflater.inflate(R.layout.location_fragment, container, false);
setUpMapIfNeeded(); // For setting up the MapFragment
MapFragment mf = new 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 (mMap == null) {
FragmentManager manager = MapActivity.fragmentManager;
MapFragment smf = (MapFragment) manager.findFragmentById(R.id.location_map);
mMap = smf.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
有人可以帮我吗?
你真的应该使用 getMapAsync()
。但是您可以这样做,使用片段事务以编程方式添加地图片段。
MapFragment smf = MapFragment.newInstance();
manager.beginTransaction()
.add(FRAGMENT_CONTAINER_ID, smf)
.commit();
如果您使用片段,我建议如下:
public class MapFragment extends Fragment {
private static GoogleMap mMap;
private static UiSettings mUiSettings;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.conduccion_map, container, false);
if (mMap == null)
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment)).getMap();
mMap.setMyLocationEnabled(true);
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(false);
mUiSettings.setCompassEnabled(true);
mUiSettings.setMyLocationButtonEnabled(true);
return rootView;
}
}
从 PagerAdapter 调用这个片段的代码可能是这样的:
class AppSectionsPagerAdapter extends FragmentStatePagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// Map Fragment Activity
return new MapFragment();
default:
return null;
}
}
}
希望对您有所帮助!
我想插入一个 Android 映射 v2 到片段中,但我的限制是我不能使用 .xml 文件。当我尝试实例化 MapFragment 时出现问题。这是代码:
public class LocationFragment extends Fragment{
private static View view;
private static GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
view = (RelativeLayout) inflater.inflate(R.layout.location_fragment, container, false);
setUpMapIfNeeded(); // For setting up the MapFragment
MapFragment mf = new 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 (mMap == null) {
FragmentManager manager = MapActivity.fragmentManager;
MapFragment smf = (MapFragment) manager.findFragmentById(R.id.location_map);
mMap = smf.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
有人可以帮我吗?
你真的应该使用 getMapAsync()
。但是您可以这样做,使用片段事务以编程方式添加地图片段。
MapFragment smf = MapFragment.newInstance();
manager.beginTransaction()
.add(FRAGMENT_CONTAINER_ID, smf)
.commit();
如果您使用片段,我建议如下:
public class MapFragment extends Fragment {
private static GoogleMap mMap;
private static UiSettings mUiSettings;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.conduccion_map, container, false);
if (mMap == null)
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment)).getMap();
mMap.setMyLocationEnabled(true);
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(false);
mUiSettings.setCompassEnabled(true);
mUiSettings.setMyLocationButtonEnabled(true);
return rootView;
}
}
从 PagerAdapter 调用这个片段的代码可能是这样的:
class AppSectionsPagerAdapter extends FragmentStatePagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// Map Fragment Activity
return new MapFragment();
default:
return null;
}
}
}
希望对您有所帮助!