2013-08-29 93 views
1

這是我從示例地圖應用程序複製的代碼,即使示例地圖應用程序未顯示地圖。Android簡單地圖視圖不顯示地圖v2

所以我分別複製並粘貼了我的API密鑰的代碼。這次也沒有顯示地圖。

主要活動

package com.example.map; 

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; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

public class MainActivity extends FragmentActivity { 

    private GoogleMap mMap; 

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

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

    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(); 
      } 
     } 
    } 

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

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<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"/> 

Android清單文件

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <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"/> 
    <!-- External storage for caching. --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <!-- My Location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <!-- Maps API needs OpenGL ES 2.0. --> 
    <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.maps.v2.API_KEY" 
       android:value="AIzaSyDWHZJWlXbCOiVk4xlbmPfOGi6eMFTOCYM"/> 

    <activity 
      android:name="com.example.map.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> 
    </application> 
</manifest> 

OUT PUT:

http://i.stack.imgur.com/CzKs5.png

+1

你有沒有例外的應用程序崩潰? – Raghunandan

+0

確保您使用的API密鑰是用於地圖而不是其他Google apis。也許[這](http://stackoverflow.com/questions/18498107/android-blank-page-when-using-google-map-api-2)與您的問題有關? – linus

+0

沒有我的應用不崩潰它沒有顯示地圖 – Raaja

回答

0

嘗試添加這兩個權限:

<permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>

,並嘗試測試在真實設備上該應用程序。

看到你的堆棧跟蹤將是有用的頂部。

+0

我試圖在真實設備上測試,但顯示證書錯誤 – Raaja

+0

我認爲你不再需要這些權限。 – Mitch

0
I changed the code like this 

**Manifest** 

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <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"/> 
    <!-- External storage for caching. --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <!-- My Location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 


    <permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> 
    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/> 
    <!-- Maps API needs OpenGL ES 2.0. --> 
    <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.maps.v2.API_KEY" 
       android:value="AIzaSyDWHZJWlXbCOiVk4xlbmPfOGi6eMFTOCYM"/> 

    <activity 
      android:name="com.example.map.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> 
    </application> 
</manifest> 





**LOGCAT** 

Again the map is not showing 

08-30 10:20:30.444: I/Choreographer(1166): Skipped 30 frames! The application may be doing too much work on its main thread. 

08-30 10:20:30.724: D/dalvikvm(1166): GC_FOR_ALLOC freed 302K, 11% free 3645K/4056K, paused 72ms, total 95ms 

08-30 10:20:31.084: D/dalvikvm(1166): GC_FOR_ALLOC freed 154K, 13% free 3655K/4188K, paused 200ms, total 201ms 

08-30 10:20:31.474: D/dalvikvm(1166): GC_FOR_ALLOC freed 286K, 16% free 3524K/4188K, paused 150ms, total 174ms 

08-30 10:20:31.554: D/dalvikvm(1166): GC_FOR_ALLOC freed 154K, 16% free 3522K/4188K, paused 31ms, total 31ms 

08-30 10:20:31.625: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3522K/4188K, paused 45ms, total 45ms 

08-30 10:20:31.674: D/dalvikvm(1166): GC_FOR_ALLOC freed 172K, 16% free 3523K/4188K, paused 31ms, total 32ms 

08-30 10:20:31.734: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3523K/4188K, paused 32ms, total 33ms 

08-30 10:20:31.794: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3523K/4188K, paused 45ms, total 46ms 

08-30 10:20:31.854: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3523K/4188K, paused 35ms, total 37ms 




**Console** 
[2013-08-30 10:20:22 - MAp] ------------------------------ 

[2013-08-30 10:20:22 - MAp] Android Launch! 

[2013-08-30 10:20:22 - MAp] adb is running normally. 

[2013-08-30 10:20:22 - MAp] Performing com.example.map.MainActivity activity launch 

[2013-08-30 10:20:22 - MAp] Automatic Target Mode: Unable to detect device compatibility. 

Please select a target device. 

[2013-08-30 10:20:25 - MAp] Application already deployed. No need to reinstall. 

[2013-08-30 10:20:25 - MAp] Starting activity com.example.map.MainActivity on device 
emulator-5556 

[2013-08-30 10:20:27 - MAp] ActivityManager: Starting: Intent { 
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 
cmp=com.example.map/.MainActivity } 
0

添加在你的manifest文件 - 使用許可權的android:NAME =「com.example.map.permission.MAPS_RECEIVE」 添加爲你得了這是考慮API密鑰: 主要用於瀏覽器應用程序(與參照網址)

從「API訪問」 https://code.google.com/apis/console/?pli=1#project:606531687416:access

點擊還要檢查所有有效的服務地圖。如鏈接中服務菜單中的Google Maps Android API v2。 希望它能起作用。