【发布时间】:2014-05-18 17:22:07
【问题描述】:
我目前正在尝试 Google 服务提供的 LocationClient。当我在建筑物旁边时,我会收到频繁的位置更新,但在道路中间,它们的频率较低且准确性较低。在乡下,根本没有更新。我想知道谷歌位置服务是否使用 GPS,所以我禁用了所有位置提供程序,但手机设置中的 GPS 除外,我立即停止接收任何更新。显然,我的 LocationClient 没有收到任何使用 GPS 数据的更新。
我错过了什么吗?我必须为 LocationClient 设置什么,还是必须使用标准的 LocationManager.GPS_PROVIDER?
我正在尝试构建一个运行应用程序,我需要准确的位置数据。任何建议将不胜感激;-)!
这是我使用的实现:
public class CustomLocationProvider implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener, android.location.LocationListener {
private static final float DATA_UPDATE_DISTANCE = 100.0f;
private static final long ONE_MIN = 1000 * 60;
private static final long FIVE_MIN = ONE_MIN * 5;
private static final int UPDATE_TIME = 5000;
private static final int FASTEST_INTERVAL = 16;
private static final int ACCURACY_THRESHOLD = 30;
private Context mContext;
private LocationClient mLocationClient;
private Location mPreviousLocation;
private float mTotalDistance;
private float mDistanceSinceLastUpdate = 0.0f;
private WeakReference<Activity> mDelegate;
public interface LocationProviderDelegate {
void locationUpdated(Location location);
}
// These settings are the same as the settings for the map. They will in fact give you updates
// at the maximal rates currently possible.
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(UPDATE_TIME) // 5 seconds
.setFastestInterval(FASTEST_INTERVAL) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
public CustomLocationProvider (Context context, Activity delegate) {
if(context != null) {
mContext = context;
mDelegate = new WeakReference<Activity>(delegate);
}
}
public void enableTracking(boolean enable) {
if(enable) {
setUpLocationClientIfNeeded();
mLocationClient.connect();
} else {
if(mLocationClient != null) {
mLocationClient.disconnect();
}
}
}
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(
mContext,
this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}
@Override
public void onLocationChanged(Location location) {
if(mPreviousLocation != null
&& location.getTime() > mPreviousLocation.getTime()
&& location.getAccuracy() < ACCURACY_THRESHOLD)
{
float distanceIncrement = mPreviousLocation.distanceTo(location);
mDistanceSinceLastUpdate += distanceIncrement;
mTotalDistance += distanceIncrement;
if(mDistanceSinceLastUpdate >= DATA_UPDATE_DISTANCE)
{
mDistanceSinceLastUpdate = 0.0f;
requestDataUpdate(location);
}
updateLocation(location);
} else if( mPreviousLocation == null){
requestDataUpdate(location);
updateLocation(location);
}
}
private void updateLocation(Location location) {
try {
((LocationProviderDelegate) mDelegate.get()).locationUpdated(location);
} catch (Exception e) {
Logger.logError("Cannot cast as a LocationProviderDelegate");
}
mPreviousLocation = location;
}
/**
* Callback called when connected to GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onConnected(Bundle connectionHint) {
mLocationClient.requestLocationUpdates(
REQUEST,
this); // LocationListener
Location location = mLocationClient.getLastLocation();
if (location != null) {
if (age(location) < FIVE_MIN) {
mPreviousLocation = location;
requestDataUpdate(location);
}
}
}
/**
* Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}.
*/
@Override
public void onDisconnected() {
// Do nothing
}
/**
* Implementation of {@link OnConnectionFailedListener}.
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
Logger.postMessageToTheUser("Connection failed", mDelegate.get());
}
public void Reset(){
mTotalDistance = 0;
}
public float TotalDistance() {
return mTotalDistance;
}
public void requestDataUpdate(Location location)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
double offset = 0.002;
LatLngBounds bounds = LatLngBounds.builder()
.include(new LatLng(latitude - offset, longitude - offset))
.include(new LatLng(latitude + offset, longitude + offset))
.build();
updateData();
}
private long age(Location location) {
return System.currentTimeMillis() - location.getTime();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
String newStatus = "";
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
newStatus = "OUT_OF_SERVICE";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
newStatus = "TEMPORARILY_UNAVAILABLE";
break;
case LocationProvider.AVAILABLE:
newStatus = "AVAILABLE";
break;
}
String msg = provider + ": " + newStatus;
Logger.postMessageToTheUser(msg, mDelegate.get());
}
}
【问题讨论】:
标签: android gps location google-api-client