是否有任何简单的方法,如何确定是否已获取 GPS 信号?我正在计算所需位置和我当前位置之间的距离,如果没有 GPS 修复,我想显示错误消息。问题是 - 如何简单地检查我是否有位置?
谢谢
像这样听修复:
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
}
});
这是我检查gps是否启用的方法。如果不是,用户会要求启用它。
private void checkGPS() {
try {
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
Log.d("GPS", "GPS koll misslyckades");
e.printStackTrace();
}
if (!isGPSEnabled) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.alert_start_gps_title))
.setMessage(getString(R.string.alert_start_gps_message))
.setCancelable(false)
.setPositiveButton("Aktivera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// Sent user to GPS settings screen
final ComponentName toLaunch = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
final Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 1);
dialog.dismiss();
return;
}
})
.setNegativeButton("Avbryt",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
finish();
return;
}
});
AlertDialog gpsAlert = builder.create();
gpsAlert.show();
}
}
玩得开心!