Iam using
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(bestProvider, 0, 0, this);
location = locationManager.getLastKnownLocation(bestProvider);
but first time it is not giving location object.Always giving me null.I read in developer.android.com ,
"Cached Location is dismissed if it is
too old"
But second time it is giving location updates.How to solve my problem.
Regards,
Android Developer
There are few options... if getLastKnownLocation() is null because it's to old you only can wait to get a newer position fix.
However if you don't need a very accurate location you can use the networkProvider which is faster.
mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
CellLocation.requestLocationUpdate();
Call these all statement together..
and there is no way to simply seek device LAT and LONG statically ..
You have to wait for update..... Use any progress bar or do any other waiting stuff...
In the onLocationChanged method .. recieve the location object and save its reference to any of your class...
Related
in my application i have to update specific data on travelling of each 10 meters.
For that i m using LocationManager to get updates , but i am not getting correct answers.
I have used following code : Also added Permissions coarse, fine , internet .
locationManager.requestLocationUpdates(getProviderName(), 10000,
10, locationListener);
private String getProviderName() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
// criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(true);
criteria.setBearingRequired(true);
criteria.setCostAllowed(false);
criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);
criteria.setSpeedAccuracy(Criteria.ACCURACY_MEDIUM);
criteria.setBearingAccuracy(Criteria.ACCURACY_MEDIUM);
return locationManager.getBestProvider(criteria, true);
}
Is this condition says LocationManager to update on every 10 seconds for 10 meters covered or not? am i right?
I have some problems with the location API. In some devices, sometimes it won`t give the location with any provider, though rebooting the phone solves the problem. On the other hand, when not getting the location, Google Maps places you in the map instantly with no problems.
Here is the code:
Intent intent = new Intent(Constants.LOCATION_BROADCAST);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_HIGH);
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO){
criteria.setAccuracy(Criteria.ACCURACY_LOW);
locationManager.requestSingleUpdate(locationManager.getBestProvider(criteria, true), pendingIntent);
}
else {
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 0, 0, pendingIntent);
}
Any thoughts?
Try using all the location providers and see if it solves your problem, i.e. something like
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> allproviders = lm.getAllProviders();
for(String provider : allproviders) {
myLocationManager.requestLocationUpdates(provider, 5000, 0, this);
}
If this code always works, then you'll just need to work out why your code is ignoring a provider that my code finds. Could easily be something to do with your power or accuracy criteia.
My app has been ported from Android to blackberry and it works fine...
the only thing not working is the GPS location which works on the android....
the app displays a message to the using that we are trying to find the location and if no location is found we tell the user that we could not retrieve a location..
here is the code
private void gpsLocation()
{
//Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Provide Criteria
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if(location != null){
onLocationChanged(location);
} else{
Log.i(TAG, "no GPS location available - waiting for GPS fix");
Toast.makeText(getApplicationContext(), R.string.gpsWaitingToast, Toast.LENGTH_SHORT).show();
if (Debug.GPS_USE_MONTREAL) {
currentLatitude = Double.valueOf(Debug.GPS_LAT_MONTREAL); currentLongitude = Double.valueOf(Debug.GPS_LNG_MONTREAL); // montreal
}
waitForGpsFix(GPSFIX_DELAY);
}
its not working,, I restarted the app ,, did it next to a window and all that stuff..
nothing works..
your advise and help is greatly appreciated.
Regards
There was previously a bug where LocationManager.NETWORK_PROVIDER returned null, which may still be returned by the locationManager.getBestProvider() method. This should be fixed in the newer OS builds, however.
If you'd like to fix your code immediately, all you would need to do would be to set:
provider = LocationManager.GPS_PROVIDER
as in the early stages, GPS_PROVIDER was the only provider that was implemented in the player.
I'm trying to get the location of the device, but the code
Criteria criteria = new Criteria();
String provider = myLocationManager.getBestProvider(criteria, false);
myLocationManager.requestLocationUpdates(provider, 0, 0,
myLocationListener);
Location lastLocation = myLocationManager.getLastKnownLocation(provider);
"lastLocation" returns null. if restart the device, the same null is obtained. is there any other standard way to obtain latitude and longitude from the device?
You will receive your location asynchonously in your myLocationListener. After that (when listener is invoked) gatLastKnownLocation will return something not null.
Here is a complete example of how it should be implemented.
I would like to know how can I get location from the best provider
do I have to make two separate criteria 1 for the GPS and 1 for the network or is there a way to put them all together ?
this is my code when I add the COARSE criteria the GPS does not go on (no GPS flashing logo on the top of the screen) and when I use the FINE criteria I dont get any thing from the network.......so do I have to write criteria for both and switch between them for what ever is available or can they both be in the same criteria ?
because I have the "getBestProvider(criteria, true);" in my code so it should get the location from the best provider....right..??!
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
Using a getBestProvider with Criteria.ACCURACY_FINE will never return the NETWORK_PROVIDER, even though wifi position is usually quite accurate.
In my application, I use getProviders to get all providers matching my criteria, then add the NETWORK_PROVIDER if it is activated and not yet in the list.
Then I launch a requestLocationUpdates for each of these providers, making sure to call removeUpdates when I get a location accurate enough.
This way, if the location provided by the network is accurate enough, the gps provider will be turned off
#nicopico getBestProvider with Criteria.ACCURACY_FINE can return the NETWORK_PROVIDER if gps is not enabled
i use the following code in my app
public void getLocation() {
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
// boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
Log.e(TAG, "getLocation fired Google Play Services are not available");
mHandler.post(new UiToastCommunicaton(context.getApplicationContext(),
context.getResources().getString(R.string.gpserv_notfound)));
}
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on
Log.e(TAG, "network location provider not enabled");
} else {
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Check provider exists then request for location
if (provider != null) {
requestLocation(locationManager, provider);
} else {
//start wifi, gps, or tell user to do so
}
}
}
public void requestLocation(LocationManager locationManager, String provider) {
Log.e(TAG, "requestLocation fired getting location now");
if (provider != null) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
//locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper());
} else {
//tell user what has happened
}
}
this code is in a class that implements locationListener
you can edit this line
locationManager.requestLocationUpdates(provider, 0, 0, this);
to reflect your implementation of locationListener