2017-09-20 142 views
0

我創建了一個新的Android項目與谷歌地圖活動。我的活動代碼:谷歌地圖只顯示在Android工作室灰色瓷磚

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 


/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(-34, 151); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 

}

,這是我的清單:

<?xml version="1.0" encoding="utf-8"?> 

<!-- 
    The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
    Google Maps Android API v2, but you must specify either coarse or fine 
    location permissions for the 'MyLocation' functionality. 
--> 

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="xxxxxx.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<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" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES"/> 

<uses-permission android:name="xxxxx.mapservice.permission.MAPS_RECIEVE"/> 
<uses-permission android:name="xxxxx.provides.gsf.permission.READ_GSERVICES"/> 
<permission android:name="xxxxx.mapservice.permission.MAPS_RECIEVE" 
    android:protectionLevel="signature"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <!-- 
     The API key for Google Maps-based APIs is defined as a string resource. 
     (See the file "res/values/google_maps_api.xml"). 
     Note that the API key is linked to the encryption key used to sign the APK. 
     You need a different API key for each encryption key, including the release key that is used to 
     sign the APK for publishing. 
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    --> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

,我有一個谷歌API密鑰和限制它與sh1鍵。我遵循谷歌文檔,但不幸的是我的地圖沒有加載。

雖然我正在搜索一些意見,建議我應該刪除debug.keystore,但這不適用於我。我錯過了什麼?

而且我的地圖佈局的xml文件是:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/map" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="ir.iuwa.testmap.MapsActivity" /> 

回答

0

這種添加到您的清單應用程序的標籤之外,並檢查

<uses-feature 
android:glEsVersion="0x00020000" 
android:required="true" /> 
0

我找到了解決辦法! 我的錯誤是在生成正確的sha1代碼。以前,我發現使用gradle這個菜單在Android Studio中的右側使用以下過程這一關鍵:首先點擊gradle這個,然後,在應用程序,任務,機器人,我點擊signingReport:

enter image description here

那麼SHA1在出現的Gradle控制檯:

enter image description here

我falut是,這個代碼是用於調試模式! 我按照以下過程爲我的發行版keystore獲取sha1發行版,並在Google開發者控制檯中輸入此新版sha1後,我的地圖完全顯示。 要獲得sha1版本,首先你應該複製你的發行密鑰庫地址: 去建立>> generateSigendApk並複製你的密鑰庫路徑。

enter image description here

,那麼你應該複製機器人工作室使用你的JDK的路徑。轉到文件>> ProjectStructure,你可以看到你的jdk路徑。複製此路徑並在cmd中轉到此文件夾。

enter image description here

然後CD尤爾JDK,然後bin文件夾,並複製以下行: 的keytool -list -v -keystore「複製的鑰匙存放路徑」 -alias「密鑰別名」 現在進入並享受您的發佈sha1代碼:)