【发布时间】:2014-03-13 06:44:24
【问题描述】:
我正在使用以下代码来获取我的当前位置。但我面临的问题是,在 Gps 的情况下,纬度和经度总是返回 0.0。网络提供商工作正常。 .我已经打开手机中的GPS设置并设置了所有权限。但我仍然面临这个问题。
*/Java 代码/*
public class GpsLocationTracker extends Service implements LocationListener
{
private Context mContext;
private boolean isGpsEnabled = false;
private boolean isNetworkEnabled = false;
private boolean canGetLocation = false;
private Location mLocation;
private double mLatitude;
private double mLongitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 60000;
private LocationManager mLocationManager;
/**
* mContext constructor of the class
*/
public GpsLocationTracker(Context mContext)
{
this.mContext = mContext;
getLocation();
}
/**
* method to finding the 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*/
System.out.println("Gps and Network not 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);
System.out.println("location gps"+mLocation);
if (mLocation != null)
{
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
System.out.println("Latitude gps"+mLatitude);
System.out.println("longitude gps"+mLongitude);
}
}
}
}
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
/**
* call this function to stop using gps in our 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 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 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
}
【问题讨论】:
-
你是用真机测试吗?
-
您是否在清单中授予权限?
-
是的。我使用真实设备进行了测试,并且还在清单中授予了权限。在网络提供商中它工作正常
-
@user3228311,好的,这说明你的代码没问题,你肯定是在房间里测试,我建议你到外面去,在开阔的天空下或靠近窗户的地方。因为 GPS_PROVIDER 需要开阔的天空场。 :)
-
好的。现在我在房间里测试。房间里有可能得到latlong吗?
标签: java android google-maps gps