[英]Google maps Current Location
我有一个非常复杂的应用程序,它具有各种不同的功能。 这些功能之一包括google maps标签。 该应用程序的结构使得每个选项卡都是一个片段。 在这种情况下,我很难声明我的Google Map对象来获取我当前的位置数据。
public static class GpsClass extends Fragment{
View rootView;
private GoogleMap mMap;
public static final String ARG_SECTION_NUMBER = "section_number";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
if (rootView != null){
((ViewGroup) rootView.getParent()).removeView(rootView);
}
try{
rootView = inflater.inflate(R.layout.gps, container,false);
mMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mymap)).getMap();
mMap.setMyLocationEnabled(true);
} catch(InflateException e){
}
return rootView;
}
}
在此代码实现中,当我尝试声明mMap时,我得到了一个Null Pointer异常。 如果我删除了getAvtivity()调用,则日食会告诉我我不能对非静态方法进行静态引用。 如果我删除了mMap行,则我的应用程序将在一个选项卡中使用Google地图(其他选项卡将实现所有其他功能)。
您可以包含布局xml吗?
如果您的地图片段是SupportMapFragment
( com.google.android.gms.maps.SupportMapFragment
),请使用:
mMap = ((SupportMapFragment) getFragmentManager ().findFragmentById (R.id.map)).getMap ();
要么
如果您的地图片段是MapFragment
( com.google.android.gms.maps.MapFragment
),请使用:
mMap = ((MapFragment) getFragmentManager ().findFragmentById (R.id.map)).getMap ();
编辑:OP问题的实际答案:在布局XML中声明地图片段时,请确保使用正确的类名称作为地图片段。
尝试这个
GoogleMap mMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
并在您的xml中
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.