我使用LocationClient获取位置。使用WiFi连接它工作正常,但没有WiFi,它重复相同的位置(即使与移动数据连接!)。我使用了LocationManager,但它没有用。
我想知道谷歌地图如何正常工作(使用移动数据连接),但我找不到合适的位置。
使用“LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY”或“LocationRequest.PRIORITY_HIGH_ACCURACY”会产生相同的结果。
这是我的代码:
public class LocationTracker implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
LocationClient mLocationClient;
Location mCurrentLocation;
LocationRequest mLocationRequest;
LocationListener locationListener;
Handler handler = new Handler();
private static final int MILLISECONDS_PER_SECOND = 1000;
public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
private static final int FASTEST_INTERVAL_IN_SECONDS = 5;
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
* FASTEST_INTERVAL_IN_SECONDS;
Context context;
public LocationTracker(Context context) {
this.context = context;
}
public void getLocation() throws Exception {
try {
final int result = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (result != ConnectionResult.SUCCESS) {
Toast.makeText(
context,
"Google Play service is not available (status="
+ result + ")", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
throw new Exception(e);
}
mLocationClient = new LocationClient(context, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationClient.connect();
}
private final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Toast.makeText(context, "Connection Failed", Toast.LENGTH_LONG).show();
}
@Override
public void onConnected(Bundle connectionHint) {
locationListener = new MyLocationListener();
mLocationClient.requestLocationUpdates(mLocationRequest,
locationListener);
mCurrentLocation = mLocationClient.getLastLocation();
}
@Override
public void onDisconnected() {
}
答案 0 :(得分:0)
在代码中尝试这种方式
if(isInternetEnabled==true)//check for internet connection here
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
}
else
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
}
因为我发现GPS_PROVIDER
没有快速响应位置更新,然后NETWORK_PROVIDER
。
Fused Location provider example它解决了你的问题试试吧....
答案 1 :(得分:0)
我解决了。 LocationClient.getLastLocation()返回上次缓存的位置。我必须检查LocationListener.onLocationChanged()中的位置,如果location与上一个缓存位置不相等,我可以使用它。
这是代码:
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
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.location.LocationRequest;
public class LocationTracker implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
LocationClient mLocationClient;
Location mCurrentLocation;
LocationRequest mLocationRequest;
LocationListener locationListener;
Handler handler = new Handler();
private static final int MILLISECONDS_PER_SECOND = 1000;
public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
* FASTEST_INTERVAL_IN_SECONDS;
Context context;
public LocationTracker(Context context) {
this.context = context;
}
public void getLocation() throws Exception {
try {
final int result = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (result != ConnectionResult.SUCCESS) {
Toast.makeText(
context,
"Google Play service is not available (status="
+ result + ")", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
throw new Exception(e);
}
mLocationClient = new LocationClient(context, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationClient.connect();
}
private final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
// sendLocation(location);
if (!(location.getLatitude() == mCurrentLocation.getLatitude() && location
.getLongitude() == mCurrentLocation.getLongitude())) {
mLocationClient.removeLocationUpdates(locationListener);
mLocationClient.disconnect();
//use location as answer
}
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Toast.makeText(context, "Connection Failed", Toast.LENGTH_LONG).show();
}
@Override
public void onConnected(Bundle connectionHint) {
final LocationManager manager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//GPS provider is not enabled
return;
}
locationListener = new MyLocationListener();
mLocationClient.requestLocationUpdates(mLocationRequest,
locationListener);
mCurrentLocation = mLocationClient.getLastLocation();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (mLocationClient.isConnected()) {
mLocationClient.removeLocationUpdates(locationListener);
mLocationClient.disconnect();
//GPS can not find the location
}
}
}, 2 * 60 * 1000);
}
@Override
public void onDisconnected() {
}
}