Fetching an Current Accurate Location in android is not an easy task, So to make this task very simple with just 2-3 lines of code there is an InstantLocate Library which comes in picture.
allprojects{
repositories{
maven{ url 'https://jitpack.io'}
}
}
dependencies {
implementation 'com.github.mukul56:InstantLocate:1.0'
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
First you have to request for permissions which you declared in your Manifest
-
Create an Array of Permissions
String[] permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
-
Request For Permissions
ActivityCompat.requestPermissions(this, permissions, 1);
For fethching location it is necessary to first Request for required permissions
Initialize the InstantLocate by using following line:
InstantLocate instantLocate = new InstantLocate(this);
Methods of InstantLocate:
- instantLocation() This method is used to fetch instantLocation
- getContinuousLocation() This method is used to fetch continuous Location untill you call stop() method
- getLatitude() This method is used to get latitude of a location and return double value
- getLongitude() This method is udes to get longitude of a location and return double value
- stop() This method is used to stop fetching of location
- isGpsEnabled() This method is used to check Gps is enabled or not. If Gps is not enabled then you should call showSettingAlert() to enable Gps
-
If you want to get instantLocation then you need not to call stop() method
-
If you call getContinuousLocation then you should call stop() method to stop fetching of location