我尝试在我的应用上显示地图,但我无法显示,对我来说不可能......
的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>
使用权限修改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
创建我的主要课程MainActivity.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
有什么建议吗?
更新 - 解决方案
解决了,我把显示浏览器应用程序的密钥(带有引用者)&#34;而不是Android应用程序的键#(带证书)&#34;来自Google API控制台
答案 0 :(得分:4)
解决方案:
使用Google API控制台中的“浏览器应用程序密钥(带有引用程序)”而不是“Android应用程序密钥(带证书)”
答案 1 :(得分:2)
我检查了你的代码。听起来不错。 但试试这个。
<?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'
答案 2 :(得分:0)
我遇到了同样的问题(授权失败):
简单的解决方案:
起初我只能通过摆脱所有 API 密钥限制来解决它。如果您可以使用不受限制的密钥,这可能会正常工作。
我的解决方案:
我还是想使用一些限制。我开始玩限制设置,直到找到错误的根源:
如果您选择应用程序限制 -> Android 应用程序,您将看到一个名为限制使用您的 Android 应用程序的设置。您需要在此处提供应用程序的包名称和SHA-1 证书指纹。
我的问题与包名称有关:出现问题是因为我创建了地图片段,我想在其中显示子包中的地图.要使 API 密钥起作用,您必须提供如下所示的包:
正确的包名示例:com.example.application
因为我在子包中创建了片段,所以它会自动将子包添加到 API 密钥限制中:
不正确的包名称示例:com.example.application.subpackage