GoogleMap V2无法加载地图。联系Google服务器时出错

问题描述 投票:2回答:2

我正在使用Google Api 19.在运行Google Map V2时,在运行时出现Stacktrace错误。无法加载地图。与Google服务器联系时出错。这可能是身份验证问题(但可能是由于网络错误)。

StackTrace:

 E/Random(2450)          : > 17.384058210414967, 78.48609545247936
 E/Random(2450)          : > 17.385480575802443, 78.48634970995319
 E/Random(2450)          : > 17.384707320313552, 78.48718165182942
 E/Random(2450)          : > 17.384469518257607, 78.48737972822704
 E/Random(2450)          : > 17.386012910991372, 78.48664538492055
 E/Random(2450)          : > 17.384268963692328, 78.48682608163563
 E/Random(2450)          : > 17.385942881352822, 78.48616019819285
 E/Random(2450)          : > 17.384971626221553, 78.48736770924768
 E/Random(2450)          : > 17.384794739104553, 78.48631955135781
 E/Random(2450)          : > 17.38564903886939, 78.48614895429334
 D/gralloc_goldfish(2450): Emulator without GPU emulation detected.
 I/Choreographer(2450)   : Skipped 621 frames!  The application may be doing too much work on its main thread.
 E/Google Maps Android API(2450): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).

Manifest:

<?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="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">

        <meta-data
     android:name="com.google.android.gms.version"
     android:value="@integer/google_play_services_version" />

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

    </application>   

</manifest>       

MainActivity.java:

package com.map.googlemap;

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


            // 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 = 12.987100;
            double longitude = 80.251692;

            // lets place some 10 random markers
            for (int i = 0; i <1; 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_ROSE));
                if (i == 1)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));

                googleMap.addMarker(marker);

                // Move the camera to last position with a zoom level
                if (i == 0) {
                    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();
            }
        }
    }


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

输出:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9XMXNsNS5wbmcifQ==” alt =“在此处输入图像描述”>“ >>

我不知道如何解决这些stackTrace错误。任何人都可以帮助我解决这些。谢谢。

我使用的是Google Api19。运行Google Map V2时,在运行时出现Stacktrace错误。无法加载地图。与Google服务器联系时出错。这可能是身份验证问题(...

android google-maps google-maps-android-api-2
2个回答
1
投票

我通过执行以下步骤解决了这些问题:


0
投票

对我来说,问题是我在Manifest中的application标记下使用了network_security_config设置。

© www.soinside.com 2019 - 2024. All rights reserved.