The fused location provider is a location API in Google Play services that intelligently combines different signals to provide the location information that your app needs.
The fused location provider manages the underlying location technologies, such as GPS and Wi-Fi, and provides a simple API that you can use to specify the required quality of service
The main class in fused location provider API is FusedLocationProviderClient. You can get FusedLocationProviderClient object by calling getFusedLocationProviderClient method on LocationServices. FusedLocationProviderClient has getLastLocation and requestLocationUpdates methods which can be used to location. Method getLastLocation gives last know location. If you want location updates as device location changes, you need to use requestLocationUpdates.
These methods run on the background not on the main thread, you can use callback handlers and listeners to get data and update UI.
To get address for the given location, you need to use GeoCoder class. GeoCoder can be used to geocoding and reverse geocoding, geocoding converts street address into longitude and latitude coordinates and reverse-geocoding converts longitude and latitude coordinates into street address.
To get street address of the current location, you need to call getFromLocation method on GeoCoder object passing current location coordinates. To run this call on background thread, we need to use intent service. To receive results from GeoCoder and update UI, you’ll have to define ResultReceiver in the activity.
To use the location manager make the Google play service available via your app build.gradle file.
dependencies {
compile 'com.google.android.gms:play-services:9.2.0'
compile 'com.google.android.gms:play-services-location:9.2.0'
}
Add the following permissions to your application in your AndroidManifest.xml file
1. INTERNET
2. ACCESS_FINE_LOCATION
3. ACCESS_COARSE_LOCATION
If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
1. Create a class extending ResultReceiver. It is generic interface for receiving a callback result from someone. Use this by creating a subclass and implement onReceiveResult(int, Bundle), which you can then pass to others and send through IPC, and receive results they supply with send(int, Bundle).
2. LocationServices is the main entry point for location services integration. Create a new instance of FusedLocationProviderClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.
3. Override onLocationResult() method of LocationCallback. It is used for receiving notifications from the FusedLocationProviderApi when the device location has changed or can no longer be determined. The methods are called if the LocationCallback has been registered with the location client using the requestLocationUpdates(GoogleApiClient, LocationRequest, LocationCallback, Looper) method.
4. Create a method called startLocationUpdates(). This first checks of the app has permissions to use device location informations.
If it has permission, it jumps straight away to creating an instance of LocationRequest. LocationRequest objects are used to request a quality of service for location updates from the FusedLocationProviderApi. Location requests from applications with ACCESS_COARSE_LOCATION and not ACCESS_FINE_LOCATION will be automatically throttled to a slower interval, and the location object will be obfuscated to only show a coarse level of accuracy.
If it doesn't, it requests for permission through requestPermissions() method provided by ActivityCompat.
5. Permission results are handled by onRequestPermissionsResult() method. This interface is the contract for receiving the results for permission requests.
6. This is the method called in onLocationResult() in our calling function. This method directly interacts with Service and initiates it using startService(Intent intent).
Above class works as a Receiver. This interacts or fetches data from a Service.
So, now we'll create a Service class.
Location precision can be increased using following methods:
Locality: address.getLocality()
County: address.getSubAdminArea()
State: address.getAdminArea()
Country: address.getCountryName()
Postal Code: address.getPostalCode()
And you're all set to go!