Getting device location Android - android

I'm new to android but my current project requires to work with gps tracking. I've searched the web to find some answers, and some topics confused me. Can you tell me please, I'm not sure I've understand:
Where do we need to use coarse and when fine?
whats the main difference between using NETWORK_PROVIDER and GPS_PROVIDER?

From the documentation:
If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. Permission for ACCESS_COARSE_LOCATION allows access only to NETWORK_PROVIDER.
So in short, the difference is the level of details of the location you get.
The network provider determines the location of the users using cell towers, wifi access points etc. Distance between towers and user’s position are considered in the case of cell towers. This location provider offers a faster response but can be fuzzy.
The GPS provider determines the location of the users using satellites. This is usually more precise, but takes more time.
Note: there is also a PASSIVE_PROVIDER, which doesn't actively requests location, but is able to eavesdrop on other app's requests, i.e. it gets location information when other apps ask for it.

Fused Location Provider gives the location technology and gives you the best location.This api gives the user access to the best location without using much power on the device.
Add to your manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
In activity code
private FusedLocationProviderClient flpClient;
flpClient = LocationServices.getFusedLocationProviderClient(this);
to get the last known location
flpClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
// Logic to handle location object
}
}
});
Look at the api for further explanation https://developer.android.com/training/location/

Related

why ACCESS_COARSE_LOCATION needs to location must be enabled

I am trying to get user location with low battery consumption and without GPS (location). Milimetric coordinates are not necessary for me. So I decided to use
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
and also at this google developer docs https://developer.android.com/guide/topics/location/strategies.html it says:
Permission for ACCESS_COARSE_LOCATION allows access only to
NETWORK_PROVIDER.
I dont actually understand why this API doesn't give user current coordinates without location being enabled. In my opinion, if I use COARSE_LOCATİON instead of FINE_LOCATION, API should not use location.
I am also trying "Basic Location Sample" at https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample
You are requesting for user's LOCATION it may be with FINE LOCATION or COURSE LOCATION. User needs to AUTHORIZE the app for doing that.
The only difference is that the device will spend less energy trying to get the user's location and won't get exactly location.

Is is possible to block android location from using 3G to get location, get only from gps?

This is how I start the location listener:
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, REQUEST, this, Looper.getMainLooper());
Where my request looks like this:
public LocationRequest REQUEST = LocationRequest.create().setInterval(PS_MEDIUM_REQ_TIME).setFastestInterval(PS_MEDIUM_REQ_TIME)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
The issue is when I get into the metro, not having gps signal, it get the data from networking. And 3G etc, works via repeaters, so it will throw my location to the nearest data Tower. I would prefer it to simply just not give locations, in this case, cause throwing my location a couple of kilometers far away is not an acceptable case.
So my question is: is it possible to force it to accept only gps coordinates? Or to atleast check from where they come, so I know if to accept or not?
This is from the docs of the LocationRequest:
Applications cannot specify the exact location sources, such as GPS, that are used by the LocationClient.
But when using the LocationManager you can request location updates specifying a location provder, for example LocationManager.GPS_PROVIDER.
See LocationManager
Since you need to use the FusedLocationProviderApi you might want to use the Location.getAccuracy() method to reject or accept location readings. The accuracy is given as 68% confidence radius. A smaller value means higher accuracy. The higher the accuracy, the more likely it was acquired using GPS.
There also is a Location.getProvider() method which returns the provider if it was set. If the provider wasn't set it will return null.
Additionally you could use LocationRequest.setPriority(int) and use LocationRequest.PRIORITY_HIGH_ACCURACY as parameter to make sure you only get the most accurate readings.

Does location service require Internet to get location?

