简体   繁体   English

Google地图当前位置

[英]Google maps Current Location

I have a really complicated app that has various different functions. 我有一个非常复杂的应用程序,它具有各种不同的功能。 One of these functions include a google maps tab. 这些功能之一包括google maps标签。 The app is structured in such a way that each tab is a fragment. 该应用程序的结构使得每个选项卡都是一个片段。 In this particular case I am having trouble declaring my google map object to grab my current location data. 在这种情况下,我很难声明我的Google Map对象来获取我当前的位置数据。

Here is the relevant code 这是相关的代码

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;
        }
    }

In this codes implementation I get a Null Pointer exception when I try to declare mMap. 在此代码实现中,当我尝试声明mMap时,我得到了一个N​​ull Pointer异常。 If I remove the getAvtivity() call the eclipse tells me I can't make a static reference to a non-static method. 如果我删除了getAvtivity()调用,则日食会告诉我我不能对非静态方法进行静态引用。 If I remove the mMap lines my app works with a google map in one tab (the other tabs implement other things which all work). 如果我删除了mMap行,则我的应用程序将在一个选项卡中使用Google地图(其他选项卡将实现所有其他功能)。

Can you include your layout xml? 您可以包含布局xml吗?

If your map fragment is SupportMapFragment ( com.google.android.gms.maps.SupportMapFragment ), use: 如果您的地图片段是SupportMapFragmentcom.google.android.gms.maps.SupportMapFragment ),请使用:

mMap = ((SupportMapFragment) getFragmentManager ().findFragmentById (R.id.map)).getMap ();

or 要么

if your map fragment is MapFragment ( com.google.android.gms.maps.MapFragment ), use: 如果您的地图片段是MapFragmentcom.google.android.gms.maps.MapFragment ),请使用:

mMap = ((MapFragment) getFragmentManager ().findFragmentById (R.id.map)).getMap ();

EDIT: The actual answer to the OP's question: Make sure you're using the right class name for the Map Fragment when declaring it in your layout XML. 编辑:OP问题的实际答案:在布局XML中声明地图片段时,请确保使用正确的类名称作为地图片段。

Try this 尝试这个

GoogleMap mMap = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

and in your xml 并在您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM