Can't get any location on Android device, while Google Maps can - android

I am writing an app which needs to get any fresh location. Accuracy does not metter. My app will mainly be used in buildings, so I am using Network location provider as a main location source.
I have troubles on some devices as follows:
My code can't access any location. But when I start Google Maps on the same device - it shows correct location. I switch back to my program and it does not see any location again.
My code does roughly the following:
Check if Network location provider is available with code below:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
If network provider is enabled - requirest single location update:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Code also starts Timer for 5 seconds. If code does not get requested location update during 5 seconds, then timer is iterating throuhg all available providers trying to get last known location and pick up the best one:
Location location = locationManager.getLastKnownLocation(provider);
The result of all these moves is that network provider is On, but code does not get any updates. Timer starts, loops through all providers and all of them does not have any last known location. getLastKnownLocation method returns null for all of them. The most strage thing is that Google Maps got correct location just a few seconds before this. I've rebooted device with no effect.
I examined best practices of getting location here. They seem to use the same approch as I currently do. Are there any tricks I am missing?

This problem occur in some devices the solution is try to get location from GPS if fails then get location from WIFI device

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

Receiving current location on Android

I've been working on an app that needs to be location-aware, and I've noticed that there are two (or more) methods of receiving location: with Google Play services (as seen here developer.android.com/training/location/retrieve-current.html#GetLocation) and with Location Manager, Providers etc. (as seen here http://www.vogella.com/tutorials/AndroidLocationAPI/article.html#locationapi_criteria).
What is the difference between these methods (if there is any)? Which one is more accurate?
edit: ok, I see that I sent the wrong link on the first thing. Won't this code (http://developer.android.com/training/location/receive-location-updates.html) give me location updates? Generally, what's the most accurate way to get my location?
The one with the GPS is accurate and that which is based on Network is not. Google Play Service use FUSE api to get the GPS location first, if the location is found (that's great), otherwise it will try to get location fix from Network Tower. In Short the one with GPS is accurate
The first method provides the details of LastKnownLocation. ie. the last location received from GPS or network provider when you or other apps accessed the location services. After that there are chances you moved a lot and it need not be your current location. So if You are planning to create an application that requires accurate location tracing, You should fetch the location as in the "Vogella" method. If the current location is unavailable, you can try using the last known location (As a plan B :-)).

GPS is not working inside the building

After few hours of testing outside of house, when i came back to my house i found that GPS is enabled but was not getting location fixes inside the building.Hence its onLocationChanged method couldn't get called.
Problem: How to know that GPS is not getting any location fixes as device continue to sense your location in "trying mode".By trying mode i mean the situation where it is not coming to any result even after 20 to 30 minutes still it declared it self as enabled (blinking in status bar).
How one could know that the GPS doesn't get location so switch to another provider like Network_Provider.
In short i want to get my device to conclude something that GPS can find location fix for sure or you have to take location by another means.
I hope at least someone can give me idea about how to deal with that.
The link below has an awesome tutorial, of how to get the location from GPS and/or Network.
It uses a timer task, which analyzes if there is a GPS location in a specific period of time, assume 20 seconds. If not, it will return the location from Network as the current location. If there is a location from GPS, then it will compare which update is new (latest), and return that.
What is the simplest and most robust way to get the user's current location on Android?

When does the network provider update the location in android

I use network provider to get location in Android application. Sometimes I found the location would not be changed even after I arrived at a new place. At the same time, I found the locations were changed in Google Map. So I quit and restarted my application, and the locations wasn't changed either. But after a long period, or I reboot the phone, I could see the new location.
Does any one know what's the reason ? I have used getLastKnownLocation() to get the latest location in my application.
Update:
When this problem happened, I found the cellid and LAC was changed while the location wasn't changed. So it's very strange. I think Google Map may use cellid & LAC to get the location directly, so it can get the correct location.
More update:
When this problem happened, I used the cellid and LAC to get the location via HTTP POST request from Google server. This location was correct.
BTW, I found my question was like this question: Android network location takes hours to update location
I wouldn't trust getLastKnownLocation(). In my experience this isn't reliable.
You'd be better off requesting location callbacks using LocationManager.requestLocationUpdates(). If you only need the location once you can then unregister for the callbacks using LocationManager.removeUpdates() after that. If your application needs to be notified when the location changes then you can register for callbacks with a reasonable minium-time and/or minimum-distance parameter (based on whatever your application requires.

Want to get current location of user in android activity

I need current location of user in my application on based on that location I will search on database!
I am getting current location right now by using example code given but it is not accurate. If no GPS I get location by network provider but it shows sometime 2 location names at the same location. If it is showing one name 1 min before after 2 min it will change to another location.
This code is look but do not know how to use it. Please help me out in this regard!
What is the simplest and most robust way to get the user's current location on Android?
I need to get the location name.
Network location is not as reliable as GPS location, so you might get some fluctuation. That's even more true if you're relying on wifi APs.
You may try to average locations or wait to see if it stabilises in some location in particular, or show the most common location (i.e., the one reported most of the time).

Categories

Resources