2015-09-06 215 views
1

我最近用Android Studio打開了一個新項目,並嘗試使用默認的Map Activity在我的應用程序上生成地圖。但是,當我在我的測試設備上運行應用程序時,底部只有一個帶有谷歌徽標的空白屏幕。我已經制作了一個API密鑰,並將其插入到兩個google_maps_api.xml文件中,並且使用了具有x86系統映像的模擬器以及一個真正的測試設備。谷歌地圖空白屏幕

Android清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.subhendugoswami.spaces" > 

<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 ACCESS_COARSE/FINE_LOCATION 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" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.subhendugoswami.frontend.testLogin" 
     android:label="@string/app_name" > 

    </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="@string/google_maps_key" /> 

    <activity 
     android:name="com.example.subhendugoswami.frontend.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> 
    android:debuggable="true" 
</application> 

MapsActivity.java 包com.example.subhendugoswami.frontend;

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

import com.example.subhendugoswami.spaces.R; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity { 
private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

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

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 

/** 
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
* installed) and the map has not already been instantiated.. This will ensure that we only ever 
* call {@link #setUpMap()} once when {@link #mMap} is not null. 
* <p> 
* If it isn't installed {@link SupportMapFragment} (and 
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
* install/update the Google Play services APK on their device. 
* <p> 
* A user can return to this FragmentActivity after following the prompt and correctly 
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
* have been completely destroyed during this process (it is likely that it would only be 
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
* method in {@link #onResume()} to guarantee that it will be called. 
*/ 
private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

/** 
* This is where we can add markers or lines, add listeners or move the camera. In this case, we 
* just add a marker near Africa. 
* <p> 
* This should only be called once and when we are sure that {@link #mMap} is not null. 
*/ 
private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
} 

}

回答

0

你所得到的API密鑰(欲瞭解更多信息,請使用this)的第一個。 然後,對於調試模式,您必須將您的api密鑰複製到'app \ src \ debug \ res \ values \ google_maps_api.xml'並將其發佈到'app \ src \ release \ res \ values \ google_maps_api.xml' 。

+0

我沒有看到該應用程序的\ src \調試或應用程序的\ src \目錄發佈在我的項目。 – Spectrem

0

我只是創造了一個新的目錄,新的密鑰,一個新的項目,項目編譯和運行良好