0

我使用 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() {

}
4

2 回答 2

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

融合位置提供程序示例,它可以解决您的问题,试试吧....

于 2014-07-26T08:42:37.943 回答
0

我解决它。LocationClient.getLastLocation() 返回上次缓存的位置。我必须检查 LocationListener.onLocationChanged() 中的位置,如果位置与上次缓存的位置不相等,我可以使用它。

这是代码:

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() {

}

}

于 2014-08-17T13:52:43.583 回答