【问题标题】:Call google map in Fragment在 Fragment 中调用谷歌地图
【发布时间】:2014-07-07 11:34:01
【问题描述】:

我在我的项目中使用NavigationDrawer,我正在实现google maps,但由于getSupportFragmentManager(),我无法这样做,它总是给出错误:

getSupportFragmentManager() 类型的方法 MapView 未定义。

代码如下:

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.SupportMapFragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class MapView extends Fragment {


    private Context context = getActivity();

    public MapView(){}

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

        View rootView = inflater.inflate(R.layout.map_view, container, false);

        GoogleMap gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        Log.e("Maps", "Result int value::" + result);
        switch (result) {
        case ConnectionResult.SUCCESS:
            Log.e("Maps", "RESULT:: SUCCESS");          
            break;

        case ConnectionResult.DEVELOPER_ERROR:
            Log.e("Maps", "RESULT:: DE");           
            break;

        case ConnectionResult.INTERNAL_ERROR:
            Log.e("Maps", "RESULT:: IE");           
            break;

        case ConnectionResult.INVALID_ACCOUNT:
            Log.e("Maps", "RESULT:: IA");           
            break;

        case ConnectionResult.NETWORK_ERROR:
            Log.e("Maps", "RESULT:: NE");           
            break;

        case ConnectionResult.RESOLUTION_REQUIRED:
            Log.e("Maps", "RESULT:: RR");           
            break;

        case ConnectionResult.SERVICE_DISABLED:
            Log.e("Maps", "RESULT:: SD");           
            break;

        case ConnectionResult.SERVICE_INVALID:
            Log.e("Maps", "RESULT:: SI");           
            break;

        case ConnectionResult.SERVICE_MISSING:
            Log.e("Maps", "RESULT:: SM");           
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            Log.e("Maps", "RESULT:: SVUR");         
            break;
        case ConnectionResult.SIGN_IN_REQUIRED:
            Log.e("Maps", "RESULT:: SIR");          
            break;      

        default:
            break;
        }
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.setMyLocationEnabled(true);
        Log.e("Maps", "------EOC-------");

        return rootView;
    }

}

【问题讨论】:

标签: android google-maps fragment


【解决方案1】:
import fragment from android.support.v4.app.Fragment;
and just change 

    GoogleMap gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    to 

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

【讨论】:

  • 抱歉,按照步骤操作后,我得到了这个:- 无法从 Fragment 转换为 SupportMapFragment...
  • 但是我也在片段中使用地图,但我没有收到此类错误。请确保您已导入像这样的片段 import android.support.v4.app.Fragment;
猜你喜欢
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多