2013-04-29 135 views
0

我試圖使用谷歌地圖v2構建一個簡單的行人死亡吸盤應用程序。但總是出現錯誤'授權失敗'和'無法聯繫谷歌服務器'。我已經檢查過API密鑰,在Google API控制檯中啓用「Google Maps Android v2」服務,並允許我的應用使用該API。但仍然無法工作。谷歌地圖v2 uthorization失敗,無法聯繫谷歌服務器

這裏是我的清單:

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 
    <permission 
     android:name="com.example.pdr.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="com.example.pdr.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_NETWORK_STATE"/> 
    <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="com.example.pdr.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> 
     <activity android:name="com.example.pdr.MapActivity"></activity> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyCu0QDFkPWOOAr0PACX2AUSSGkPWYmdpr0"/> 
    </application> 

</manifest> 

下面是使用地圖

public class MapActivity extends FragmentActivity { 

private MapView map = null; 
private GoogleMap gMap; 

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

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 

private void setUpMapIfNeeded() { 
    if (gMap == null) { 

     gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
    } 
} 

private void setUpMap() { 
    gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
} 
} 

活動最後,這裏的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MapActivity" > 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment"/> 

</RelativeLayout> 

任何人可以幫我嗎?我一直在努力的日子.. 在此先感謝,並對不起我的英語...

回答

2

此問題是與您的api密鑰生成過程有關。請按照下面提到的步驟生成密鑰- 1.使用keytool生成SHA-1。 2.在瀏覽器上打開谷歌控制檯,不要忘記啓用谷歌地圖Android API v2從服務,現在繼續API訪問,點擊創建新的Android密鑰,粘貼您的SHA-1連同分號和您的項目包名稱,然後點擊創建, 3.複製新的API密鑰並粘貼到你的清單中。 4.最重要的是,不要忘記從sdk的extras文件夾中導入谷歌播放服務庫,如果已經安裝,否則請進入Android sdk-manager-> extras-> google play服務,使其安裝,然後從您的sdk位置導入 - extras-> google-> google play服務。 5.現在將此lib添加到您的工作項目中。

這是執行谷歌地圖android v2的整個過程。 現在您可以運行您的項目,歡呼聲......

+0

請注意在啓用服務時在API控制檯中的API的名稱.. – 2013-04-30 06:50:04