我正在使用Google Api 19.
我在运行Google map V2时在运行时收到Stacktrace错误。无法加载地图。联系Google服务器时出错这可能是一个身份验证问题(但可能是由于网络错误)。
堆栈跟踪:
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).
清单:
<?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) };
}
}
输出:
我不知道如何解决这些stackTrace Error.Anybody可以帮助我解决这些问题。谢谢。
答案 0 :(得分:1)
我通过执行以下步骤解决了这些问题:
在模拟器中:
您必须使用最新的API Google APIs(Google Inc.)-API Level
19
。
其他内容放在屏幕截图中。
API密钥:
创建新的API密钥并仔细检查Google Map API密钥和SHA密钥。
Google Play服务:
您必须下载最新的Google Play服务
com.android.vending-4.8.20.apk。如果谷歌播放该网站
服务没有工作意味着你可以找到很多网站
互联网名为com.android.vending 4.8.20.apk
。
您可以使用命令提示符运行Google Play服务
adb install com.android.vending 4.8.20.apk
。
注意:您必须下载最新的Google Play服务,因为年复一年的最新版本将在互联网上更新。
<强>输出强>:
答案 1 :(得分:0)
对我来说,问题在于我在Manifest中的application标签下使用了network_security_config设置。
我仍然不确定如何正确处理它,但是在我刚刚删除了该设置(有时我的项目不需要该设置)后,地图就开始起作用了。