[英]Google Maps V2 showing blank screen on 4.4 with google logo showing in bottom left
我在這里絕對要瘋了,我已經瀏覽了很多帖子,而且似乎沒有任何效果。 經過檢查,我的設備肯定是連接到播放服務的,當我查看自己的api憑據時,關鍵是顯示我的應用程序。 出於某種未知的原因,我似乎無法顯示地圖,只能顯示空白屏幕,而且在其中花了3個小時沒有運氣。 對於為什么我無法顯示地圖的任何幫助將不勝感激! 我什至沒有運氣就嘗試過重新生成密鑰。
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.*****.****" >
<permission
android:name="com.*****.****.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.*****.****.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 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" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="********-***********"/>
</application>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java
package com.zoome.natalie.zoo;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity {
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
GoogleMap googleMap;
// tag used on log messages.
static final String TAG = "HelloGooglePlayServices";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createMap();
}
@Override
protected void onResume() {
//auto-generated method stub
super.onResume();
// You need to do the Play Services APK check here too.
checkPlayServices();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "Device not supported.");
finish();
}
return false;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void createMap() {
try {
if (null == googleMap) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
/**
* If the map is still null after attempted initialisation,
* show an error to the user
*/
if (null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception) {
Log.e("mapApp", exception.toString());
}
}
}
並且在有用的情況下,build.gradle模塊
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.zoome.natalie.zoo"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
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-maps:6.5.87'
}
調試鍵是我最不確定的,但是我認為這是對的,非常感謝您嘗試嘗試或在哪里出錯的任何幫助。 謝謝。
假設您已經安裝了SDK東西(我相信您會因為使用Google徽標進入灰屏的事實而這么做),那么您要做的第一件事就是創建一個調試和發布密鑰庫。 完整的文檔在這里:
https://developers.google.com/maps/documentation/android/start
對於Mac / Linux用戶而言,這是快速而又骯臟的。 在終端中輸入以下內容:
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
然后,您應該看到SHA1哈希將用於調試版本。 復制並粘貼到某個地方。
我不確定您是否正在嘗試編譯該應用程序以發布版本,但是讓我們現在進行一些動作,以免您日后頭痛。 在Android Studio中,點擊構建->生成簽名APK 。
這將引導您完成一個逐步的過程,在此過程中,選擇要編譯的模塊,然后選擇要用於該模塊的密鑰。 此時,您要單擊“ 新建”
這樣一來,您就可以決定要在何處保存釋放密鑰,設置密碼並可以設置其他一些選項。
現在,通過使用與上面相同的keytool命令,但使用剛剛創建的文件的路徑以及用作密碼的密碼參數,來獲取釋放密鑰的SHA1哈希 。
現在去
https://console.developers.google.com/project
選擇您的項目,然后轉到左側的“ 憑據”鏈接。 如果您還沒有,請設置自己為Android的公共API訪問密鑰。 如果您有,請單擊“ 允許編輯Android應用程序”按鈕。 在此處粘貼您之前記下的兩個SHA1密鑰,然后在每行之后添加“; your.package.name”。 現在,將API KEY復制粘貼到某個地方。
返回Android Studio,並在您的調試和發布文件夾中找到google_maps_api.xml文件。 路徑應為:
包/app/src/debug/res/values/google_maps_api.xml包/app/src/release/res/values/google_maps_api.xml
打開其中的每一個並粘貼如下圖所示:
<string name="google_maps_key" templateMergeStrategy="preserve">API KEY</string>
最后,進入您的AndroidManifest並輸入以下內容:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
喝啤酒慶祝
如果您在地圖的查看區域中看到空白屏幕(或帶有網格),則可能性為,您正在使用版本1的API密鑰並將其放入清單中。 請查看您的開發者控制台並查找Google Maps V2密鑰!
如果您看到一個空白的屏幕,沒有任何網格,並且在左下角帶有google徽標,則很可能是您創建的API密鑰不合適,您不應該創建一個簡單的API密鑰,而是注冊應用程序的API密鑰適用於Google Developers Console中的Google Maps Android API。正確的鏈接會將您帶至“啟用API頁面”,此處為鏈接https://console.developers.google.com/flows/enableapiapiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&reusekey=true
您的清單和布局對我來說都很好,MainActivity並沒有什么異常...。
請正確創建api密鑰; 以下鏈接提供了所需的詳細信息:
https://developers.google.com/maps/documentation/android-api/signup
在運行該應用程序之前,請確保已在手機上啟用互聯網。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.