2013-08-29 51 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。 希望它能起作用。