【问题标题】:Can not get accurate location while travelling in bus without internet在没有互联网的情况下乘坐公共汽车时无法获得准确的位置
【发布时间】:2017-11-06 12:13:57
【问题描述】:

我正在开发离线汽车跟踪器 android 应用程序。它将在 5 分钟后更新位置并将其存储在 SQLite 中。我使用了FusedLocationAPI,但在没有互联网的情况下乘坐公共汽车时无法获得准确的位置。我每 5 分钟获得 999m 的精度并获得相同的位置。 我将警报管理器设置为 5 分钟。

 public static void startAlarmManager(Context context) 
{
    preferences =context.getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
    int duration= Integer.parseInt(preferences.getString(Constant.DURATION_SHARED_PREF,Constant.CONSTANT_DURATION_SHARED_PREF));
    Log.d("duration",duration+"");



    alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    gpsTrackerIntent = new Intent(context, GpsTrackerAlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, gpsTrackerIntent, 0);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime(),
            5*60000,
            pendingIntent);
}

它将触发广播接收器。

 public class GpsTrackerAlarmReceiver extends WakefulBroadcastReceiver {

private static final String TAG = "GpsTrackerAlarmReceiver";


@Override
public void onReceive(Context context, Intent intent) {


    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );
    boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

   if(statusOfGPS) {

        context.startService(new Intent(context, LocationService.class));

    }
}



}

这是定位服务。我正在通过这种方式获取位置。

 public class LocationService extends Service implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {
private static final String TAG = "LocationService";
public static GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
public Context context;

private DatabaseHelper db;

private boolean currentlyProcessingLocation = false;



@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand");

    if (!currentlyProcessingLocation) {
        currentlyProcessingLocation = true;
        startTracking();
    }


    return START_NOT_STICKY;
}

private void startTracking() {


    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {

        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) {
            googleApiClient.connect();
        }
    } else {
        Log.e(TAG, "unable to connect to google play services.");
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    Log.d(TAG, "onConnected");



    locationRequest = LocationRequest.create();
    locationRequest.setInterval(1000); // milliseconds
    locationRequest.setFastestInterval(1000); // the fastest rate in milliseconds at which your app can handle location updates
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(
            googleApiClient, locationRequest, this);

}

@Override
public void onConnectionSuspended(int i) {
    Log.e(TAG, "GoogleApiClient connection has been suspend");
}

@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "onLocationChanged");

    startupdate(location);

}

private void startupdate(Location location) {

    if (location != null) {

        db=new DatabaseHelper(this);

        db.insertLocation(location.getLatitude(), location.getLongitude(), "FusedApi Provider",  location.getAccuracy());


        stopLocationUpdates();
        stopSelf();

    }
}

public void stopLocationUpdates() {
    if (googleApiClient != null && googleApiClient.isConnected()) {
        googleApiClient.disconnect();
    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    Log.e(TAG, "onConnectionFailed");

    stopLocationUpdates();
    stopSelf();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}



}

请帮助...如果我错了。提前致谢。

【问题讨论】:

  • 使用全球定位系统。坐在窗边。或者把你的手机放在窗外。
  • 感谢您的快速回复,离线获取位置难吗?

标签: android location android-gps fusedlocationproviderapi android-fusedlocation


【解决方案1】:

当 GPS 不实用时,您可以使用 LocationManager.NETWORK_PROVIDER,这是来自电话运营商的位置,它不如 GPS 准确,但在运营商有塔的任何地方都可以使用。我这样做的方法是设置一个标志 isGpsAvailable() 以查看是否为真我使用 GPS,否则我使用网络提供的位置。 This Google's doc 提供详细的解决方案,包括您可以使用的代码 sn-ps,并根据您的需要更改它的方式。

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多