Localization Is Returning Null. What Is Wrong With The Code?
double longitude = 0; double latitude = 0; try { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLoc
Solution 1:
There nothing wrong with your code, You are using GPS_PROVIDER which requires open sky field to fetch GPS Information. I suggest you to use NETWORK_PROVIDER if you want to fetch GPS details in side the room as well.
try
{
LocationManagerlm= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Locationlocation= lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Solution 2:
publicclassLocationHandlerextendsServiceimplementsLocationListener {
privatefinal Context mContext;
booleanisGPSEnabled=false;
booleanisNetworkEnabled=false;
booleancanGetLocation=false;
Location location; // locationdouble latitude; // latitudedouble longitude; // longitudeprivatestaticfinallongMIN_DISTANCE_CHANGE_FOR_UPDATES=0;
privatestaticfinallongMIN_TIME_BW_UPDATES=1000 * 1;
protected LocationManager locationManager;
publicLocationHandler(Context context) {
this.mContext = context;
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) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
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 Servicesif (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
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;
}
publicvoidstopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(LocationHandler.this);
}
}
publicdoublegetLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitudereturn latitude;
}
publicdoublegetLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitudereturn longitude;
}
publicbooleancanGetLocation() {
returnthis.canGetLocation;
}
publicvoidshowSettingsAlert() {
try {
AlertDialog.BuilderalertDialog=newAlertDialog.Builder(mContext);
alertDialog.setTitle("GPS is settings");
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
Intentintent=newIntent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
} catch (Exception e) {
Log.e("", e.getMessage());
}
}
@OverridepublicvoidonLocationChanged(Location location) {
if (location != null) {
this.latitude = location.getLatitude();
this.longitude = location.getLongitude();
Log.e("onLocationChanged", "YES");
}
}
@OverridepublicvoidonProviderDisabled(String provider) {
if (!TextUtils.isEmpty(provider)) {
if (provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER)
&& provider
.equalsIgnoreCase(LocationManager.NETWORK_PROVIDER)) {
showSettingsAlert();
}
}
}
@OverridepublicvoidonProviderEnabled(String provider) {
}
@OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
}
@Overridepublic IBinder onBind(Intent arg0) {
returnnull;
}
}
.....
LocationHandler gps = newLocationHandler(this);
if (gps.canGetLocation) {
Log.e(" Latitude", String.valueOf(gps.getLatitude()));
Log.e(" Longitude", String.valueOf(gps.getLongitude()));
}
.................Permissions
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" />
Post a Comment for "Localization Is Returning Null. What Is Wrong With The Code?"