如何在Android活动中添加地图?

时间:2014-08-02 06:19:57

标签: java android google-maps android-intent

我正在尝试在活动中添加地图,但没有让它工作!我尝试使用This教程链接。 我还尝试通过on activity中的View Map链接添加它。请有人帮帮我 这是我的logcat代码片段:

MainActivity.java     package com.example.map;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

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

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="com.example.map.MainActivity" >

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

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

</RelativeLayout>

的AndroidManifest.xml

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

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

<permission
    android:name="com.example.map.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.example.map.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" />


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

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyB2mLtP5XYhIq1KB1TEhE60wlcDxQSeUWA" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>

1 个答案:

答案 0 :(得分:1)

你的min sdk是8。

更改此

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

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

元标记必须是清单中Application标记的子标记。

... //rest of the code
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyB2mLtP5XYhIq1KB1TEhE60wlcDxQSeUWA" />
</application>

您在清单

中缺少一个metag标记
<meta-data 
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" /> // missing

你可以摆脱

<permission
android:name="com.example.map.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>