Android 在片段类中实现googlemapsapi

Android 在片段类中实现googlemapsapi,android,google-maps,android-layout,android-fragments,Android,Google Maps,Android Layout,Android Fragments,我有一个关于在片段类中实现GoogleMaps的问题。目前,我在XML布局文件中使用MapView,在依赖项中使用“compile'com.google.android.gms:play services:7.0.0',因此我不能在类中使用Places API(我相信)或MapsInitializer.initialize。我当前的布局文件如下所示: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:a

我有一个关于在片段类中实现GoogleMaps的问题。目前,我在XML布局文件中使用MapView,在依赖项中使用
“compile'com.google.android.gms:play services:7.0.0'
,因此我不能在类中使用Places API(我相信)或MapsInitializer.initialize。我当前的布局文件如下所示:

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

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

</RelativeLayout>
那么,如何实现MapView来加载和工作呢?我觉得我尝试了很多不同的东西,但都没有效果。有什么想法吗?另外,我可能犯了一个错误,尝试在映射之前实现Places API。我尝试了代码,它可以工作,您可以更改一点代码吗?如果是这样,你可以试试

我上传了工作代码。我在我的一个项目中做了
映射片段。只需查看名为
UpcomingFrament
的第三个选项卡,它包括
Map fragment


这对您有帮助吗?如果是,请接受,谢谢
    package com.examples.blahblah.blahblah;

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

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;


public class menu_1_fragment extends Fragment  {
    MapView mapView;
    GoogleMap map;

    private Context mContext;
    @Override
    public void onAttach(final Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }


    private GoogleApiClient mGoogleApiClient;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.menu1_layout, container, false);
        return v;
    }

    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        mGoogleApiClient = new GoogleApiClient
                .Builder(mContext)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }


}