【问题标题】:GPS location never changesGPS位置永远不会改变
【发布时间】:2013-12-04 12:36:52
【问题描述】:

我正在编写一个应用程序,每 10 秒获取一次我的坐标并发送到服务器。 我有一项服务,每 10 秒(通过 AlarmManager 实现)获取当前 GPS 坐标。 但它总是只显示第一个获取坐标,为什么?

public class GpsService extends Service implements LocationListener {
// flag for GPS status
boolean isGPSEnabled = false;


// flag for GPS status
boolean canGetLocation = false;

// Declaring a Location Manager
private LocationManager locationManager;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 1 minute

@Override
public void onCreate() {
    Log.i("myLogs", "onCreate");
    super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
    Log.i("myLogs", "onBind");
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("myLogs", "onStartCommand");
    getLocation();
    if(location != null) {
        Log.i("myLogs", "lat = " + Double.toString(location.getLatitude()) + "lng = " + Double.toString(location.getLongitude()));
    }
    else
        Log.i("myLogs", "no location for your today");
    return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
    Log.i("myLogs", "onDestroy");
    super.onDestroy();
}
public Location getLocation() {
    try {
        locationManager = (LocationManager) this
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!isGPSEnabled) {
            stopSelf();
        } else {
            this.canGetLocation = true;
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("myLogs", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

}

【问题讨论】:

标签: android gps


【解决方案1】:

如果这段代码是第一次执行,location 就是null 所以它会分配最新的位置

在第二遍时,它不为空,因此它会跳过整个部分,并且永远不会更新 locationlatitudelongitude

if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("myLogs", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }

【讨论】:

  • 如何获取新位置?
  • 删除if (location == null) 检查。我不确定这是否会让您在调用locationManager.requestLocationUpdates() 时遇到麻烦,因此您可能必须找到另一种方法来确保 that 函数只被调用一次。
【解决方案2】:

为确保您获得最新且准确的位置,您需要实现位置侦听器方法。您还应该注册一个广播接收器来监听位置的变化。 Here 是一篇博文,详细解释了这一切。它监听位置变化并根据准确性和新鲜度更新位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多