Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.morristech:EasyLocation:1.0'
}
dependencies {
implementation("com.github.morristech:EasyLocation:1.0")
}
<dependency>
<groupId>com.github.morristech</groupId>
<artifactId>EasyLocation</artifactId>
<version>1.0</version>
</dependency>
libraryDependencies += "com.github.morristech" % "EasyLocation" % "1.0"
:dependencies [[com.github.morristech/EasyLocation "1.0"]]
Getting location updates requires lots of boilerplate code in Android, You need to take care of
EasyLocation does all this stuff in background, so that you can concentrate on your business logic than handling all above
In your build.gradle
:
Add the following maven{} line to your PROJECT build.gradle file
allprojects {
repositories {
...
maven { url "https://jitpack.io" } // add this line
}
}
com.google.android.gms:play-services-location dependency also needs to be added like this
x.x.x can be replaced with google play service version your app is using versions information available here
dependencies {
compile 'com.github.vishal259:EasyLocation:1.0'
compile "com.google.android.gms:play-services-location:x.x.x"
}
Extend your Activity
from EasyLocationAppCompatActivity
or EasyLocationActivity
:
Create location request according to your needs
LocationRequest locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(5000)
.setFastestInterval(5000);
Create EasyLocation request, and set locationRequest created
EasyLocationRequest easyLocationRequest = new EasyLocationRequestBuilder()
.setLocationRequest(locationRequest)
.setFallBackToLastLocationTime(3000)
.build();
}
Request Single location update like this
requestSingleLocationFix(easyLocationRequest);
Or Request Multiple location updates like this
requestLocationUpdates(easyLocationRequest);
You're good to go!, You will get below callbacks now in your activity
@Override
public void onLocationPermissionGranted() {
}
@Override
public void onLocationPermissionDenied() {
}
@Override
public void onLocationReceived(Location location) {
}
@Override
public void onLocationProviderEnabled() {
}
@Override
public void onLocationProviderDisabled() {
}
Additional Options
Specify what messages you want to show to user using EasyLocationRequestBuilder
EasyLocationRequest easyLocationRequest = new EasyLocationRequestBuilder()
.setLocationRequest(locationRequest)
.setLocationPermissionDialogTitle(getString(R.string.location_permission_dialog_title))
.setLocationPermissionDialogMessage(getString(R.string.location_permission_dialog_message))
.setLocationPermissionDialogNegativeButtonText(getString(R.string.not_now))
.setLocationPermissionDialogPositiveButtonText(getString(R.string.yes))
.setLocationSettingsDialogTitle(getString(R.string.location_services_off))
.setLocationSettingsDialogMessage(getString(R.string.open_location_settings))
.setLocationSettingsDialogNegativeButtonText(getString(R.string.not_now))
.setLocationSettingsDialogPositiveButtonText(getString(R.string.yes))
.build();
Copyright 2016 Vishal Sojitra
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.