如何使用谷歌地图Android版中的片段? [英] How to use google map in android fragment?
本文介绍了如何使用谷歌地图Android版中的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发使用碎片的应用程序。我的一个片段,我想使用谷歌地图,但我收到错误信息。
公共类地图扩展片段{
公共地图(){
//必需的空公共构造
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
//充气的布局该片段
使用GoogleMap地图=((SupportMapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap();
//错误:(38 84)错误:不可兑换的类型
//要求:SupportMapFragment
//发现:片段
返回inflater.inflate(R.layout.fragment_map,集装箱,FALSE); }
}
当我尝试投放地图片段我收到的评论的错误。我应该怎么做来解决这个?谢谢大家。
编辑:布局;
<片段
机器人:layout_width =match_parent
机器人:layout_height =200dp
机器人:ID =@ + ID /图
机器人:名字=com.google.android.gms.maps.SupportMapFragment/>
解决方案
您可以通过这种方法。结果这是有我的片段类图形页面
公共类DashBoardFragment扩展片段{
// --------------类变量
私人MapView类mMapView;
私人GoogleMap的mGoogleMap;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.fragment_dash_board,集装箱,FALSE);
mMapView =(图形页面)view.findViewById(R.id.map_dashBoard);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); 尝试{
MapsInitializer.initialize(getActivity()getApplicationContext());
}赶上(例外五){
e.printStackTrace();
}
mGoogleMap = mMapView.getMap();
}}
结果
下面是我对XML片段结果
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=com.dev.abc> < com.google.android.gms.maps.MapView
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / map_dashBoard
机器人:layout_width =match_parent
机器人:layout_height =match_parent
/>
< / LinearLayout中>
结果
清单应连同权限如下结果
<元数据
机器人:名字=com.google.android.maps.v2.API_KEY
机器人:价值=你的API密钥/>
I am developing an app that use fragments. One of my fragments i want to use google map but i am getting errors.
public class Map extends Fragment {
public Map() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
GoogleMap map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//Error:(38, 84) error: inconvertible types
//required: SupportMapFragment
//found: Fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
}
When i trying to cast map fragment i am getting the error in comment. What should i do to solve this? Thanks for everyone. Edit: the layout ;
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
解决方案
You can go through this approach.
This is my Fragment class which has MapView
public class DashBoardFragment extends Fragment{
//-------------- class variables
private MapView mMapView;
private GoogleMap mGoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_dash_board, container, false);
mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mGoogleMap = mMapView.getMap();
}
}
Below is my xml for the fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dev.abc">
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_dashBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Manifest should have the following along with permissions
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your api key" />
这篇关于如何使用谷歌地图Android版中的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文