-1

尝试在 android 中执行 MAP-API 程序

MainActivity.java

public class MainActivity extends FragmentActivity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

显现

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



    <uses-sdk
        android:minSdkVersion="12"
        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" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="-----key-------------" />
    </application>

</manifest>

[编辑]


显现

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

    <uses-sdk
        android:minSdkVersion="11"
        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" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.mapearth.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="---key----" />
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

mainActivity.java

public class MainActivity extends FragmentActivity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // check if map is created successfully or not

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

在此处输入图像描述

我也有

在此处输入图像描述

在此处输入图像描述



[最终编辑]

日志::

[2013-10-19 10:57:21 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/android/gms/common/data/Freezable;
[2013-10-19 10:57:21 - MapEarth] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/android/gms/common/data/Freezable;
4

5 回答 5

4

Map V2:在此处输入图像描述在这里检查您正在使用 MapFragment 和 SupportMapFragment 的布局,因此您收到错误 Dont Use
android:name="com.google.android.gms.maps.MapFragment"

仅使用

class="com.google.android.gms.maps.SupportMapFragment"

在清单中:将包名称更改为您的

<activity
            android:name="com.example.mapearth.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >

选择 google play 库

于 2013-10-18T10:59:34.780 回答
0

我面临的错误之一是......即使我将项目导入到我的工作区


  • 我无法链接它......
  • 我花了一整天的时间才发现....错误是什么....(这是一个愚蠢的错误)

在此处输入图像描述

我没有选择“将项目复制到工作区选项

现在我能够成功导入它

于 2013-10-19T05:43:26.060 回答
0

您需要在 activity_main.xml 中使用 SupportMapFragment,如下所示:

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

该活动将您的 XML 解释为 SupportMapFragment,但是在您的 XML 布局中,您声明它是一个普通的地图片段,从而导致错误。将其更改为 SupportMapFragment 并且它应该正确解释它。

于 2013-10-18T11:39:47.167 回答
0

尝试用它替换您的 activity_main.xml 文件的内容:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <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" />          
</LinearLayout>
于 2013-10-18T11:15:51.207 回答
0

您的包裹名称是

  package="com.example.mapearth"

但是在活动声明中你有

  <activity
        android:name="info.androidhive.googlemapsv2.MainActivity"

改成

  <activity
        android:name="com.example.mapearth.MainActivity"

对于 api 12 及更高版本,请使用MapFragmentelse 使用SupportMapFragment.

如果您正在使用,则MapFramgent需要扩展标准Activity并使用getFragmentManager

将您的 min sdk 更改为 11 或以下

您也可以删除这些

<permission
        android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />

查看指定权限

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

还要从您的 xml 中删除以下内容

  android:name="com.google.android.gms.maps.MapFragment"
  // in case your min sdk is 11 and below use SupportMapFragment 

编辑:

在xml中你有

    android:name="com.google.android.gms.maps.MapFragment" 

在初始化时处于活动状态,您有

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

所以改为

    android:name="com.google.android.gms.maps.SupportMapFragment"  

和这个

    <uses-sdk
    android:minSdkVersion="11" // 11 or below

编辑:

正确参考google play services library project

导入 google-play-service 库,在此参考 android 旁边显示一个红色 X

于 2013-10-18T10:57:19.800 回答