i need help as soon as possible, right now, i have developed a app that must show current Location Client, and i have try http://developer.android.com/training/location/retrieve-current.html and its work well, but i want to show the client location without clicking the button, i have try to change the memthod getLocation(), but everytime i have tried to call method getLocation(), my app always error,
nb: i have removed the parameter of getLocation() and the xml i already change too. i have try to put the method in onCreated and also in onStart, it's error
anybody can help me please to solve my problem?
for Google map v2(the latest version),you should implements the LocationSource to update current location , than also you do need make a location request use LocationClient to get current location then update it with LocationSource.OnLocationChangedListener
Related
In my app I am using FusedLocationProviderClient to get user location every time he enters the app by mFusedLocationClient.getLastLocation().addOnCompleteListener().But sometimes result is null,I am using <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in manifest.How can I get user location even if it not so correct?Because the logic of app depends on that location,I don't need to show it on map or smth like that,just get the location.Thx in advance
Don't use getLastLocation(). There is always a chance to have it return null if it doesn't have a location currently.. Use requestUpdates or requestSingleUpdates- when the callback for those is called the location is guaranteed to be non-null. getLastLocation() should only be used as an optimization to improve speed, it cannot be relied on.
Follow the following example and your app is guaranteed to work as you wish. I have recently used this code and it is working like a charm.
Only thing you should know is that for the location updates to actually work ( to get real latitude and longitude values) you should enable "google play services". There may be several ways to do this but perhaps the simplest of all is to open "settings" on your device and probably somewhere under "security" or "permissions" (depending on your android distribution) find "google play services" and give it the permission it needs.
https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates
"Exception for the locationjava.lang.IndexOutOfBoundsException: Invalid index 0, size is 0"
I had this error for no reason in my android app which was able to fetch the current longitude latitude of current location using LocationTracker and later on by the help of Geocoder was able to display the name of the place in string.
I didnt see anything wrong with my code also before my app was working fine.
Later, I found out the problem was due to not granting permission for the app I had to go to
>>setting>>application>>app manager>>appname>>>>permissions and turn on the location toggle button.
After rerunning the app the problem was solved.
My runtime message showed error in this line before solving the problem
String address = addresses.get(0).getAddressLine(0);
which is written under button listener for the button which on clicking we get the location of the place
Are there any other reasons possible for similar error??
Also I think good practice will be to place the code in MainActivity so one can grant the permission through the application itself and dont have to go to the setting>>application....and so on. Any suggestion in this matter??
also let me know whether the technique i used to solve the problem was right or not??
In my app I am using Android Location Manager API and I have a requirement of identifying whether the location coordinates are from normal Location Manager or if they are from Fake(Mock) Location provider. I used location.isFromMockProvider(). Once I turn on the fake location providing app, isFromMockProvider() method returns me true. And after that it does not matter if I turn, fake location providing app, on or off isFromMockProvider() method always returns true.
I found that once FakeGPS app, once triggered, overrides the location manager coordinates and once I stop the FakeGPS app, the Location Manager does not start collecting correct coordinates.
Is there something I am missing.
Disappointed with no response but I am glad that I got it working :)
Posting the resolution as it might help someone else.
Once the FakeGPS app starts mocking location, it stops my app's Location manager.
Now even if I stop the FakeGPS app mocking service, the location manager of my app won't trigger back.
I have to detect the event and restart the location manager of my app, after removing any test provider and clearing test locations.
just resetting and removing test location should help
String t = LocationManager.GPS_PROVIDER;
locationManager.clearTestProviderEnabled(t);
locationManager.clearTestProviderLocation(t);
locationManager.clearTestProviderStatus(t);
locationManager.removeTestProvider(t);
I am working on app. When user click on button he will get current location using GPS.
All is working fine but one problem.
Problem
There might be some location where gps will not work...like closed area...and gps can not find location...at that time it will return previously know location...Previously known location may be too older or wrong.
so at that time I want no location found instead of last location.
Is there any way to check if fix is available or not ?
I want to get user's location in my app. So, I read documentations and know how to get it work. But, if user haven't set "Alloy apps to use internet/GPS to get location" in preferences, my app crashes.
The only way I found is to show dialog that location settings are not enabled and to go programmaticaly to location preferences.
But is there another way to solve this? I saw many apps (like yandex.maps) that do not request this settings being enabled.
well, your app should not install if the user doesn't agree to the terms of use, and allow the permissions requested.
still you should check to see that there's a way to look the information up when you want it.
in your code, before the actual test for user's location, you should probably call a function that checks for all the location manager listings, and return false if there's no way to determine this.
if the function returns false, you should prompt the user and do nothing. else, you should do what it is you're trying to do.
try reading vogella's tutorial about using location manager
Refer this two links below and you might get your answer easily.
How do I get the current GPS location programmatically in Android?
android - how to get current location latitude and longitude
Hope it will help you.