2015-09-06 237 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

我只是创造了一个新的目录,新的密钥,一个新的项目,项目编译和运行良好