I am using this code to get the location provider and location.
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
mBestProvider = mLocationManager.getBestProvider(criteria, false);
mLocation = mLocationManager.getLastKnownLocation(mBestProvider);
If I turn off the GPS, the location is by network, but if I reboot the phone(so I loss the last position known) and with the 3G data connections off. So I am only using WIFI, I cant get any provider thus therefore any location. However google places app can locate me. I think it might be getting the lastknownlocation.But in that case my others applications should be able to get that location. Any idea whats happening?
The way you are calling this it will return all providers enabled or not because you are passing it false which is probably your intention but have you checked the return string?
mBestProvider = mLocationManager.getBestProvider(criteria, false);
You might be getting the gps provider, or you might be getting the network provider, I have learned not to trust the criteria mechanism because it seems to work differently per carrier and device (I have had some weird bugs reported because of this)
So I always ask specifically for gps and network providers and check last known for both, then use an algo to determine the best one to use.
The network provider can use cell or wifi hotspot/routers to determine location (google keeps a database of wifi information) so it's possible to get a fix with just wifi, not saying that is whats happening but it could be.
If that bears no fruit then it's possible that they are simply caching the last location update in preferences, some applications do that. To test the thesis, failing all of the above just leave the phone in that state and move to a very different location with the same properties if possible, should only take 2000 meters or so. If your app still reports null and places reports the old location you have your answer.
If places did report the newer location with wifi, and your app cannot (assuming you actually verify you are getting the network provider) then there is a chance they are using a private API via the Google Location Server (GLS) / MASF server via partial cell / wifi info but that's at the extreme end of the tin foil curve.
Related
I have an Android IoT device running Android 5.1.1 on Rockchip RK3288 that as name implies can connect to a WiFi network but doesn't have a GPS or a Cellular module. Whenever I try to access location and find the best location provider it always return me as a 'Passive' provider and hence i have no way of getting my current location.
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
I am a bit perplexed that the device has an active network connection but NETWORK_PROVIDER isn't available.
Try Google Geolocation API.
Google Geolocation API use wifiAccessPoints and cellTowers too. As you said device don't have GPS and Cellular module you need to use WifiAccessPoints for it.
For WifiAccessPoints you need to pass all your nearest wifi connection data. For E.g In android when click on WIFI it shows list of wifi near to you. So, you need to just pass all that wifi list to WifiAccessPoints so it returns the location of your device.
You can Pass Parameter "considerIp": "true" so using your Ip Address it get the current location of your device.
For My Opinion use Get Current location through IP Address which return the Highest Accuracy ratio for current location.This is also said by the Google Maps Geolocation API document.
For more information you can refer Google Geolocation API which also provide sample data so that you can check the result from here.
I hope this will give you some hint or get some idea to actually you want to achieve.
Some resources say getLastKnownLocation() merely gives a location of some previous app's Location Change Listener.
But one thing I feel is missing from the conversation -- if the phone has GPS enabled, isn't this GPS tracking/updating as the phone moves? So if I call getLastKnownLocation(), isn't it getting the current GPS from the phone's constantly-updated GPS?
If so, then why do people warn against using it / accuse it of potentially getting a "stale" location? If the GPS is being tracked / updated, and getLastKnownLocation() makes a one-time grab of it's current position, what makes getLastKnownLocation() bad?
What am I mistaken about the Locations service or GPS?
The documentation says: Returns a Location indicating the data from the last known location fix obtained from the given provider.
Also, about the "out of date location": This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location. ("This" refers to getting the last known location).
Also, taken from LocationProvider docs:
Each provider has a set of criteria under which it may be used; for example, some providers require GPS hardware and visibility to a number of satellites; others require the use of the cellular radio, or access to a specific carrier's network, or to the internet. They may also have different battery consumption characteristics or monetary costs to the user.
if the phone has GPS enabled, isn't this GPS tracking/updating as the phone moves?
Well, it is, but not constantly. This has to do with battery consumption, if the phone was constantly updating the GPS location, the battery life would suffer a lot. The GPS location is usually updated when some app requests it. But even that request is not guaranteed to succeed. For example, suppose your going into a tunnel. Some application requests to update the GPS coordinates right before entering the tunnel and it succeeds. Now, you've entered the tunnel, which is 5km long. Most probably your device won't be able to get a GPS fix from the tunnel, so for the next 5km (at least) getLastKnownLocation() will return an outdated value, since the devices last known location was at the entrance of the tunnel.
What you could do is explicitly request to update the GPS location, that however might take some time and there are no guarantees that it will succeed.
You have the assumption "if the phone has GPS enabled, isn't this GPS tracking/updating as the phone moves". This assumption is incorrect. The GPS functionality takes up a lot of battery life so it should be used sparingly and almost certainly not all the time.
I've developed an app that receives the location from both GPS_PROVIDER and NETWORK_PROVIDER, however the NETWORK_PROVIDER returns the best values retrieved from WIFI and GPRS, without having any control over it. I need to get the value returned by the GPRS location listener even when you have WIFI active, so I can use it to dismiss the fake locations from other apps.
Is it possible to do this?
At the moment I'm testing this solution Disable / Check for Mock Location (prevent gps spoofing) , I'll let you know if it solves my issue
Unfortunately it is not possible, no.
You can do something like this:
final LocationManager locationManager = getLocationManager();
final List<String> providers = locationManager.getProviders(true);
if (providers.size() > 1) {
providers.remove(LocationManager.NETWORK_PROVIDER);
}
But you don't have the option to distinguish between a cell or a wifi 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.
Constant Value: "network"
Source: http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER
What you can try is to use this solution. However, since this doesn't seem to be documented I'd be careful relying on it because it might break in future OS versions or might not work on every device.
I have re-read the whole documentation and as I understand, this API intelligently fuses the providers to give you an accurate result.
The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs.
To my understanding, it will use Network and WiFi and GPS to give you a location. Now it is only logical to assume if one of those 3 is unavailable, it will use the remaining 2.
My problem is I'm testing my app to get Location when both WiFi and GPS are off. That will leave Location Service with the network provider only. But I'm getting Latitude and Longitude = 0.0
Long story short, either way I'm wrong in assuming that it will work relying only on network (no GPS, no WiFi) or there is something else I don't know or haven't read yet. Which of this 2 is my issue?
PS: my LocationRequest configuration is set to setInterval(30000) to setFastestInterval(60000) (30 and 60 seconds respectively, for each update.
I'm developing a android app and I want to receive coordinates from a smartphone. Two questions:
1: GPS is enabled, but there is no signal, so I have no coordinates. How can I check, if GPS is sending coordinates or not?
2: If GPS is enabled and there is no signal, which other method can I use to get the coordinates from the smartphone?
Getting coordinates requires using the location services, which by default uses all manner to determine location, not just the GPS, but cellular signals as well. Here is a tutorial on using the location services in Android.
One word of advice - if you are going to use GPS and generally distribute your app, know that the GPS service is VERY battery intensive. It requires judicious coding to not drain your users battery. You have been warned!
1) fetch device coordinates using locationService.
mContext = getActivity();
mLocationManager = (LocationManager) mContext.getSystemService(Application.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(mProvider, 60000, 100, mAttLocationListener);
boolean gpsEnabled = mLocationManager.isProviderEnabled(mProvider);
2) You can still get rough estimates using location information stashed in wi-fi