in android, GPS_PROVIDER works, but NETWORK_PROVIDER does not - android

I am building an android application.
I can get GPS coordinates fine using
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
But when I try to use the network provider (editing the line, not doing two registers)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0,locationListener);
I get no coordinates.
I am checking if I have coordinates doing a println(location.toString()) inside the locationListener.
Both GPS and WIFI location are enabled on the device (and, as far as I can tell, other applications manage to use WIFI location)
Am I missing something? Is there a special procedure to use NETWORK_PROVIDER ?
(I have only "fine location" permissions, but the docs explicitly say this also allows "coarse location")

gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission android.permission.ACCESS_FINE_LOCATION.
network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.

Related

get location coordinates without GPS

My question is can you get location coordinates latitude and longitude using WIFI or any other source when location is off or disabled. Basically I want to get current location when the GPS is not enabled
If location permission isn't granted or location settings are off, no. That's on purpose- the user doesn't want to give you their location, respect that. Now if it's just GPS but not location that's off you can use coarse location. That uses wifi only, is less accurate, but can be used with less battery drain than GPS.

Get Location updates from GPS only, not from Network

I need accurate location updates. When using FusedLocationServices from GooglePlayServices, even if I set HighAccuracy, it will get locations from Wi-Fi and cellular when GPS is not available. How do I request locations ONLY from GPS, and with no updates coming in when GPS is not available?
Use LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ...)
this removed the need for GooglePlayServices.Location completely.

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.

What happens if Android device does not have GPS as a location provider

Given this code
if (!locator.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//consider forcing user to turn on gps here
} else {
provider = LocationManager.GPS_PROVIDER;
}
What happens if the device does not have GPS. Right now if that provider isn't enabled then it I will jump the user to their system settings to turn it on. But what if it isn't there? How would this be handled. I dont want to necessarily lock those kind of users out of the app.
If the GPS provider is not available (or enabled), then try for whatever you can get:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_HIGH);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = lm.getBestProvider(criteria, true);
This assumes that you prefer a rough position over no position at all.
You only need the GPS if you want precise locations. If the device simply does not have a GPS, then you can estimate with less accuracy through the NETWORK_PROVIDER. That's usually suitable if you just need the general location of a user (west side of a city, near a memorial, etc.). If you absolutely need a specific location that's within a specific accuracy for your app to work, then you're user without GPS is just out of luck.
Alternatively, if you enable GPS and the user doesn't have GPS, it simply won't get updates. In that case, you'll have to mention that the updates aren't precise because of lack of GPS, and the user will have to use a weaker version of your app.

Categories

Resources