android - Google Map Android Api V2 示例代码不起作用

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

我曾尝试使用 Google map 示例代码,之前在 Google_Play_SERVICE/SAMPLE 中提供了所有可用选项,它显示了以下日志。

01-14 17:58:39.773: E/Google Maps Android API(13114): Authorization failure.

它向我展示了所有示例 map 选项的空白屏幕。

现在我创建了一个新项目,这里是它的完整代码:-

Activity.java

public class MapActivity extends FragmentActivity
{
    private Context context = this;

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

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

        int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        Log.e("Maps", "Result int value::" + result);
        switch (result) {
        case ConnectionResult.SUCCESS:
            Log.e("Maps", "RESULT:: SUCCESS");          
            break;

        case ConnectionResult.DEVELOPER_ERROR:
            Log.e("Maps", "RESULT:: DE");           
            break;

        case ConnectionResult.INTERNAL_ERROR:
            Log.e("Maps", "RESULT:: IE");           
            break;

        case ConnectionResult.INVALID_ACCOUNT:
            Log.e("Maps", "RESULT:: IA");           
            break;

        case ConnectionResult.NETWORK_ERROR:
            Log.e("Maps", "RESULT:: NE");           
            break;

        case ConnectionResult.RESOLUTION_REQUIRED:
            Log.e("Maps", "RESULT:: RR");           
            break;

        case ConnectionResult.SERVICE_DISABLED:
            Log.e("Maps", "RESULT:: SD");           
            break;

        case ConnectionResult.SERVICE_INVALID:
            Log.e("Maps", "RESULT:: SI");           
            break;

        case ConnectionResult.SERVICE_MISSING:
            Log.e("Maps", "RESULT:: SM");           
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            Log.e("Maps", "RESULT:: SVUR");         
            break;
        case ConnectionResult.SIGN_IN_REQUIRED:
            Log.e("Maps", "RESULT:: SIR");          
            break;      

        default:
            break;
        }
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.setMyLocationEnabled(true);
        Log.e("Maps", "------EOC-------");
    }
}

activity_map.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"/>

list .xml

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

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

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

    <permission
        android:name="net.dottech.map.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="net.dottech.map.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" >
        <activity
            android:name="net.dottech.map.MapActivity"
            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="removed" />
    </application>

</manifest>

日志

01-21 13:09:09.243: E/Maps(449): Result int value::0
01-21 13:09:09.243: E/Maps(449): RESULT:: SUCCESS
01-21 13:09:09.243: E/Maps(449): ------EOC-------
01-21 13:09:09.248: D/LocationManagerService(1852): gps location requested by an application
01-21 13:09:09.253: D/SensorManager(449): registerListener :: handle = 0  name= K3DH 
Acceleration Sensor delay= 20000 Listener= maps.i.a@41a054f8
01-21 13:09:09.353: D/(449): Device driver API match
01-21 13:09:09.353: D/(449): Device driver API version: 10
01-21 13:09:09.353: D/(449): User space API version: 10 
01-21 13:09:09.353: D/(449): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Fri May  4 10:32:42 KST 2012
01-21 13:09:12.398: D/Sensors(449): Remain listener = Sending .. normal delay 200ms
01-21 13:09:12.398: I/Sensors(449): sendDelay --- 200000000
01-21 13:09:12.398: I/Sensors(1852): setDelay :: handle = 0   delay = 200000000
01-21 13:09:12.398: D/SensorManager(449): JNI - sendDelay
01-21 16:14:17.835: E/Google Maps Android API(12168): Failed to load map.  Could not contact Google servers.

截图

Google Map Android Api V2 Example tried, no success

这是结果,带有缩放选项的空白屏幕,MapView 中没有 map 。

我已遵循以下提到的每一步:-

1.) Tutorial

2.) Google Map Android Api V2

最佳答案

请检查..

  1. 检查您的项目中是否存在包含“android-support-v4.jar”的“libs”文件夹。

    “android-support-v4.jar”位于“android-sdk”目录下的“/extras/android/compatibility/v4/android-support-v4.jar”中。

  2. 在运行您的项目之前,您必须将项目构建目标设置为“Google APIs”,而不是 Android x.x。 version :选择您的项目,然后在 Eclipse 中单击“项目”>“属性”>“项目构建目标”,然后选择任何“Google APIs”,然后在您的手机上运行您的项目。如果您使用模拟器,还必须将模拟器的 AVD 设置为任何“Google API”。

  3. 再次声明,您无需创建新的 Google Maps API key 来测试您的项目,只需使用默认提供的 API key ,它显示为“浏览器应用程序 key (带引荐来源网址)” “在您的 Google API 控制台中。

  4. 最后,最重要的是将Google Play 服务添加为Android 库项目,如下所示:

    选择"file">“导入”>“Android”>“将现有的 Android 代码导入工作区”,然后单击“下一步”。 选择浏览...,输入/extras/google/google_play_services/libproject/google-play-services_lib,然后单击完成。

关于android - Google Map Android Api V2 示例代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14216205/

相关文章:

android - Actionbarsherlock 中的选项卡总是在操作栏内?

android - 如何维护多层 ImageView 并根据最大的一个保持它们的纵横比?

android - 使用 LiveData 时,为什么要在 ViewModel 类中双重声明变量?

android - Maps v2 信息窗口在触摸标记时不显示,但在触摸不可见的信息窗口后显示

android - 在 ScrollView 中滚动到 TableLayout 的最后一行

android - 我移动时如何在谷歌地图上绘制路径?

android - 捕获 Google Map Android API V2 的屏幕截图

android - 禁用 GoogleMaps v2 View 以便它忽略所有触摸事件

android - Android Maps Api V2 中是否有类似于 onFirstFix 方法的功能?

java - 如何在 Google Maps Android API v2 中向 MapFragment 添加自定义控件?