未定义 MapFragment 类型的 GetMap()

GetMap() undefined for the type MapFragment

我正在尝试让地图片段在我的应用程序中运行,但在尝试获取我的 GoogleMap 对象时我仍然遇到错误。

MapFragment.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;

public class MapFragment extends Fragment {

private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_map, container, false);

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

    return view;
    }
}

我的框架代码是

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <Button
    android:id="@+id/btnEugene"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick_Eugene"
    android:text="Eugene" />

    <Button
    android:id="@+id/btnHpc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick_HPC"
    android:text="HPC" />

    <Button
    android:id="@+id/btnTheGrove"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="onClick_FG"
    android:text="Forest Grove" />

    <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_below="@+id/btnTheGrove"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

我得到的确切错误是

The method getMap() is undefined for the type MapFragment

我做错了什么?

我建议使用 SupportMapFragment 而不是 MapFragment

XML代码:

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

Java代码:

package com.mapexample.android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLngBounds;

public class MapViewFragment extends Fragment {

    Activity mActivity;

    private GoogleMap googleMap;
    LatLngBounds.Builder mLatLngBound;
    public static View rootView;

    public MapViewFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }

        try {
            rootView = inflater.inflate(R.layout.fragment_map, container, false);
        } catch (Exception e1) {
            // e1.printStackTrace();
        }
        mActivity = getActivity();

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mActivity);
        if (status == ConnectionResult.SUCCESS) {
            // Success! Do what you want
            try {
                MapsInitializer.initialize(mActivity);
                // Loading map
                initilizeMap();
            } catch (Exception e) {
                // e.printStackTrace();
            }

        } else {
            Toast.makeText(mActivity, "Not Installed Google Play Services..", Toast.LENGTH_SHORT).show();
        }
        return rootView;
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    @SuppressLint("NewApi")
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_map_mapview)).getMap();

            googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {

                }
            });

            googleMap.setMyLocationEnabled(true);
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(mActivity, "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onDestroy() {
        try {
            SupportMapFragment fragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(
                    R.id.fragment_map_mapview);
            if (fragment != null) {
                getActivity().getSupportFragmentManager().beginTransaction().remove(fragment).commit();
                googleMap = null;
            }

        } catch (IllegalStateException e) {
            // e.printStackTrace();
        }
        super.onDestroy();
    }

}

getMap() 已弃用。尝试使用 getMapAsync

编辑* 哦,还要将您的 class 名称更改为其他名称 import com.google.android.gms.maps.MapFragment 这是你应该在 xml 中使用的 class 并且有 getMap() 方法:)

问题的原因是你的片段名称 MapFragment 与 gms 的 MapFragment class 相同,然后你的转换有问题,尝试将你的片段名称更改为否则像 MyMapFragment:

public class MyMapFragment extends Fragment {

      private GoogleMap map;

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {

           View view = inflater.inflate(R.layout.fragment_map, container, false);

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

           return view;
     }
}

那应该就可以了,当然需要import com.google.android.gms.maps.MapFragment;.

如果您不想更改片段名称,则必须将 MapFragment 转换为整个包名称 com.google.android.gms.maps.MapFragment

You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.