2014-01-17 79 views
0

我正在將Google地圖整合到一個小應用程序中。我AndroidManifest.xmlGoogle地圖加載問題

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.mymaps" 
    android:versionCode="1" 
    android:versionName="1.0" > 
<permission 
     android:name="com.example.mymaps.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 
    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="18" /> 
<uses-permission 
    android:name="info.androidhive.googlemapsv2.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" />  
    <!-- Required to show current location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  
    <!-- Required OpenGL ES 2.0. for Maps V2 --> 
    <uses-feature android:glEsVersion="0x00020000"/> 
    <uses-feature 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.mymaps.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.maps.v2.API_KEY" 
    android:value="MyKey" /> 
    </application>  
</manifest> 

而我的主要活動是

public class MainActivity extends Activity implements OnClickListener { 
    TextView lat,lng; 
    Button getCds,getMap; 
    private GoogleMap googleMap; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initializeVars(); 
    } 

    private void initializeVars() { 
     // TODO Auto-generated method stub 
     getCds = (Button)findViewById(R.id.bGC); 
     getMap = (Button)findViewById(R.id.bGM); 
     lat = (TextView)findViewById(R.id.TextView2); 
     lng = (TextView)findViewById(R.id.TextView4); 
     getCds.setOnClickListener(this); 
     getMap.setOnClickListener(this); 
     try { 
      // Loading map 
      initilizeMap(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void initilizeMap() { 
     if (googleMap == null) { 
      googleMap = ((MapFragment) getFragmentManager().findFragmentById(
        R.id.map)).getMap(); 

      // check if map is created successfully or not 
      if (googleMap == null) { 
       Toast.makeText(getApplicationContext(), 
         "Sorry! unable to create maps", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
    } 

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

    public void onClick(View v) { 
     Log.d("Debugging", "I am in onCLick"); 
     switch (v.getId()) { 

     case R.id.bGC: 
     { 
      Log.d("Debugging", "I am in case R.id.bGC"); 
      lat.setText("My Lat"); 
      lng.setText("My Long"); 
      break; 
     } 

     case R.id.bGM: 
     { 
      Log.d("Debugging", "I am in case R.id.bGM"); 
      lat.setText("blank"); 
      lng.setText("blank"); 
      break; 
     } 
     } 
    } 
} 

當我加載我提示以下錯誤:應用程序。

01-17 14:01:49.054: E/Google Maps Android API(2832): Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map. 
01-17 14:01:49.064: E/Google Maps Android API(2832): Ensure that the following correspond to what is in the API Console: Package Name: com.example.mymaps, API Key: MyKey, Certificate Fingerprint: SomeValue 
01-17 14:01:49.064: I/Google Maps Android API(2832): Failed to contact Google servers. Another attempt will be made when connectivity is established. 
01-17 14:01:59.234: D/dalvikvm(2832): GC_CONCURRENT freed 572K, 9% free 7576K/8263K, paused 6ms+3ms 
01-17 14:02:04.304: E/Google Maps Android API(2832): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors). 

夥計們給一點更多的視角。用於之前工作的相同代碼(可能是3-4個月前)。它似乎不再有效。我不確定是什麼原因導致相同。

你可以請指教如何調試一樣嗎?

+0

你不需要'permission.MAPS_RECEIVE'。 Google Play Services 3.1.59使其無效。資料來源:http://stackoverflow.com/questions/14832911/android-map-v2-why-maps-receive-permission – gian1200

+0

您使用的是Google Play服務的哪個版本? – gian1200

+0

我在哪裏檢查?對不起,我是新的,所以不知道在哪裏檢查相同。 – misguided

回答

1

具有

<uses-permission 
    android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" /> 

<uses-permission 
    android:name="com.example.mymaps.permission.MAPS_RECEIVE" /> 

的變化,我認爲這個問題是關鍵,這是不正確的,你正在使用,在您的清單您的API密鑰清楚與您在API控制檯中顯示的API密鑰不匹配。將API密鑰從控制檯粘貼到清單中。應該解決你的問題。

你可以卸載該應用,並做一個項目乾淨,然後重新安裝應用程序..有這樣的嘗試,但它仍然不作比

更多 請訪問爲完整的過程使用新的谷歌地圖

http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

http://www.learn2crack.com/2013/12/android-google-maps-api-v2-example.html

最好的教程是

http://codebybrian.com/2012/12/06/google_maps_android_v2_sample.html

0

your.package.name.permission.MAPS_RECEIVE不再需要。請從清單中刪除它。你也錯過了這個:

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" /> 

它應該被添加在<application>標籤。