为API级别8-10实施Google Maps V2

时间:2014-05-29 11:37:54

标签: android google-maps

我开发的应用需要使用Google地图来显示城市周边的地方。因为谷歌地图V2适用于12级及以上的API,或者我告诉过,我使用了如下的SupportMapFramgment。

activity_map_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/rectangle" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/button_layout"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop = "7dp"
        android:layout_marginBottom="7dp"
        android:background="@drawable/background"
        android:orientation="vertical"
        android:paddingBottom="1dp"
        android:paddingTop="1dp" >

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

    <LinearLayout
        android:id="@+id/button_layout"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop= "7dp"
        android:layout_marginBottom= "7dp"
        android:orientation="horizontal"
        android:weightSum="100" >

        <ImageButton
            android:id="@+id/btn_sugested"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_marginRight="2dp"
            android:layout_weight="50"
            android:background="@android:color/transparent"
            android:onClick="sugestedBussiness"
            android:src="@drawable/te_sugjeruar_p" />

        <ImageButton
            android:id="@+id/btn_all"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="3dp"
            android:layout_weight="50"
            android:background="@android:color/transparent"
            android:onClick="allBussiness"
            android:src="@drawable/te_gjithe" />
    </LinearLayout>

</RelativeLayout>

MapViewActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_view);

        ............


        if (android.os.Build.VERSION.SDK_INT > 14) {
            getActionBar().setHomeButtonEnabled(true);
        }


        setMapFeatures();

}
.
.
.
.
.

private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map_list)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
.
.
.
.
..

private void setMapFeatures() {

        // Loading map
        initilizeMap();

        // Changing map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        // Showing / hiding your current location
        googleMap.setMyLocationEnabled(true);

        // Enable / Disable zooming controls
        googleMap.getUiSettings().setZoomControlsEnabled(true);

        // Enable / Disable my location button
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);

        // Enable / Disable Compass icon
        googleMap.getUiSettings().setCompassEnabled(true);

        // Enable / Disable Rotate gesture
        googleMap.getUiSettings().setRotateGesturesEnabled(true);

        // Enable / Disable zooming functionality
        googleMap.getUiSettings().setZoomGesturesEnabled(true);

        GeoLocationH myGPS = new GeoLocationH(MapViewActivity.this);
        if (myGPS.canGetLocation()) {

            myGPS.getLocation();
            myGPS.UpdateMyLocation();
            sourcePosition = new LatLng(MyLocationH.getMyLatitude(),
                    MyLocationH.getMyLongitude());



        }

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(sourcePosition).zoom(Utils.CAMERA_ZOOMING).build();

        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

    }
......

当我在8-10级的设备中运行应用程序时,它会给我下面的错误堆栈

05-29 13:24:57.547: W/GooglePlayServicesUtil(3792): Google Play services is missing.
05-29 13:24:57.555: W/GooglePlayServicesUtil(3792): Google Play services is missing.
05-29 13:24:57.571: W/GooglePlayServicesUtil(3792): Google Play services is missing.
05-29 13:24:57.571: W/GooglePlayServicesUtil(3792): Google Play services is missing.
05-29 13:24:57.579: W/GooglePlayServicesUtil(3792): Google Play services is missing.
05-29 13:24:57.610: W/GooglePlayServicesUtil(3792): Google Play services is missing.
05-29 13:24:57.618: D/AndroidRuntime(3792): Shutting down VM
05-29 13:24:57.618: W/dalvikvm(3792): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-29 13:24:57.618: E/AndroidRuntime(3792): FATAL EXCEPTION: main
05-29 13:24:57.618: E/AndroidRuntime(3792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.apk.myikubinfo/com.dev.apk.myikubinfo.MapViewActivity}: java.lang.NullPointerException
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.os.Looper.loop(Looper.java:130)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at java.lang.reflect.Method.invoke(Method.java:507)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at dalvik.system.NativeStart.main(Native Method)
05-29 13:24:57.618: E/AndroidRuntime(3792): Caused by: java.lang.NullPointerException
05-29 13:24:57.618: E/AndroidRuntime(3792):     at com.dev.apk.myikubinfo.MapViewActivity.setMapFeatures(MapViewActivity.java:290)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at com.dev.apk.myikubinfo.MapViewActivity.onCreate(MapViewActivity.java:114)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-29 13:24:57.618: E/AndroidRuntime(3792):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-29 13:24:57.618: E/AndroidRuntime(3792):     ... 11 more

从错误堆栈中我得到的消息是错误是在这行代码中创建的:

// Changing map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

请有人帮我这个。

1 个答案:

答案 0 :(得分:0)

W/GooglePlayServicesUtil(3792): Google Play services is missing.

您的测试环境没有Google Play服务,因此您无法使用Maps V2显示Google地图。请使用具有Google Play服务的设备再试一次。或者,follow the instructions to add code to detect this case并帮助用户安装Google Play服务(如果适用于他们的设备)。