Google Map API V2不会在Android上显示地图

时间:2013-06-16 05:42:50

标签: android api google-maps google-maps-api-2

我尝试从此tutorial学习Google Map API。我在教程中创建包名称,在教程的一半中更改xml文件中的代码只在java文件中没有做任何事情。当我运行我的项目时它没有错误。但它显示灰色背景不显示地图。我检查API KEY,我认为我使用sha1跟随;包名称,然后它显示API KEY这样

  

API密钥:AIzaSyC3B722X4KNSr3Ah-QIQEAhjJOYq0SCsRI

     

Android应用:   79:77:3E:70:B0:2E:75:99:62:6E:2D:DF:DB:1A:41:C3:5D:2D:52:67; ca.sfu.cmpt276.bfraser

这是我的代码

activity_main.xml中

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

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


</RelativeLayout>

清单

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

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

<permission
android:name="ca.sfu.cmpt276.bfraser.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="ca.sfu.cmpt276.bfraser.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"/>
<!-- 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.maps.v2.API_KEY"
android:value="AIzaSyC3B722X4KNSr3Ah-QIQEAhjJOYq0SCsRI"/>


<activity
android:name="ca.sfu.cmpt276.bfraser.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>

如何在Android中使用Google Map API显示地图?

2 个答案:

答案 0 :(得分:1)

你的min sdk是8.你应该使用支持片段。

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

您的活动必须延长FragmentActivity

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

确保您已添加支持库

同时确保导入以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;    

答案 1 :(得分:1)

根据我的经验,当您未正确设置密钥时会发生这种情况。要么你改变了包名并且没有意识到它。我有同样的问题。为了解决这个问题,我首先看一下应用程序在我的android模拟器上运行时的控制台。在它,它抛出错误说应用程序没有与谷歌沟通。这是解决此问题的最简单方法。

播放此视频:https://www.youtube.com/watch?v=vgr4l1nsFdU

从中,你会意识到你可能做错了什么。 从谷歌教程中,它说要使用keytool -list -v -keystore&#34;%USERPROFILE%.android \ debug.keystore&#34; -alias androiddebugkey -storepass android -keypass android。我想那可能是你弄错了。

相关问题