Android LocationManager.getLastKnownLocation() - android

From android documentation LocationManager.getLastKnownLocation():
Returns a Location indicating the data from the last known location
fix obtained from the given provider.
Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
How often provider updates device location ? How its works ?
The reason why I asking is that I dont want to use locationListener,
I just need to get current geo location on button click, and thats it.
Can I just do like this ?
final LocationManager mlocManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
final Location currentGeoLocation = mlocManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

How often provider updates device location ? How its works ?
It depends, and you may never know. If a device has good reception then updates about the user's location will fire rapidly.
Can I just do like this ?
Not really. If that is firing too soon then you will not get any location back. If the user doesn't have great reception then it could take 10 seconds before you have a reasonable idea of the user's location. I think you'll have to approach this slightly differently. I'm afraid I must recommend locationListeners :P

if you absolutely can't wait for a location you can try getLastKnownLocation(), but you need to be prepared to have a NULL result.
getLastKnownLocation() is just a cache of the last location from the provider you specified. In some cases there won't be one at all, like when the phone just rebooted. Even when there is a cached location it may not be accurate.

Related

Is Google`s FusedLocationAPI always reliable ? (mock location , spoofing , etc .. )

There are a number of workarounds for detecting the possibility of a fake location being returned by the LocationManager API. those include checking if the mock location setting is enabled and checking the list of installed apps for the ones using the mock location permission. if any of the above happens we could go with removing the TestProvider from the current LocationManager instance and getting a location again, just in case the api returns bogus results. however the above approach wont work with the FusedLocationAPI. is it always granted to return the real location ? or should i consider changing my code to use the LocationManager ?
because of the nature of product i'm working on, it is necessary to be sure of the location.
thanks in advance

First android app last location returns null after sending coordinates through eclipse emulator control

All I am trying to do is a real simple app that gets the location that I send it via eclipse and displays it. Location keeps returning null after lines below.
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Any help would be appreciated. As of right now, i don't care about updating locations yet.
This is my first app, be gentle.
Thanks
Generally this means that the GPS hasn't found a fix yet, since you're asking for a location immediately (It's actually returning null BEFORE you're sending co-ordinates through).
You need to make a Location Listener, which will listen for changes in the state of your GPS.
Here's an example.

How to get current location on android

Hey guys
i want to get the latitude and longitude of the users location.
I have tried developers site and stack overflow , there are loads of examples targeted on the moving user which use some thing like :-
onLocationChanged()
I dont need updates of the location , the user will just open it for a minute or two
so i need to find the location the time the application starts and what if the user has never used any geolocation app before hence i would also like an alternative for
LocationManager.getLastKnownLocation()
My code is :-
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(currentLocation.this, "Latitude -> "+Double.toString(loc.getLatitude())
+" Longitude is -> "+Double.toString(loc.getLongitude()), Toast.LENGTH_LONG).show();
and is working fine.
But i am scared that the getLastKnownLocation may return null when used for first time.
Thanks
Sign up for location updates and once you receive the first update, you can unregister for future updates.
In other words, create a LocationListener and register it with your LocationManager's requestLocationUpdates method. In the onLocationChanged method of your LocationListener, call the removeUpdates method on your LocationManager, with this as parameter.
It might take some time before you get a new location, but except for using getLastKnownLocation there's not really an alternative.

android location update problem

I have an activity that needs to display an alert to user when location is updated,
but i need first to get current location (update last known location) and then start a location update listener to display alert in this activity.
Basically what i need is to display an alert if location changed from time activity was created.
If i use getLastKnownLocation when activity is created, it might be outdated, then the requestLocationUpdate listener is called a few seconds from activity creation time.
How can i do this?
Thanks in advance,
Getting last known location (quickly)
LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
from ( http://developer.android.com/guide/topics/location/obtaining-user-location.html#FastFix )
Listen for location updates: http://developer.android.com/guide/topics/location/obtaining-user-location.html#Updates
This is the functionality you can use. But know that last location might be pretty old and inaccurate. Depends on when a application was trying to get the location the last time. But it normally should be somewhere in the area you are.
If that is not good enough, than you will have to do something like this:
start listening to updates
save the answers for the first 1-3 times or so. (didn't test what would be best. maybe even higher). or maybe for 1 minute or 5 minutes (gps can take a long time if you need the location exactly)
after that you do not save the location updates anymore but check if it moved.
I hope this helps.
Check Blow Link..
May be it's help you...
In that u have to pass sach argument so that Location manager can easily Update Your Location and your code is also right to get Location ....
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29

Search for Best Provider in Android Application

I am trying to search for best provider with this case below:
// GPS
case R.id.main_menu_gps:
// Set up location services
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mBestProvider = mLocationManager.getBestProvider(new Criteria(), true);
Log.d(DEB_TAG, "####Value of mBestProvider is " + mBestProvider);
if(mBestProvider != null){
showGpsSearchingDialog();
mLocationManager.requestLocationUpdates(mBestProvider, 150000, 1, this);
} else {
Log.d(DEB_TAG, "Provider is null");
showGpsAlertDialog();
}
break;
My device is returning "GPS" as the best provider but is not able to find a location and my progress dialogue is displayed forever searching. If I go into the phone settings of "Location/Security" and check the "Use wireless networks" the best provider is Network and it works to return a location.
Am I doing something wrong when the best provider is GPS and no data is returned?
You may take a look at my strategy to choose best provider What is the simplest and most robust way to get the user's current location on Android?
If GPS is enabled it will always return GPS as the best provider since you specified an empty criteria. It can take a decently delay for a GPS fix and if you are in a building you may never get a fix at all. So if the network location will be acceptable then you should just do network. Or have a timeout so that at some point you stop waiting on gps and then switch to network instead. If you are using a MapView, you may want to use the MyLocationOverlay because it's internal logic will handle that for you.
Your code looks fine btw.
You aren't really doing anything wrong, there are just certain techniques for gaining the location via GPS. The main issue is that the GPS location may be the best available provider (in terms of accuracy) however it may not be able to obtain the current location do to network/structural obstruction. Without logic in place to determine the amount of time that has gone by while the provider has attempted to attain the location unsuccessfully or without the accuracy required; you may never get the location via GPS and will have to use a different provider or the last known location as a fall-back.

Categories

Resources