2013-07-18 52 views
23

我正在谷歌地图文档中为Google Map Api v2(https://developers.google.com/maps/documentation/android/start#specify_settings_in_the_application_manifest)提供示例代码。Google Map API v2未显示设备上的地图

代码成功运行,但Map未加载到设备上。设备上只显示白色屏幕。我正在使用4.0.3版本的Android设备。

enter image description here

1)我启用了服务项目。

enter image description here

2)生成的密钥:

enter image description here

enter image description here

3) “的Google Play-services_lib” 作为图书馆项目:

enter image description here

4)Android清单:

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

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

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

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

    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> 
    <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" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <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="AIzaSyCvZrhQ8YBfyYBvTic3DSdu2O06nAmhpg4" /> 

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

5)的活动:

package com.example.mapdemo; 

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

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

6)布局:

<?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" 
      android:name="com.google.android.gms.maps.MapFragment"/> 
+1

我也搞不清楚这一点,似乎我第一次在API控制台中输入包的名称是不完整的。然后,即使重新安装后,地图仍然没有显示。但是我只需要等几分钟 - 出于某种原因,第一次花了很长时间。 – Ixx

+0

出于好奇 - 你是如何计算出权限的?我已经阅读了谷歌文档,它只是说你需要INTERNET,ACCESS_NETWORK_STATE,WRITE_EXTERNAL_STORAGE权限以及可选的ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION权限.....那么MAPS_RECEIVE和READ_GSERVICES来自哪里? – Chris

回答

15

使用片段活性

例如:

public class Maps extends FragmentActivity { 
    GoogleMap map; 
    double lat; 
    double lan; 
    boolean flag = false; 

    // private LocationManager lm; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mapptry); 

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

变化乌尔map.xml到

<fragment 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" /> 

编辑

要做这也

进口android.support.v4.app.FragmentActivity;

在你做这个右键项目 - >属性 - > buildpath-> Java构建路径 - >库..然后点击添加外部JAR

的去

用户\ android-软件开发工具包\演员\机器人\ SUPPORT \ V4

选择Android的支持,v4.jar

PS:做这一切前提是你r API密钥是正确的。如果您API密钥是错误的,那么也它只显示一个白色的屏幕

+0

不要像发布你的问题@vinidog时那样暴露你的API KEY,这不是很好的练习。您可能需要更新 – larrytech

+0

是的,我的API密钥无效,我重新生成了密钥并且它工作正常。 – ashishdhiman2007

3

尝试添加

GoogleMap googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); 

<?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" 
android:name="com.google.android.gms.maps.MapFragment" 
/> 

您已经添加之外的应用程序下面的代码替换您的XML标签,而不是放在标签内

<uses-feature 
android:glEsVersion="0x00020000" 
android:required="true" /> 

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

希望这可能有助于面临同样问题的人

1

可能是API密钥的问题,或者您只是在项目api控制台(https://console.developers.google.com/project)和您的eclipse Windows - >首选项 - > Android - >中检查SHA1指纹。构建是相同的,其他智能只是从控制台更新SHA1来构建路径,反之亦然。

0

最可能的是,当你确保下面提到的东西您的问题将得到解决:

  1. 激活谷歌地图API

  2. 使用正确的包名称,而生成API密钥(在我的情况下,这是错误).. 和这似乎问题您的情况也包名称似乎是错误的(com.example.mapdemo)

  3. 确保您使用正确的密钥库文件生成SHA1

希望这会有所帮助。