GooglePlayServices [GoogleMapAPI] - 看不到地图

时间:2015-12-12 10:09:09

标签: android google-maps android-fragments google-play-services google-maps-android-api-2

出于某种原因,我无法使用Google Play服务API看到任何地图。

activity_map:

package com.m.get.dl;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class Map extends FragmentActivity implements OnMapReadyCallback
{
    MapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment_id);
        mapFragment.getMapAsync(Map.this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) 
    {
        googleMap.setMyLocationEnabled(true);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // Setting map type
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onResume() 
    {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) 
    {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();
    }
}

我认为问题在于XML文件 - activity_map.xml:

<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    class="com.google.android.gms.maps.MapFragment"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/fragment_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

清单:

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

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="23" />

    <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="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.USE_FINGERPRINT" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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="My key" />

        <activity
            android:name=".Map"
            android:label="@string/title_activity_map" >
        </activity>
    </application>

</manifest>

所以在活动中我得到这个屏幕而不是地图。

enter image description here

我的fragment标签是错误的吗?

1 个答案:

答案 0 :(得分:1)

检查您正在使用的服务器密钥。确保您使用了准确的SHA-1和包名称。其余的代码看起来不错!

另外,如果您收到任何错误,请检查日志:

Authentication failed on the server...

这表示你的密钥不正确。

<强>更新

您将在“身份验证失败后...”

后收到此日志
Ensure that the following Android Key exists: ...

您可以在“确保...”错误消息下方获取正确的SHA-1密钥和软件包名称。 两者都用';'

分隔

现在创建新的API密钥并在Manifest文件标记中添加此新密钥。

**Manifest:**
<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

这将解决您的问题!