I am little confused with the answers. can anybody give me a right answer to solve this question. I have developed an emergency alert app. I am getting location from GPS and Network service. In some situation there is no mobile data or WiFi connected so no internet.
I would like to know will my app still get LAT/LONG or is internet required to get LAT/LONG?
I am trying this on Galaxy Nexus which runs version 4.3...
I am using Google play service option:
#Override
public void onConnected(Bundle bundle)
{
locationRequest = LocationRequest.create();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
#Override
public void onLocationChanged(Location location)
{
Log.e("location","onLocationChanged");
if (location != null)
{
Log.e("SaveCurrent","Location"+String.valueOf(location.getLatitude()));
Log.e("SaveCurrent","Location"+String.valueOf(location.getLongitude()));
}
}
Thanks!
GPS - Geolocation Positioning Services works using satellite, so no internet is required if your cellphone have GPS. So coordinates can be acquired using this alone which would be very accurate.
According to wikipedia also :- "The Global Positioning System (GPS) is a space-based satellite navigation system that provides location and time information in all weather conditions, anywhere on or near the Earth where there is an unobstructed line of sight to four or more GPS satellites."
Internet Location - Other than GPS you can also get your current position using internet, which uses your mobile network or wifi network location to triangulate your current location. This is not as highly accurate as GPS.
So for your emergency application you can get the current location if GPS satellite services is available without having any internet connection.
If you want reverse geocoding you will need an internet connection

Get *coarse* location from GPS provider on Android

My app only needs very rough location data, so I originally set my manifest permissions to ACCESS_COARSE_LOCATION, and set up a location listener for NETWORK_PROVIDER. This gave me exactly the kind of rough location estimate I needed, but only with Google Location Services turned on.
I was expecting that if the user only had GPS enabled, that I would still receive a rough estimate of their location. But it seems like the only way to get ANY location information from the GPS_PROVIDER is by using the ACCESS_FINE_LOCATION permission.
So, is it true that with only GPS enabled, an app cannot receive location information unless it has the ACCESS_FINE_LOCATION permission? In other words, the GPS_PROVIDER can't send rough location estimates if the app only has ACCESS_COARSE_LOCATION permission?
So, is it true that with only GPS enabled, an app cannot receive location information unless it has the ACCESS_FINE_LOCATION permission?
Generally speaking, yes.
Quoting the documentation for LocationManager:
If your application only has the coarse permission then it will not have access to the GPS or passive location providers. Other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.
In other words, the GPS_PROVIDER can't send rough location estimates if the app only has ACCESS_COARSE_LOCATION permission?
Quoting the Android 4.2 release notes:
Compared to previous versions of Android, user location results may be less accurate if your app requests the ACCESS_COARSE_LOCATION permission but does not request the ACCESS_FINE_LOCATION permission.
To meet the privacy expectations of users when your app requests permission for coarse location (and not fine location), the system will not provide a user location estimate that’s more accurate than a city block.
However, I am assuming that this does not supersede the "will not have access to the GPS" statement from LocationManager. I assume that this means that NETWORK_PROVIDER data might be inhibited, if it is deemed too accurate.

Android Location Providers - GPS or Network Provider?

In my application I would like to determine the user's current location. I do however have a couple of questions in this regard:
There are different Location Providers, which one is the most accurate? The GPS Provider or the Network Provider?
In how far do those available provider differ? How do they function?
Could you please provide me with some code-snippets or tutorials on how to get started with implementing GPS functionality in my application?
There are 3 location providers in Android.
They are:
gps –> (GPS, AGPS): Name of the GPS location provider. This
provider determines location using satellites. Depending on
conditions, this provider may take a while to return a location fix.
Requires the permission android.permission.ACCESS_FINE_LOCATION.
network –> (AGPS, CellID, WiFi MACID): Name of the network
location provider. This provider determines location based on
availability of cell tower and WiFi access points. Results are
retrieved by means of a network lookup. Requires either of the
permissions android.permission.ACCESS_COARSE_LOCATION or
android.permission.ACCESS_FINE_LOCATION.
passive –> (CellID, WiFi MACID): A special location provider for
receiving locations without actually initiating a location fix. This
provider can be used to passively receive location updates when other
applications or services request them without actually requesting the
locations yourself. This provider will return locations generated by
other providers. Requires the permission
android.permission.ACCESS_FINE_LOCATION, although if the GPS is not
enabled this provider might only return coarse fixes. This is what
Android calls these location providers, however, the underlying
technologies to make this stuff work is mapped to the specific set of
hardware and telco provided capabilities (network service).
The best way is to use the “network” or “passive” provider first,
and then fallback on “gps”, and depending on the task, switch between
providers. This covers all cases, and provides a lowest common
denominator service (in the worst case) and great service (in the best
case).
Article Reference : Android Location Providers - gps, network, passive By Nazmul Idris
Code Reference : https://stackoverflow.com/a/3145655/28557
-----------------------Update-----------------------
Now Android have Fused location provider
The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs. It simplifies ways for apps to get the user’s current location with improved accuracy and lower power usage
Fused location provider provide three ways to fetch location
Last Location: Use when you want to know current location once.
Request Location using Listener: Use when application is on screen / frontend and require continues location.
Request Location using Pending Intent: Use when application in background and require continues location.
References :
Official site : http://developer.android.com/google/play-services/location.html
Fused location provider example:
GIT : https://github.com/kpbird/fused-location-provider-example
http://blog.lemberg.co.uk/fused-location-provider
--------------------------------------------------------
GPS is generally more accurate than network but sometimes GPS is not available, therefore you might need to switch between the two.
A good start might be to look at the android dev site. They had a section dedicated to determining user location and it has all the code samples you need.
http://developer.android.com/guide/topics/location/obtaining-user-location.html
There are some great answers mentioned here. Another approach you could take would be to use some free SDKs available online like Atooma, tranql and Neura, that can be integrated with your Android application (it takes less than 20 min to integrate).
Along with giving you the accurate location of your user, it can also give you good insights about your user’s activities. Also, some of them consume less than 1% of your battery

Categories

Resources