LastKnownLocation is reset everytime - android

I would have thought that requesting the "lastKnownLocation" would return a saved coordinate (GeoPoint) that points to the last known location of the device (from when it last had access to the GPS functionalities).
According to some tests (made on OSMDroid's GpsMyLocationProvider), it seems like requesting that location returns a NullPointer.
GpsMyLocationProvider provider = new GpsMyLocationProvider(MainActivity.mainActivity);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(provider, map);
I've been trying to set up default GeoPoint for centering the map when it is first launched, and then recentering the map where the GPS now has confirmed a position.
The problem is that provider.getLastKnownLocation() returns a NullPointer exception unless it is given time to actually get the current location from the GPS.
What am I getting wrong here? Shouldn't the "last known location" be a GeoPoint that is saved in memory on the phone and always have something there, unless the phone never ever had access to GPS localization ?

Please see https://developer.android.com/training/location/retrieve-current
You must set the permissions correctly too. Note that at the bottom they describe why the location may be null so see these for troubleshooting.
If you're using an emulator see How to emulate GPS location in the Android Emulator?

Related

Mock coordinates Android Marshmallow

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);

Tracking the location in android

I got the current location latitude and longitude while giving on in a switch button in android and displaying the location in google map. But I need the latitude and longitude changes automatically wherever the device moves and the location on the map should change with respect to the position. How is it possible? Can anyone give me a way?
You could have a look at GoogleApiClient.
You can use requestLocationUpdates(...) to get the users position in a given intervall.
You can find more Info here
But honestly you could have find the answer on your own if you googled a bit.

Compare Location coordinates on Android

We want our application to get location data when user opens the Activity and check if it's inside a certain boundary of location.
We are able to get the Coordinates of the current phone location now we want to compare it that if it is inside a building location or not. How do we check if the coordinates (lat Long) of the phone is inside a specific radius range?
thanks
You can use google's Geofence API.
new Geofence.Builder().setCircularRegion(latitude,longitude,radius);
https://developer.android.com/training/location/geofencing.html

How to know gps fix is available or not?

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 ?

Android location and Google Settings

so I have been developing this app for a few weeks now using my own handheld device and today I was trying to get Google map to center in on the current user location in my app
map.setMyLocationEnabled(true); //Locate the current position of the user
The setLocationEnabled(true) above should normally show the location of the user on the map and when the GPS button is pressed it will zoom into it. However after I added the following code this does not work anymore it doesn't show the location of the user anymore and I also I notice "Google Settings" was installed onto my device which after looking around was an update by Google so it might not be the code itself. But I do not know if Google Settings is the cause for this.
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
map.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
Even after commenting out this block of code the problem still persist. I do not know how to fix this and I hope someone could help me. I am not sure if this has to do with the code itself or just my device, it literally does not get the location of the user at all even though it was working for the past few weeks and it doesn't give any errors or warnings even pressing the GPS button on my map gives nothing no errors or warnings.
EDIT: I tried using an older working version of my code for this app and it still does not work in terms of getting the user location to come up on the map.
Thanks.
Why you dont use:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(map.getMyLocation().getLatitude(),map.getMyLocation().getLongitude()), 15));
The map store your actual location, so you dont need LocationManager class.

Categories

Resources