GetMap()未定义MapFragment类型

时间:2015-02-26 11:33:33

标签: android google-maps

我正在尝试在我的应用程序中使用地图片段,并且在尝试获取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;
    }
}

我的frament代码是

  

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>

我得到的确切错误是

  

对于MapFragment类型

,方法getMap()未定义

我做错了什么?

4 个答案:

答案 0 :(得分:4)

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

编辑* 哦,还要将你的班级名称改为不同的名字 import com.google.android.gms.maps.MapFragment这是你应该在xml中使用的类,并且有getMap()方法:)

答案 1 :(得分:0)

我建议使用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();
    }

}

答案 2 :(得分:0)

问题的原因是您的片段名称MapFragment与gms&#39; MapFragment类相同,那么您的转换就会出现问题,请尝试将您的片段名称更改为其他内容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

答案 3 :(得分:0)