Google maps V2 - 授权失败,我无法显示地图
Google maps V2 - Authorization failure, I can't display map
我正在尝试在我的应用程序上显示地图,但我无法显示,这对我来说是不可能的...
我的步骤:
- 从 SDKManager 安装 "Google play services"。
- 从项目添加库
build.gradle
应用插件:'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "prueba.mapas.alvaro.myapplication"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
也将库添加到 AndroidManifest.xml
。
清单(部分)
<application
......
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
......
</application>
- 获取 SHA1
- 转到“https://code.google.com/apis/console/b/0/?noredirect”获取 API 密钥。
- Android Studio 上的包名称
修改 AndroidManifest.xml
权限。
<permission
android:name="prueba.mapas.alvaro.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="prueba.mapas.alvaro.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"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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=".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" />
//api key
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyD1UolEb1tDMe6qB5BUF5orFzc3CDtvJYg"/>
</application>
创建我的布局map_activity.xml
创建我的委托人classMainActivity.class
public class MainActivity 扩展了 ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
}
}
连接我的设备,它报告错误并且在我的设备上总是出现白屏。
注意:我总是从我的设备上的应用程序中清除数据,卸载并重新安装,但不起作用
更新
我已在控制台上启用 Google Maps V2
有什么建议吗?
更新 - 解决方案
已解决,我在 Google API 控制台
中将 "Key for browser apps (with referers)" 而不是 "Key for Android apps (with certificates)" 放在清单上
解决方案:
在清单上放置 "Key for browser apps (with referers)" 而不是 Google API 控制台
中的 "Key for Android apps (with certificates)"
我检查了你的代码。听起来不错。
但是试试这个。
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iqamah_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
使用FragmentActivity
public class YourActivity extends FragmentActivity {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.iqamah_map)).getMap();
}
}
更新您的 build.gradle
compile 'com.google.android.gms:play-services:5.0.89'
我有同样的问题(授权失败):
简单的解决方法:
起初我只是设法通过摆脱所有 API 键限制来解决它。如果您可以使用不受限制的密钥,这可能会正常工作。
我的解决方案:
我还想使用一些限制。我开始尝试限制设置,直到找到错误的来源:
API key restrictions
如果你 select 应用程序限制 -> Android 应用程序 你会得到一个名为 限制使用您的 Android 应用程序。在这里您需要提供应用程序的 包名称 和 SHA-1 证书指纹.
我的问题与包名有关:出现问题是因为我在我想要的地方创建了地图片段在子包中显示地图。要使 API 键 起作用,您必须提供如下所示的包:
正确的包名示例:com.example.application
因为我在子包中创建了片段,所以它自动将子包添加到 API 键限制:
不正确的包名示例:com.example.application.subpackage
我正在尝试在我的应用程序上显示地图,但我无法显示,这对我来说是不可能的...
我的步骤:
- 从 SDKManager 安装 "Google play services"。
- 从项目添加库
build.gradle
应用插件:'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "prueba.mapas.alvaro.myapplication"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
也将库添加到 AndroidManifest.xml
。
清单(部分)
<application
......
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
......
</application>
- 获取 SHA1
- 转到“https://code.google.com/apis/console/b/0/?noredirect”获取 API 密钥。
- Android Studio 上的包名称
修改
AndroidManifest.xml
权限。<permission android:name="prueba.mapas.alvaro.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="prueba.mapas.alvaro.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"/> <!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <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=".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" /> //api key <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyD1UolEb1tDMe6qB5BUF5orFzc3CDtvJYg"/> </application>
创建我的布局
map_activity.xml
创建我的委托人class
MainActivity.class
public class MainActivity 扩展了 ActionBarActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_activity); MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map); } }
连接我的设备,它报告错误并且在我的设备上总是出现白屏。
注意:我总是从我的设备上的应用程序中清除数据,卸载并重新安装,但不起作用
更新
我已在控制台上启用 Google Maps V2
有什么建议吗?
更新 - 解决方案
已解决,我在 Google API 控制台
中将 "Key for browser apps (with referers)" 而不是 "Key for Android apps (with certificates)" 放在清单上解决方案:
在清单上放置 "Key for browser apps (with referers)" 而不是 Google API 控制台
中的 "Key for Android apps (with certificates)"我检查了你的代码。听起来不错。 但是试试这个。
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iqamah_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
使用FragmentActivity
public class YourActivity extends FragmentActivity {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.iqamah_map)).getMap();
}
}
更新您的 build.gradle
compile 'com.google.android.gms:play-services:5.0.89'
我有同样的问题(授权失败):
简单的解决方法:
起初我只是设法通过摆脱所有 API 键限制来解决它。如果您可以使用不受限制的密钥,这可能会正常工作。
我的解决方案:
我还想使用一些限制。我开始尝试限制设置,直到找到错误的来源:
API key restrictions
如果你 select 应用程序限制 -> Android 应用程序 你会得到一个名为 限制使用您的 Android 应用程序。在这里您需要提供应用程序的 包名称 和 SHA-1 证书指纹.
我的问题与包名有关:出现问题是因为我在我想要的地方创建了地图片段在子包中显示地图。要使 API 键 起作用,您必须提供如下所示的包:
正确的包名示例:com.example.application
因为我在子包中创建了片段,所以它自动将子包添加到 API 键限制:
不正确的包名示例:com.example.application.subpackage