【发布时间】:2015-06-11 04:29:04
【问题描述】:
我曾尝试使用在我的应用程序中在线找到的 GPSTracker 类,并且我之前曾让它工作,但现在似乎莫名其妙地不起作用。
public class GPSTracker extends Service implements LocationListener {
private static final String TAG = "GPSTracker";
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location = null; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 30 * 1; //
// Declaring a Location Manager
protected LocationManager locationManager;
TextView tv;
public GPSTracker(Context c){
this.mContext = c;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// 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("GPS", "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;
}
/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
在我的 MainActivity 中,我初始化了一个 GPSTracker gps 变量,并在我的 onCreate() 方法中创建了一个新实例:
gps = new GPSTracker(MapActivity.this);
现在使用它的代码是:
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng point = new LatLng(gps.getLatitude(),gps.getLongitude());
pointList.add(point);
googleMap.addMarker(new MarkerOptions().position(point).title("Home"));
}
不幸的是,尽管我的手机启用了 Wi-Fi 和 GPS,但我的标记始终出现在 (0,0)。我能想到的唯一改变的是电池,但我认为它不会受到> 20%的影响。使用 Toasts 表明 isGPSEnabled 出现为真,而 isNetworkEnabled 出现为假(不知道为什么会出现这种情况,因为 Wi-Fi 工作正常)。
我有几个相关的问题也想帮助我理解整个机制。当调用 locationManager.requestLocationUpdates() 时,这些更新是否继续在后台发生?例如,当我在 MapActivity 中创建 GPSTracker 类的实例时,是否每次想要获取当前位置时都需要创建该类的另一个实例?保存在变量“gps”中的对象实例中的纬度和经度值是否会发生变化?如果是这种情况,我可以在每次移动 10 米时调用 gps.getLatitude(),它会自动更改纬度和经度的值。或者,由于 GPSTracker 实现了 LocationListener,我是否需要在 onLocationChanged() 方法中添加代码来请求位置更新?我对整个情况有点困惑,所以任何帮助将不胜感激。谢谢。
【问题讨论】:
-
重启我的手机后,它现在可以工作了,谁知道为什么。但是,对我的问题的任何解释将不胜感激。