2014-02-26 53 views
1

我做了所有的步驟,我添加谷歌播放服務到我的項目,我什麼都做。我不是什麼問題。我創建我的api密鑰使用調試證書指紋在eclipse窗口中使用SHA1指紋> preferences> android;我使用的是LG E400 CON的Android 2.3.6MapView谷歌地圖api V2看起來空白只顯示網格+/-按鈕

MainActivity:

import android.os.Bundle; 
import android.app.Activity; 


import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapView; 
import com.google.android.gms.maps.MapsInitializer; 

public class MainActivity extends Activity { 
    GoogleMap map; 
    MapView m; 
    com.google.android.gms.maps.Projection projection; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    m=(MapView) findViewById(R.id.mapview); 
    m.onCreate(savedInstanceState); 
    map = m.getMap(); 
    if (map != null) { 
     // The Map is verified. It is now safe to manipulate the map. 

    }else{ 
     // check if google play service in the device is not available or out-dated. 
     GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

     // nothing anymore, cuz android will take care of the rest (to remind user to update google play service). 
    } 

    try{map.getUiSettings().setMyLocationButtonEnabled(false); 
    map.setMyLocationEnabled(true); 
    }catch(Exception ex){ex.printStackTrace();} 

    MapsInitializer.initialize(this); 

} 
@Override 
public void onResume() { 

    super.onResume(); 
    m.onResume(); 
} 
@Override 
public void onDestroy() { 
    super.onDestroy(); 
    m.onDestroy(); 
} 
@Override 
public void onLowMemory() { 

    super.onLowMemory(); 
    m.onLowMemory(); 
} 
@Override 
public void onSaveInstanceState(Bundle outState){ 
    super.onSaveInstanceState(outState); 
    m.onSaveInstanceState(outState); 
    } 
} 

佈局:

<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" 
tools:context=".MainActivity" > 


<com.google.android.gms.maps.MapView 
android:id="@+id/mapview" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 

</RelativeLayout> 

清單:

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

android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="9" 
    android:targetSdkVersion="15" /> 

<permission 
    android:name="com.example.voicesee.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.example.voicesee.permission.MAPS_RECEIVE" /> 
<!-- Copied from Google Maps Library/AndroidManifest.xml. --> 
<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 following two 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"/> 
<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" > 
    <meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" /> 


    <activity 
     android:name="com.example.voicesee.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="AIzaSyAguDjIFiVWi18ll7Yh0oa8C7hWju1PUzI"/> 


</application> 

感謝您的幫助!

回答

1

我有類似的問題,我的問題是因爲我選錯了調試密鑰庫。我正在使用Ubuntu機器。因爲我以root用戶身份打開eclipse,eclipse中默認的keystore位於我的根文件夾(/root/.android/.android/debug.keystore),而我正在用我的用戶文件夾中的調試密鑰庫創建api key。我添加了一個鏈接到回答問題: Google Maps V2 shows blank screen on android 2.2

+0

謝謝!!我閱讀鏈接頁面中的答案! – user3338328

0

的第一個項目,我沒有你爲什麼要使用MapView當你把你明白映射在Acitivity,因爲它是當你這樣做來顯示地圖的難度來說,谷歌的documentation狀態:

對於如果您希望定位早期的平臺,則使用MapFragment(或SupportMapFragment)的更簡單的顯示Map的方法。

其次,如果你已經這樣做,那麼你在履行它的一些錯誤,因爲你放置:在錯誤的地方

MapsInitializer.initialize(this); 

方法,爲documentation狀態:

如果在獲取地圖之前需要使用功能,請使用此類來初始化Google Maps Android API。必須調用它,因爲需要初始化諸如BitmapDescriptorFactory和CameraUpdateFactory等類。

+0

好吧!我看到我會糾正我的代碼,然後再試一次。謝謝!! – user3338328

0

您將希望使用一個片段來封裝您的地圖,並且片段僅受Android API級別11及以上(3.0及以上)的支持。這可能是你的問題的原因。

如提到的here,您需要爲您的API版本使用SupportMapFragment。

Here是學習如何使用它的鏈接。

如果這不起作用,我的猜測是API密鑰。我們遇到了網格和縮放按鈕加載的地方,而不是地圖,關鍵是罪魁禍首。我會通過步驟here來確保你已經正確地做了所有事情(包括使用谷歌播放服務)

+0

謝謝!我會做一些改變 – user3338328

0

在我的代碼,我改變的MapView與supportMapfragment,我刪除我以前debug.keytool並重建它,然後刪除我的舊apikey並創建一個新的密鑰。看來我的問題是與谷歌播放服務的連接,因爲它在添加GooglePlayServicesUtil.isGooglePlayServicesAvailable()之後起作用,並在調用setContentview()前驗證與Google Play服務的連接()