【问题标题】:How to use google map in android fragment?如何在android片段中使用谷歌地图?
【发布时间】:2016-02-01 12:22:10
【问题描述】:

我正在开发一个使用片段的应用程序。我想使用谷歌地图的片段之一,但我遇到了错误。

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

        }
    }

当我尝试投射地图片段时,我收到了评论错误。我应该怎么做才能解决这个问题?谢谢大家。 编辑:布局;

<fragment
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

【问题讨论】:

  • 它是如何工作的?您在 return 语句之后有代码。
  • 在谷歌上搜索。太老的问题了。
  • 它对我不起作用。我解决了这个问题(现在返回语句在代码之后但仍然不起作用)
  • 发布整个堆栈跟踪。支持库或 vanilla 片段?

标签: android google-maps android-fragments


【解决方案1】:

您可以通过这种方法。
这是我的 Fragment 类,它有 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();     
}

}


下面是我的片段 xml

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


清单应具有以下以及权限

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your api key" />

【讨论】:

    【解决方案2】:

    在 return 语句之后,您正在转换为 mapFragment。由于该程序认为该方法在该点上是完整的,因此它永远不会到达它。试试这个:

    Inflater v = inflater.inflate(R.layout.fragment_map, container, false);
    GoogleMap map = ((SupportMapFragment)v.getFragmentManager().findFragmentById(R.id.map)).getMap();
    return v;
    

    我也没有看到你使用意图来启动谷歌地图活动。

    【讨论】:

      【解决方案3】:

      Google for Maps 最新更新,fragment 仅支持 MapView。

      <com.google.android.gms.maps.MapView
          android:id="@+id/mapView"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
      

      【讨论】:

        猜你喜欢
        • 2020-12-21
        • 1970-01-01
        • 2017-08-16
        • 1970-01-01
        • 1970-01-01
        • 2013-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多