Android:谷歌地图api版本2

时间:2013-01-20 09:36:18

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

我按照步骤配置Google Maps Android API v2 Official steps
我使用的是Android 3.1版本。 我收到错误,我运行项目。
清单文件

<?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.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="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>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="I paste my key as shown in picture"/>        
    </application>

</manifest>

Api Key MainActivity.java

package com.example.mapdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

activity_main.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.MapFragment"/>

logcat的

01-20 15:00:18.325: E/dalvikvm(856): Could not find class 'com.google.android.gms.maps.model.LatLng', referenced from method com.google_map.MainActivity.<clinit>
01-20 15:00:18.395: E/dalvikvm(856): Could not find class 'com.google.android.gms.maps.MapFragment', referenced from method com.google_map.MainActivity.onCreate
01-20 15:00:18.565: E/AndroidRuntime(856): FATAL EXCEPTION: main
01-20 15:00:18.565: E/AndroidRuntime(856): java.lang.ExceptionInInitializerError
01-20 15:00:18.565: E/AndroidRuntime(856):  at java.lang.Class.newInstanceImpl(Native Method)
01-20 15:00:18.565: E/AndroidRuntime(856):  at java.lang.Class.newInstance(Class.java:1301)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1663)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.os.Looper.loop(Looper.java:132)
01-20 15:00:18.565: E/AndroidRuntime(856):  at android.app.ActivityThread.main(ActivityThread.java:4025)
01-20 15:00:18.565: E/AndroidRuntime(856):  at java.lang.reflect.Method.invokeNative(Native Method)
01-20 15:00:18.565: E/AndroidRuntime(856):  at java.lang.reflect.Method.invoke(Method.java:491)
01-20 15:00:18.565: E/AndroidRuntime(856):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-20 15:00:18.565: E/AndroidRuntime(856):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-20 15:00:18.565: E/AndroidRuntime(856):  at dalvik.system.NativeStart.main(Native Method)
01-20 15:00:18.565: E/AndroidRuntime(856): Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.maps.model.LatLng
01-20 15:00:18.565: E/AndroidRuntime(856):  at com.google_map.MainActivity.<clinit>(MainActivity.java:32)
01-20 15:00:18.565: E/AndroidRuntime(856):  ... 15 more

我还想知道 Google map API v2

的最低要求

3 个答案:

答案 0 :(得分:1)

您应该尝试在this guide

中导入google play libs

答案 1 :(得分:1)

首先安装Google Play服务,如果您使用的是Android 2.2,请使用此代码

public class MainActivity extends FragmentActivity {    
    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        SupportMapFragment fragment = new SupportMapFragment();
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit();
    }    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

答案 2 :(得分:0)

我遇到了类似的问题,我通过以下方案步骤解决了这个问题:

-1-我下载了最新的ADT(Windows 64位版本23.0.3),以便安装Google Play服务API。

-2-问题仍然存在,在此link尝试@ user3469294答案。

ADT 64位版本出现问题,使用SupportMapFragment(保持崩溃),下载ADT 32位并在其上安装google play服务API然后使用它的库解决了我的问题