我想获取我的当前位置,但是有时我的GPS无法获取位置并在Google地图中显示灰点。当我的GPS工作不正常时,我想从该灰色点获取经度和纬度。我如何获得该坐标?
我使用了此解决方案Get user's current location using GPS,但是坐标仍然为空,但是如果当我的Google地图显示蓝点时GPS正常工作,则可以很好地工作
public class GpsLocationTracker extends Service implements LocationListener
/**
* context of calling class
*/
private Context mContext;
/**
* flag for gps status
*/
private boolean isGpsEnabled = false;
/**
* flag for network status
*/
private boolean isNetworkEnabled = false;
/**
* flag for gps
*/
private boolean canGetLocation = false;
/**
* location
*/
private Location mLocation;
/**
* latitude
*/
private double mLatitude;
/**
* longitude
*/
private double mLongitude;
/**
* min distance change to get location update
*/
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
/**
* min time for location update
* 60000 = 1min
*/
private static final long MIN_TIME_FOR_UPDATE = 60000;
/**
* location manager
*/
private LocationManager mLocationManager;
/**
* @param mContext constructor of the class
*/
public GpsLocationTracker(Context mContext) {
this.mContext = mContext;
getLocation();
}
/**
* @return location
*/
public Location getLocation() {
try {
mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
/*getting status of the gps*/
isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
/*getting status of network provider*/
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGpsEnabled && !isNetworkEnabled) {
/*no location provider enabled*/
} else {
this.canGetLocation = true;
/*getting location from network provider*/
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
}
}
/*if gps is enabled then get location using gps*/
if (isGpsEnabled) {
if (mLocation == null) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
/**
* call this function to stop using gps in your application
*/
public void stopUsingGps() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(GpsLocationTracker.this);
}
}
/**
* @return latitude
* <p/>
* function to get latitude
*/
public double getLatitude() {
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
}
return mLatitude;
}
/**
* @return longitude
* function to get longitude
*/
public double getLongitude() {
if (mLocation != null) {
mLongitude = mLocation.getLongitude();
}
return mLongitude;
}
/**
* @return to check gps or wifi is enabled or not
*/
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* function to prompt user to open
* settings to enable gps
*/
public void showSettingsAlert() {
AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(new ContextThemeWrapper(mContext, R.style.AppTheme));
mAlertDialog.setTitle("Gps Disabled");
mAlertDialog.setMessage("gps is not enabled . do you want to enable ?");
mAlertDialog.setPositiveButton("settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent mIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(mIntent);
}
});
mAlertDialog.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
final AlertDialog mcreateDialog = mAlertDialog.create();
mcreateDialog.show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
然后我在MyActivity中使用该类
Double sLatitude, sLongitude;
GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(CheckInOutDetailActivity.this);
/**
* Set GPS Location fetched address
*/
if (mGpsLocationTracker.canGetLocation())
{
sLatitude = mGpsLocationTracker.getLatitude(); //0.0 if gmaps grey;
sLongitude = mGpsLocationTracker.getLongitude(); //0.0 if gmaps grey
}
else
{
mGpsLocationTracker.showSettingsAlert();
}
我也使用FusedLocation,但如果我的gmap是灰点,仍然会为空
public void capturePhoto()\\{
FusedLocationProviderClient client;
String sLatitude, sLongitude;
client = LocationServices.getFusedLocationProviderClient(this);
client.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
sLatitude = String.valueOf(location.getLatitude());
sLongitude = String.valueOf(location.getLongitude());
}
else
{
GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(CheckInOutDetailActivity.this);
/**
* Set GPS Location fetched address
*/
if (mGpsLocationTracker.canGetLocation())
{
sLatitude = mGpsLocationTracker.getLatitude()+"";
sLongitude = mGpsLocationTracker.getLongitude()+"";
}
else
{
mGpsLocationTracker.showSettingsAlert();
}
}
}
});
}