我尝试按照http://developer.android.com/training/location/retrieve-current.html的指南获取当前位置
我的 google play 服务启动了,但是我的 locationclient 仍然无法连接,并且在我运行程序时出现这些错误。(PS:以前我的位置代码可以工作,但现在不行,奇怪)
干得好
package com.example.Wifin;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
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.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class myMap extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,LocationListener {
GoogleMap mMap;
LatLng Current;
Marker here;
LocationClient mLocationClient;
int resultCode;
Location mCurrentLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(ConnectionResult.SUCCESS == resultCode){
Log.d("Location Updates",
"Google Play services is available.");
}
else{
Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
}
mLocationClient = new LocationClient(this, this, this);
mCurrentLocation = mLocationClient.getLastLocation();
Current = new LatLng(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
here = mMap.addMarker(new MarkerOptions()
.position(Current)
.title("you're here!")
.snippet("Connect to home2, Signal Strenght -20dBm")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.testmarker)));
}
@Override
public void onConnectionFailed(ConnectionResult result) {
}
@Override
public void onConnected(Bundle connectionHint) {
mLocationClient.connect();
}
@Override
public void onDisconnected() {
}
@Override
public void onLocationChanged(Location arg0) {
}
}
下面是我的目录
09-16 21:31:37.187: D/dalvikvm(14050): GC_FOR_ALLOC freed 259K, 5% free 8612K/8992K, paused 14ms, total 15ms
09-16 21:31:37.447: D/dalvikvm(14050): GC_FOR_ALLOC freed 816K, 10% free 8307K/9176K, paused 15ms, total 15ms
09-16 21:31:37.527: D/dalvikvm(14050): GC_FOR_ALLOC freed 301K, 8% free 8517K/9176K, paused 17ms, total 17ms
09-16 21:31:37.607: D/dalvikvm(14050): GC_FOR_ALLOC freed 311K, 5% free 8717K/9176K, paused 17ms, total 17ms
09-16 21:31:37.627: D/Location Updates(14050): Google Play services is available.
09-16 21:31:37.637: D/AndroidRuntime(14050): Shutting down VM
09-16 21:31:37.637: W/dalvikvm(14050): threadid=1: thread exiting with uncaught exception (group=0x4180f700)
09-16 21:31:37.637: E/AndroidRuntime(14050): FATAL EXCEPTION: main
09-16 21:31:37.637: E/AndroidRuntime(14050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Wifin/com.example.Wifin.myMap}: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.os.Looper.loop(Looper.java:137)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-16 21:31:37.637: E/AndroidRuntime(14050): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 21:31:37.637: E/AndroidRuntime(14050): at java.lang.reflect.Method.invoke(Method.java:525)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-16 21:31:37.637: E/AndroidRuntime(14050): at dalvik.system.NativeStart.main(Native Method)
09-16 21:31:37.637: E/AndroidRuntime(14050): Caused by: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.u.y(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cm.a(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cm$c.y(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cl.getLastLocation(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cm.getLastLocation(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.location.LocationClient.getLastLocation(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.example.Wifin.myMap.onCreate(myMap.java:51)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.Activity.performCreate(Activity.java:5133)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-16 21:31:37.637: E/AndroidRuntime(14050): ... 11 more
09-16 21:31:37.927: D/dalvikvm(14050): GC_FOR_ALLOC freed 300K, 4% free 8930K/9284K, paused 18ms, total 18ms
09-16 21:31:38.987: I/Process(14050): Sending signal. PID: 14050 SIG: 9
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Wifin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" android:maxSdkVersion="18"/>
<permission android:name="com.example.Wifin.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="com.example.Wifin.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
这是我的清单文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@style/Theme.NoTitle" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
<activity android:name=".myMap"
android:label="@string/myMap"
/>
<receiver android:name=".WifiReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDlimK4K3HlYCjYHP4k9k6c-DWC0z-8JQ8"/>
</application>
</manifest>