Android Location Manager - android

There are two providers: GPS_PROVIDER and NETWORK_PROVIDER to get our current location in Android.
Is it possible to disable location service and get the user location purely by WIFI?
I tried to disable the location service but the NETWORK_PROVIDER return false.

Is it possible to disable location service and get the user location
purely by WIFI?
Yes it is possible by adding input to getLocastKnowsLocation as LocationManager.NETWORK_PROVIDER
LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location networkLocation = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLocation != null) {
Location bestResult = networkLocation;
}

Yes, you can still get the current location of the device thru wifi. The location data available to an Android device includes the current location of the device. You can use the My Location layer and the My Location button to provide your user with their current position on the map.
One of the unique features of mobile applications is location awareness. The Location API's available in Google services facilitate adding location awareness to your app with automated location tracking, geofencing and activity recognition.
Here's a sample demo app which discuss how to get current location of the device:
https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/MyLocationDemoActivity.java

Related

Android Fused Location Api

I am using Fused Location Api for getting the latitude and longitude.It's mentioned in the documents that it uses GPS,Wifi and network to return the most accurate location of the user.I want to ask is that will it return the position if the GPS in my phone is switched off?
Fused Location Provider will automatically choose the best location from the options available. It will select from GPS or Network location Provider. So if the device GPS is not on, you should still be able to get a location based on the network location provider, if that is enabled on the device.
You will need to have permissions for ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION to receive both kinds of locations.

How do I know the provider pf LocationManager's GPS retrieval?

I am trying to log information about how the app is fetching the coordinates. This is the code I used to grab the coordinates.
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
My question is, how do I find out the source of the coordinates? (GPS, Network Triangulation, Wifi, etc. )
In other words, getLastKnownLocation returns coordinates from multiple sources, how do I get a string name of the source?
You use NETWORK_PROVIDER. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup.
GPS_PROVIDER determines location using satellites.

do I need to enable gps when using NETWORK_PROVIDER

I've written an android application that needs the user location, it was working fine when I used gps provider, but I don't want to use GPS because it uses a lot of battery, here is my code
LocationManager locaionManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5*1000,0, new MyLocaionListener());
Location loco = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("enabled=" ,"" + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
This code returns a null Location Object, and the logCat shows enabled=false when the location service is disabled although I'm using NETWORK_PROVIDER But when I enable the location service from the phone settings I get enabled=true and I get my current location.
So my question is:
does network provider also uses the phone GPS? and how do GPS and NETWORK providers internally works.

Android get location from GPS

My situation is like that the user has their tablet with them all day and its gps or location can be turned off whole time to save battery, and then wanna post something using my application which requires them to turn on the location and then upon posting I wanna get the latitude and longitude of the device. Using LocationManager when I get the location its null. Any help would be appreciated.
I'm using the below code to get the location:
LocationManager lm = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
As I've checked around, it seems I have to put a listenere to update the device location so when I get the last known location it wouldn't be null, but how can I do that if the device's location is off before using the application? I wanna get the location where the user is posting using the application.
EDIT: I've changed my code to below, but loc is always null and loc2 always returns location. why is that?
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location loc2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
and this is the listener:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
You might want to consider using Network Location as well as GPS Location, as many users just keep the GPS radio off to conserve battery.
With Location Services set in "Battery Saving" mode, the device can get accurate geo-location data using Wi-Fi if available.
If you use Network Location data as well as GPS radio data, you would have a much better chance of getting location data when you need it.
However, if Location Services were disabled completely prior to being turned on in order to post something using your app, then you would need to register a Location Listener to get the first instance of location data.
Take a look at this post to see how to use both Network and GPS Location:
Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location
GPS rarely works indoors. If your users are "visiting shops" then it makes sense the GPS provider will never return a valid location as there is no GPS signal available indoors.
The network provider uses both Wifi and cell towers, so as long as they at least have cell service, you should return a location. That explains why you are always able to retrieve a location update with the network provider.
I would recommend you look into the Fused Provider which is the newest Location API released by Google. This API will automatically handle switching between GPS, network, and passive location listeners for you based on both what settings the user has enabled and which provider gives the best data given the users situation.

How to get the current Location mode of an android phone?

I've taken a look at this page: http://developer.android.com/reference/android/provider/Settings.Secure.html#LOCATION_MODE , but I'm still lost.
I know that the Location mode should return an integer. Battery Saving would be 2, High Accuracy would be 3 and if GPS is off then it should return 0.
I have no problem grabbing the current location...
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String stlongitude = Double.toString(longitude);
String stlatitude = Double.toString(latitude);
But when the phone's location mode is set to Device Only then I have to use LocationManager's GPS_PROVIDER. Which is not as accurate as the NETWORK_PROVIDER. I would like to do an if, else or a case statement that allows me to get the current location mode and if it's set to High Accuracy or Battery saving, use the NETWORK_PROVIDER but if it's set to Device Only use GPS_PROVIDER.
What you are trying to do with Settings.Secure and LOCATION_MODE is the right way to get the enabled providers if you are only interested in targeting devices that are KitKat or above (API 19). But, more than likely, you actually want your app to work with devices that have earlier versions of Android.
Here's code that gets the last location using the least power-hungry provider that the user has enabled:
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGpsEnabled && !isNetworkEnabled) {
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else {
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
NETWORK_PROVIDER is not as accurate as GPS,
The first thing to note is that NETWORK_PROVIDER is not as accurate as GPS, according to the developer docs, network prodiver uses
NETWORK_PROVIDER
This provider determines location based on availability of cell tower
and WiFi access points. Results are retrieved by means of a network
lookup.
The most accurate provider you can use when retrieving the users, location is the GPS, which is defined by the Andorid Developer docs as:
GPS_PROVIDER
This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.
Manipulating the provider
With that in mind, the following hints will enable you to manoeuvre the logic any way you wish:
1) To set the location manager to recieve updates using best provider the device is capable of using, you can call
/* Getting the name of the BEST provider available */
provider = locationManager.getBestProvider(criteria, true);
/* Getting Current Location using the best provider available */
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 300000, 0, this);
2) If you which to force network provider, set your location manager to ask for network provider update like this:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
3) Similarly, if you which to force GPS use:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
I think if you are looking for LOCATION_MODE, not a provider, you should check out this question and response: Change Location Mode to High Accuracy Programmatically Android
Because even if all your providers are enabled, the location mode could mean that it would fail to get a good location if you are in a building and the mode is LOCATION_MODE_SENSORS_ONLY.

Categories

Resources