0

我尝试了很多 MAP 教程,但 mt 模拟器只显示一个空白窗口。即使我尝试了谷歌地图视图示例代码,它也显示相同。以下是我尝试过的代码,请任何人为此提供解决方案

活动

package info.androidhive.googlemapsv2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;

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

    try {
        // Loading map
        initilizeMap();

        // Changing map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

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

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

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

        double latitude = 17.385044;
        double longitude = 78.486671;

        // lets place some 10 random markers
        for (int i = 0; i < 10; i++) {
            // random latitude and logitude
            double[] randomLocation = createRandLocation(latitude,
                    longitude);

            // Adding a marker
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(randomLocation[0], randomLocation[1]))
                    .title("Hello Maps " + i);

            Log.e("Random", "> " + randomLocation[0] + ", "
                    + randomLocation[1]);

            // changing marker color
            if (i == 0)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
            if (i == 1)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
            if (i == 2)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
            if (i == 3)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            if (i == 4)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            if (i == 5)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
            if (i == 6)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED));
            if (i == 7)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
            if (i == 8)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
            if (i == 9)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

            googleMap.addMarker(marker);

            // Move the camera to last position with a zoom level
            if (i == 9) {
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(randomLocation[0],
                                randomLocation[1])).zoom(15).build();

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

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

}

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

/**
 * function to load map If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().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();
        }
    }
}

/*
 * creating random postion around a location for testing purpose only
 */
private double[] createRandLocation(double latitude, double longitude) {

    return new double[] { latitude + ((Math.random() - 0.5) / 500),
            longitude + ((Math.random() - 0.5) / 500),
            150 + ((Math.random() - 0.5) * 10) };
    }
}

布局

    <?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="info.androidhive.googlemapsv2"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <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" />

    <!-- Requires OpenGL ES version 2 -->
    <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="AIzaSyBZMlkOv4sj-M5JO9p6wksdax4TEjDVLgo" />
    </application>

</manifest>

输出

![在此处输入图像描述][1]

安慰

        [2013-09-04 12:21:48 - GoogleMapsV2] ------------------------------
    [2013-09-04 12:21:48 - GoogleMapsV2] Android Launch!
    [2013-09-04 12:21:48 - GoogleMapsV2] adb is running normally.
    [2013-09-04 12:21:48 - GoogleMapsV2] Performing info.androidhive.googlemapsv2.MainActivity activity launch
    [2013-09-04 12:21:48 - GoogleMapsV2] Automatic Target Mode: launching new emulator with compatible AVD '1'
    [2013-09-04 12:21:48 - GoogleMapsV2] Launching a new emulator with Virtual Device '1'
    [2013-09-04 12:21:49 - GoogleMapsV2] New emulator found: emulator-5554
    [2013-09-04 12:21:49 - GoogleMapsV2] Waiting for HOME ('android.process.acore') to be launched...
    [2013-09-04 12:22:33 - GoogleMapsV2] HOME is up on device 'emulator-5554'
    [2013-09-04 12:22:33 - GoogleMapsV2] Uploading GoogleMapsV2.apk onto device 'emulator-5554'
    [2013-09-04 12:22:35 - GoogleMapsV2] Installing GoogleMapsV2.apk...
    [2013-09-04 12:23:32 - GoogleMapsV2] Success!
    [2013-09-04 12:23:32 - GoogleMapsV2] Starting activity info.androidhive.googlemapsv2.MainActivity on device emulator-5554
    [2013-09-04 12:23:33 - GoogleMapsV2] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=info.androidhive.googlemapsv2/.MainActivity }
    [2013-09-04 12:23:34 - GoogleMapsV2] ------------------------------
    [2013-09-04 12:23:34 - GoogleMapsV2] Android Launch!
    [2013-09-04 12:23:34 - GoogleMapsV2] adb is running normally.
    [2013-09-04 12:23:34 - GoogleMapsV2] Performing info.androidhive.googlemapsv2.MainActivity activity launch
    [2013-09-04 12:23:34 - GoogleMapsV2] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD '1'
    [2013-09-04 12:23:34 - GoogleMapsV2] ------------------------------
    [2013-09-04 12:23:34 - GoogleMapsV2] Android Launch!
    [2013-09-04 12:23:34 - GoogleMapsV2] adb is running normally.
    [2013-09-04 12:23:34 - GoogleMapsV2] Performing info.androidhive.googlemapsv2.MainActivity activity launch
    [2013-09-04 12:23:34 - GoogleMapsV2] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD '1'
    [2013-09-04 12:23:36 - GoogleMapsV2] Application already deployed. No need to reinstall.
    [2013-09-04 12:23:36 - GoogleMapsV2] Starting activity info.androidhive.googlemapsv2.MainActivity on device emulator-5554
    [2013-09-04 12:23:40 - GoogleMapsV2] Application already deployed. No need to reinstall.
    [2013-09-04 12:23:40 - GoogleMapsV2] Starting activity info.androidhive.googlemapsv2.MainActivity on device emulator-5554
    [2013-09-04 12:23:40 - GoogleMapsV2] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=info.androidhive.googlemapsv2/.MainActivity }
    [2013-09-04 12:23:40 - GoogleMapsV2] ActivityManager: Warning: Activity not started, its current task has been brought to the front
    [2013-09-04 12:23:42 - GoogleMapsV2] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=info.androidhive.googlemapsv2/.MainActivity }
    [2013-09-04 12:23:42 - GoogleMapsV2] ActivityManager: Warning: Activity not started, its current task has been brought to the front



  [1]: http://i.stack.imgur.com/MJKPM.png
4

1 回答 1

1

看来您还没有创建自己的 API 密钥。首先,您必须使用密钥库中的 SHA1 证书生成 API 密钥,而不是尝试。

如果您想使用您的密钥库生成 SHA1 证书,请遵循以下步骤:

如何从 Java 的 keytool 获取 MD5 指纹,而不仅仅是 SHA-1?

要启用服务并生成密钥访问: https ://code.google.com/apis/console/

并确保在 Google API 控制台的服务选项卡中启用“Google Maps Android API v2”。

于 2013-09-11T04:42:02.597 回答