我在log cat中收到此消息:“无法加载地图。联系Google服务器时出错。这可能是身份验证问题(但可能是由于网络错误)” 一切正常,但地图不显示。我得到空地图。
我已添加:
清单的权限
我已将该项目与Google服务相关联
我添加了我的api密钥。
这是来自控制台的消息:
[2014-06-08 19:07:13 - GeoCode_solution] Installation failed due to invalid APK file!
[2014-06-08 19:07:13 - GeoCode_solution] Please check logcat output for more details.
[2014-06-08 19:07:13 - GeoCode_solution] Launch canceled!
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gmapsapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.gmapsapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.gmapsapp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gmapsapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value= "my Api key "
/>
</application>
</manifest>
主要活动:
package com.example.gmapsapp;
import java.io.IOException;
import java.util.List;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;
@SuppressWarnings("unused")
private static final double SEATTLE_LAT = 47.60621,
SEATTLE_LNG =-122.33207,
SYDNEY_LAT = -33.867487,
SYDNEY_LNG = 151.20699,
NEWYORK_LAT = 40.714353,
NEWYORK_LNG = -74.005973;
private static final float DEFAULTZOOM = 15;
private static final String LOGTAG = "Maps";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
gotoLocation(SEATTLE_LAT, SEATTLE_LNG, DEFAULTZOOM);
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
else {
setContentView(R.layout.activity_main);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFrag.getMap();
}
return (mMap != null);
}
private void gotoLocation(double lat, double lng) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
mMap.moveCamera(update);
}
private void gotoLocation(double lat, double lng,
float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.moveCamera(update);
}
public void geoLocate(View v) throws IOException {
hideSoftKeyboard(v);
EditText et = (EditText) findViewById(R.id.editText1);
String location = et.getText().toString();
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(location, 1);
Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, DEFAULTZOOM);
}
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
log cat:
06-08 18:49:53.551: I/u(905): Making Creator dynamically
06-08 18:49:53.566: I/Google Maps Android API(905): Google Play services client version: 4452000
06-08 18:49:53.576: I/Google Maps Android API(905): Google Play services package version: 4452038
06-08 18:49:53.821: I/fpp(905): Making Creator dynamically
06-08 18:49:53.821: I/Google Maps Android API(905): Google Play services client version: 4452000
06-08 18:49:53.851: W/SystemClock(905): time going backwards: prev 1059299461602523(ioctl) vs now 1059299460974649(ioctl), tid=954
06-08 18:49:53.971: D/AbsListView(905): Get MotionRecognitionManager
06-08 18:49:53.991: D/dalvikvm(905): GC_CONCURRENT freed 1053K, 11% free 17585K/19568K, paused 10ms+4ms, total 29ms
06-08 18:49:53.996: D/AbsListView(905): onVisibilityChanged() is called, visibility : 8
06-08 18:49:53.996: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:53.996: D/AbsListView(905): onVisibilityChanged() is called, visibility : 8
06-08 18:49:53.996: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:53.996: D/AbsListView(905): onVisibilityChanged() is called, visibility : 0
06-08 18:49:53.996: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:54.011: D/dalvikvm(905): GC_FOR_ALLOC freed 36K, 10% free 17619K/19568K, paused 10ms, total 10ms
06-08 18:49:54.011: I/dalvikvm-heap(905): Grow heap (frag case) to 18.169MB for 60496-byte allocation
06-08 18:49:54.021: D/dalvikvm(905): GC_FOR_ALLOC freed <1K, 10% free 17678K/19628K, paused 11ms, total 11ms
06-08 18:49:54.081: D/AbsListView(905): onVisibilityChanged() is called, visibility : 4
06-08 18:49:54.081: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:54.091: D/AbsListView(905): onVisibilityChanged() is called, visibility : 0
06-08 18:49:54.091: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:54.101: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:54.126: D/libEGL(905): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
06-08 18:49:54.146: D/libEGL(905): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
06-08 18:49:54.151: D/libEGL(905): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
06-08 18:49:54.216: I/(905): !@IMGSRV: DATE: 2013.04.06(14:40:28) | BUILDER: LSI3D | REVISION: 67a97310a91d120068d9
06-08 18:49:54.296: D/OpenGLRenderer(905): Enabling debug mode 0
06-08 18:49:54.336: D/AbsListView(905): unregisterIRListener() is called
06-08 18:49:54.481: D/dalvikvm(905): GC_FOR_ALLOC freed 898K, 12% free 18122K/20588K, paused 14ms, total 14ms
06-08 18:49:55.591: I/Google Maps Android API(905): Failed to contact Google servers. Another attempt will be made when connectivity is established.
06-08 18:49:56.121: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0
06-08 18:49:56.456: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 7 mFalseSizeCnt:0
06-08 18:49:56.766: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0
06-08 18:49:57.111: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 7 mFalseSizeCnt:0
06-08 18:49:57.551: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 28 mFalseSizeCnt:0
06-08 18:50:03.801: D/AbsListView(905): onVisibilityChanged() is called, visibility : 8
06-08 18:50:03.801: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:03.821: D/AbsListView(905): onDetachedFromWindow
06-08 18:50:03.821: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:03.941: I/Google Maps Android API(905): Google Play services package version: 4452038
06-08 18:50:03.971: D/AbsListView(905): Get MotionRecognitionManager
06-08 18:50:03.976: D/AbsListView(905): onVisibilityChanged() is called, visibility : 8
06-08 18:50:03.976: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:03.976: D/AbsListView(905): onVisibilityChanged() is called, visibility : 8
06-08 18:50:03.976: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:03.976: D/AbsListView(905): onVisibilityChanged() is called, visibility : 0
06-08 18:50:03.976: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:03.996: D/AbsListView(905): onVisibilityChanged() is called, visibility : 4
06-08 18:50:03.996: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:04.001: D/AbsListView(905): onVisibilityChanged() is called, visibility : 0
06-08 18:50:04.001: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:04.001: W/IInputConnectionWrapper(905): showStatusIcon on inactive InputConnection
06-08 18:50:04.011: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:04.061: E/ViewRootImpl(905): sendUserActionEvent() mView == null
06-08 18:50:04.076: D/AbsListView(905): unregisterIRListener() is called
06-08 18:50:04.131: W/IInputConnectionWrapper(905): setComposingText on inactive InputConnection
06-08 18:50:04.141: D/dalvikvm(905): GC_CONCURRENT freed 2076K, 14% free 18557K/21568K, paused 2ms+3ms, total 17ms
06-08 18:50:04.141: D/dalvikvm(905): WAIT_FOR_CONCURRENT_GC blocked 14ms
06-08 18:50:04.171: D/dalvikvm(905): GC_CONCURRENT freed 658K, 15% free 18505K/21568K, paused 2ms+2ms, total 16ms
06-08 18:50:04.171: D/dalvikvm(905): WAIT_FOR_CONCURRENT_GC blocked 15ms
06-08 18:50:04.196: D/dalvikvm(905): GC_CONCURRENT freed 661K, 15% free 18389K/21568K, paused 3ms+2ms, total 17ms
06-08 18:50:05.866: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 7 mFalseSizeCnt:0
06-08 18:50:06.146: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 7 mFalseSizeCnt:0
06-08 18:50:06.746: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 23 mFalseSizeCnt:0
06-08 18:50:07.031: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 6 mFalseSizeCnt:0
06-08 18:50:07.246: D/GestureDetector(905): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 6 mFalseSizeCnt:0
06-08 18:50:10.641: E/Google Maps Android API(905): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).