Android Google Maps v2 SupportMapFragment将片段中的片段映射到具有NavigationDrawer的活动中
我正在使用一个应用程序,该应用程序使用带有用户当前位置的地图。我按照《Android平台参考:Android开发者/设计》的标准设计了UI,因此我尝试将Google地图插入到包含导航抽屉和四个片段的活动中的片段中。 问题是,在web上,我只找到了与我使用活动(例如活动XML布局中的片段)的情况相匹配的解决方案,但这不是我想要的! 我只想在片段中插入一个具有当前位置的映射,可能需要使用SupportMapFragment,事实上我的应用程序可以在Gingerbread+上运行。 我试着在网上只插入一张地图,但我不记得链接了,抱歉,但无法自定义它 为了便于理解,我附上一些代码: MainActivity.java,其中包含一个NavDrawer和4个片段Android Google Maps v2 SupportMapFragment将片段中的片段映射到具有NavigationDrawer的活动中,android,google-maps,android-fragments,gps,location,Android,Google Maps,Android Fragments,Gps,Location,我正在使用一个应用程序,该应用程序使用带有用户当前位置的地图。我按照《Android平台参考:Android开发者/设计》的标准设计了UI,因此我尝试将Google地图插入到包含导航抽屉和四个片段的活动中的片段中。 问题是,在web上,我只找到了与我使用活动(例如活动XML布局中的片段)的情况相匹配的解决方案,但这不是我想要的! 我只想在片段中插入一个具有当前位置的映射,可能需要使用SupportMapFragment,事实上我的应用程序可以在Gingerbread+上运行。 我试着在网上只插入
public class MainActivity extends AbstractNavDrawerActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new MapFragment()).commit();
}
}
...
}
java片段,我想在其中插入一个带有位置的工作映射
public class MapFragment extends Fragment {
// Argument representing the section number for this fragment.
private static final String ARG_SECTION_NUMBER = "section_number";
public static MapFragment newInstance(int sectionNumber) {
MapFragment fragment = new MapFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
fragment_map.xml地图布局
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapView" />
我确保GoogleMaps工作正常,使用带有地图的活动的示例教程工作良好。例如,我在这里读过一些解决方案,但它在我的情况下不起作用
有人能帮我吗
真的非常感谢大家 在MapFragment中使用以下代码
GoogleMap map=((MapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
if(map!=null){
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude)) // latitude and longitude info is given here
.title(TITLE)); // title for the marker
}
在布局文件中创建用于显示地图的片段
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
它不起作用。我将java代码插入到onCreateView中,但getActivity不正确,因为我在片段中工作。我用GoogleMap=MapFragment getFragmentManager.findFragmentByIdR.id.map.getMap替换该行;但它的工作原理不同。你到底犯了什么错误。。。?这段代码在一个片段中完美地工作