我有这个代码用于在我的应用中接收用户当前位置的LatLng 它大部分时间工作正常,直到我打开gps,它停止工作然后进入代码的一部分,说没有提供程序启用并重定向到一个位置,我尝试了很多修复,这是我迄今为止最好的,但它仍然非常糟糕,我无法找到解决办法。
public static LatLng getCurrentLocation(final Context context)
{
LocationManager locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String locProvider = locMgr.getBestProvider(criteria, false);
Location location = locMgr.getLastKnownLocation(locProvider);
if(location != null) return new LatLng(location.getLatitude(), location.getLongitude());
boolean isGPSEnabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNWEnabled = locMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Criteria criteria2 = new Criteria();
criteria2.setAccuracy(Criteria.ACCURACY_FINE);
criteria2.setCostAllowed(true);
if (locMgr != null) {
location = locMgr.getLastKnownLocation(locMgr.getBestProvider(criteria2, true));
if(location == null) {
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
location = locMgr.getLastKnownLocation(locMgr.getBestProvider(criteria2, true));
if(location != null) return new LatLng(location.getLatitude(), location.getLongitude());
} else
return new LatLng(location.getLatitude(), location.getLongitude());
}
if(isNWEnabled) {
if (locMgr != null) {
location = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null) return new LatLng(location.getLatitude(), location.getLongitude());}
}
if (isGPSEnabled) {
if (locMgr != null) {
location = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) return new LatLng(location.getLatitude(), location.getLongitude()); }
}
if (location == null){
Toast.makeText(context, "No location provider activated, using saved location", Toast.LENGTH_LONG).show();
SharedPreferences settings = context.getSharedPreferences("MyApp_Settings", 0x0);
String uLa = settings.getString("LATITUDE", "");
String uLo = settings.getString("LONGITUDE", "");
double lat = 0 ;
double lng = 0 ;
if (!(uLa.equals(""))) {
lat = Double.parseDouble(uLa);
lng = Double.parseDouble(uLo);
return new LatLng(lat, lng);
} else {
AlertDialog.Builder builder2 = new AlertDialog.Builder(context);
builder2.setTitle("Location");
builder2.setMessage("No Location Services Enabled, Do You Want to Open Location Settings?");
builder2.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder2.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder2.show();
Toast.makeText(context, "Using Random Location", Toast.LENGTH_LONG).show();
return new LatLng(34, -94);
}
}
/* if(location == null) {
Toast.makeText(context, "Using Random Location", Toast.LENGTH_LONG).show();
return new LatLng(34, -94);
} */
return new LatLng(location.getLatitude(), location.getLongitude());
}
我正在寻找改进此代码并修复gps提供程序的建议