get location without google play services -android - android

I know some phones do not preinstall google play sevice. I want to ask is there possible way to get location without Google Play Services , just use LocationManager. Not LocationClient.

you can use LocationManager
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

USe a LocationManager and set the appropriate provider, GPS or NETWORK.
you can get periodic updates or a single update using the requestLocationUpdates() and the getLastKnownLocation() methods respectively.
Lastly, get a Location object and retrieve the latitude, longitude by calling the required methods on the Location object.

Related

LocationManager still cant find last known location after GPS is turned on

I'm having a problem with android where even after the user turns their location service on, the LocationManager still can't find the users location.
Before I launch the activity that needs to use the user's location I call the following:
//Menu Activity
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER){
Intent nearby = new Intent(mActivityContext, NearbyActivity.class);
startActivity(nearby);
}else{
alertNoGps();
}
Once GPS is turned on the nearby activity can be launched, but the LocationManager still cannot find the users last known location. My relevant code for this is:
//Nearby Activity
try{
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
...
}else{
Toast.makeText ...
}
} catch(SecurityException e){
e.printStackTrace();
}
The phone I'm currently testing on is running Android Marshmallow 6.0.1. If the GPS is already turned on before the app is run through android studio then I have no problem getting the last known location. It's only when the GPS is turned off before I run the app and turn it on while the app is running that I have problems. What about LocationManger is causing this?
Edit: Also in the manifest I have both Fine and Coarse location permissions. I do not know if this is an issue with API 23 and above because of the extra permission checks required, but I have to imagine they are still handled by the Security Exception.
There are two different ways to retrieve the location, one is based on network and another is based on GPS services. Certainly, location based on GPS services would always be more accurate.
According to the documentation, it might take some time to receive the location using the GPS services. In this case, the last known location can be used to get the recently available location by using getLastKnownLocation() method.
Here, to get the last available location, you should change
LocationManager.GPS_PROVIDER to LocationManager.NETWORK_PROVIDER.
And if you wish to receive the accurate, real time location based on GPS services, I recommend you to use FusedLocationApi.
You can find it here
https://developer.android.com/training/location/receive-location-updates.html
Cheers..!!
This is exactly how it should work. Android clears the last known location for a provider when it is disabled, and will return null until a new point is polled for that provider.
In your situation, I would recommend that if you get null location point for last known location with GPS, just request for a single update.
I found out that similar to Pablo's answer, getLastKnownLocation() will be cleared and because it is a rather casual function can return null quite often. If GPS coordinates are more demanded, then you should be more forceful when trying to get GPS coordinates and use something like requestLocationUpdates() or Google's FusedLocationApi rather than getLastKnownLocation().
So now I am using FusedLocationApi with a GoogleApiClient I built using:
client = new GoogleApiClient.Builder(mActivityContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
After the client is connected I find the GPS location in the onConnected() method override which looks like:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = LocationServices.FusedLocationApi.getLastLocation(client);
This seems to be much more reliable in finding GPS coordinates.

Why use GoogleAPIClient to request location updates?

I noticed that it's possible to request location updates in (at least) two different ways.
Using GoogleAPIClient:
// Callback for when the GoogleAPIClient is connected
#Override
public void onConnected(Bundle connectionHint) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
Using LocationManager:
LocationManager locationManager = (LocationManager) getActivity().
getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, locationListener);
Google seems to promote method 1 in their tutorial "Receiving Location Updates". It's unclear to me what the benefit of method 1 is, because method 2 is working just as fine for me (I'm using both methods in two different places in an app).
Good news, from Google Play Services version 11.0, no need to connect GoogleApiClient
https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html
You don't have to handle callbacks and states(connected, connecting), just request/remove location updates, similar taste of LocationManager.
The new method to get location updates is to use Fused Location Provider in android.
The best thing about Fused location provider API is that you not need to worry about location updates it gives you always latest and accurate location and updates location after a interval or on location change .
One more thing about Fused location provider API is you don't need to think about best location provider because it automatically choose best one suited for your hardware of your android device.
Here is a YouTube Video in I/O 2013, which may provide more details.

How to check if location available using FusedLocationProviderApi and GoogleClient?

We can check for location availability using Location manager's isProviderEnabled method.
I am trying to use the new api's for location, i.e. fusedLocationProviderApi and trying to check if location can be retrieved.
How to check that ?
As you've mentioned in your question, You can check if the Provider is enabled by using Location Manager's isProviderEnabled method, like below.
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
By using FusedLocationApi with GoogleApiClient and LocationRequest, you can request Location Updates with LocationListener Interface which will have a callback method called when the location is changed. onLocationChanged(Location location). So you can know if the location can be granted or not in onLocationChanged method.
So an example flow can be; Checking Location providers to see if they are enabled, and then trying to connect to GoogleApiClient to see if the user has GooglePlayServices etc that can get the User's Location.

Access only location manager

I want to check location service is open or not when the app starts.
I use this code :
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
However this code check GPS open or not. In my program, i don't need GPS. I just need location access permission. How can i do that?
Thanks for help.
If you are referring on using either Wifi/Data on accessing location:
LocationManager.NETWORK_PROVIDER

How to get current location on android

Hey guys
i want to get the latitude and longitude of the users location.
I have tried developers site and stack overflow , there are loads of examples targeted on the moving user which use some thing like :-
onLocationChanged()
I dont need updates of the location , the user will just open it for a minute or two
so i need to find the location the time the application starts and what if the user has never used any geolocation app before hence i would also like an alternative for
LocationManager.getLastKnownLocation()
My code is :-
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(currentLocation.this, "Latitude -> "+Double.toString(loc.getLatitude())
+" Longitude is -> "+Double.toString(loc.getLongitude()), Toast.LENGTH_LONG).show();
and is working fine.
But i am scared that the getLastKnownLocation may return null when used for first time.
Thanks
Sign up for location updates and once you receive the first update, you can unregister for future updates.
In other words, create a LocationListener and register it with your LocationManager's requestLocationUpdates method. In the onLocationChanged method of your LocationListener, call the removeUpdates method on your LocationManager, with this as parameter.
It might take some time before you get a new location, but except for using getLastKnownLocation there's not really an alternative.

Categories

Resources