如何在Android应用程序中显示谷歌地图

时间:2014-06-15 17:22:56

标签: android api google-maps fragment

我想在我的应用程序中实现谷歌地图...我有地图api密钥并在我的xml文件中使用它但谷歌地图没有显示。当我将片段添加到xml文件时,它会导致强制关闭错误,当我更正此错误时,谷歌地图不会在运行时显示。 谢谢你的帮助。

MainActivity.java

package com.example.simplemap;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

    @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;
    }

}

AndroidMainifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <permission
 android:name="com.example.simplemap.permission.MAPS_RECEIVE"
 android:protectionLevel="signature"/>
<uses-permission android:name="com.example.simplemap.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"/> 

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

</manifest>

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"
    tools:context=".MainActivity" >

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

</RelativeLayout> 

logcat:            06-16 15:40:09.827:E / Trace(1343):错误打开跟踪文件:没有这样的文件或目录(2)     06-16 15:40:10.537:D / AndroidRuntime(1343):关闭VM     06-16 15:40:10.537:W / dalvikvm(1343):threadid = 1:线程退出未捕获异常(组= 0x40a71930)     06-16 15:40:10.557:E / AndroidRuntime(1343):致命异常:主要     06-16 15:40:10.557:E / AndroidRuntime(1343):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.simplemap / com.example.simplemap.MainActivity}:android.view.InflateException:Binary XML文件行#7:错误膨胀类片段     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.os.Handler.dispatchMessage(Handler.java:99)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.os.Looper.loop(Looper.java:137)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.ActivityThread.main(ActivityThread.java:5041)     06-16 15:40:10.557:E / AndroidRuntime(1343):at java.lang.reflect.Method.invokeNative(Native Method)     06-16 15:40:10.557:E / AndroidRuntime(1343):at java.lang.reflect.Method.invoke(Method.java:511)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)     06-16 15:40:10.557:E / AndroidRuntime(1343):at dalvik.system.NativeStart.main(Native Method)     06-16 15:40:10.557:E / AndroidRuntime(1343):引起:android.view.InflateException:二进制XML文件行#7:错误膨胀类片段     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.view.LayoutInflater.inflate(LayoutInflater.java:352)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.Activity.setContentView(Activity.java:1881)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.example.simplemap.MainActivity.onCreate(MainActivity.java:12)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.Activity.performCreate(Activity.java:5104)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)     06-16 15:40:10.557:E / AndroidRuntime(1343):......还有11个     06-16 15:40:10.557:E / AndroidRuntime(1343):引起:java.lang.IllegalStateException:应用程序的AndroidManifest.xml中的元数据标签没有正确的值。预期4323000但找到0.您必须在元素中包含以下声明:     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.maps.internal.q.B(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.maps.internal.q.A(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.maps.MapFragment $ b.gW(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.maps.MapFragment $ b.a(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.dynamic.a.a(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.dynamic.a.onInflate(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.app.Activity.onCreateView(Activity.java:4716)     06-16 15:40:10.557:E / AndroidRuntime(1343):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)     06-16 15:40:10.557:E / AndroidRuntime(1343):... 21更多     06-16 15:40:31.647:I / Process(1343):发送信号。 PID:1343 SIG:9

1 个答案:

答案 0 :(得分:0)

使用以下代码:

片段代码:

public static class MapsFragment extends Fragment {

        private SupportMapFragment suppfrag;
        static final LatLng myLoc = new LatLng(//latitiude and longitude here);
        private GoogleMap map;

        public static final String ARG_SECTION_NUMBER = "section_number";

        public MapsFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                return inflater.inflate(R.layout.fragment_map, container, false);
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            FragmentManager fm = getChildFragmentManager();
            suppfrag = (SupportMapFragment) fm.findFragmentById(R.id.map);
            if (suppfrag == null) {
                suppfrag = SupportMapFragment.newInstance();
                fm.beginTransaction().replace(R.id.map, suppfrag).commit();

                Marker location = map.addMarker(new MarkerOptions().position(CENTRU)
                        .title("Centru"));
                    location.setIcon(BitmapDescriptorFactory
                                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

                    // Move the camera instantly to Bottlecapp with a zoom of 15.
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTRU, 15));

                    // Zoom in, animating the camera.
                    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
                    getActivity().getSupportFragmentManager().popBackStack();
            }
        }
        @Override
        public void onResume() {
            super.onResume();
            if (map == null) {
                map = suppfrag.getMap();
                map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
            }
        }
    }

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"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>