0

我试图google map在我的应用程序中显示,但它显示小方块屏幕而不是google maps

这是我的Mainactivity

public class MainActivity extends FragmentActivity {

    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (servicesOK()) {
            //Toast.makeText(this, "Google play services successfully connected.", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.activity_map);
        }else{
            setContentView(R.layout.activity_main);         
        }        
    }    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean servicesOK(){
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (isAvailable == ConnectionResult.SUCCESS) {
            return true;
        }else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }else{
            Toast.makeText(this, "Can't connect to google play services.", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

我的activiy_map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我的mainfest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gmapapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <permission 
        android:name="com.example.gmapapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="com.example.gmapapp.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission
        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-feature 
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gmapapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data android:name="com.google.android.gms.version" 
           android:value="@integer/google_play_services_version" />

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

    </application>

</manifest>

我的logcat

11-30 15:23:13.430: E/dalvikvm(6948): GC_CONCURRENT freed 1785K, 24% free 7739K/10147K, paused 3ms+12ms
11-30 15:27:45.260: E/dalvikvm(6948): GC_CONCURRENT freed 1673K, 24% free 7781K/10147K, paused 3ms+4ms
11-30 15:30:40.500: E/dalvikvm(7048): GC_CONCURRENT freed 1007K, 18% free 7301K/8803K, paused 2ms+3ms
11-30 15:30:40.840: E/dalvikvm(7048): GC_FOR_ALLOC freed 856K, 23% free 7424K/9571K, paused 2ms+5ms
11-30 15:30:41.040: E/dalvikvm(7048): GC_FOR_ALLOC freed 842K, 23% free 7460K/9571K, paused 2ms+4ms
4

2 回答 2

0

我认为你的问题只是你没有在你的谷歌控制台中打开谷歌地图 api 去那里:https://cloud.google.com/console/project 点击你的项目,然后点击 APIs 和 Auth,然后选择谷歌地图安卓API v2

这应该有帮助:)

于 2014-02-27T10:15:13.730 回答
0

您必须获取此应用程序的 api 密钥

https://developers.google.com/maps/documentation/android/start#obtain_a_google_maps_api_key

在清单中

   <meta-data 
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="08DEBUGKEYABCDEFBLAHBLAHBLAH"/>
于 2014-02-26T06:31:04.747 回答