简体   繁体   中英

Implementing Google Maps V2 for API Level 8-10

I developing an app that requires using Google Map to show places around the city. Because Google Map V2 works for API level 12 and above, or so I'am told, I have used SupportMapFramgment like below.

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));

    }
......

When I run the aplication in a device of level 8-10 it gives me the error stack below

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

From the error stack I get the message that the error is created exactly at this line of code:

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

Can anybody help me with this please.

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

Your test environment does not have Google Play Services, and therefore you cannot display Google Maps using Maps V2. Try again with a device that has Google Play Services. Or, follow the instructions to add code to detect this case and help the user to install Google Play Services, if it is available for their device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM