Skip to content

Commit 1673a26

Browse files
committed
Implementation Done
1 parent 2fc4728 commit 1673a26

File tree

9 files changed

+191
-9
lines changed

9 files changed

+191
-9
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# Fused Location API Example
1+
# Fused Location API Example - An example project to demonstrate how to use Fused Location API to fetch the current location in Android
2+
[![MindOrks](https://img.shields.io/badge/mindorks-opensource-blue.svg)](https://mindorks.com/open-source-projects)
3+
[![MindOrks Community](https://img.shields.io/badge/join-community-blue.svg)](https://mindorks.com/join-community)
4+
5+
<p align="center">
6+
<img src="https://github.com/MindorksOpenSource/Fused-Location-API-Example/assets/using-gps-location-manager-in-android-android-tutorial-banner.jpg">
7+
</p>
8+
<br>
9+
10+
### Reference Blog Step by Step Guide - [Using Fused Location API To Fetch Current Location](https://blog.mindorks.com/using-gps-location-manager-in-android-android-tutorial)

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ dependencies {
3434
testImplementation 'junit:junit:4.12'
3535
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3636
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
37+
implementation 'com.google.android.gms:play-services-location:17.0.0'
3738
}

app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.mindorks.example.fusedlocation">
44

5+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,96 @@
11
package com.mindorks.example.fusedlocation
22

3+
import android.content.pm.PackageManager
34
import androidx.appcompat.app.AppCompatActivity
45
import android.os.Bundle
6+
import android.os.Looper
7+
import android.widget.Toast
8+
import com.google.android.gms.location.LocationCallback
9+
import com.google.android.gms.location.LocationRequest
10+
import com.google.android.gms.location.LocationResult
11+
import com.google.android.gms.location.LocationServices
12+
import com.mindorks.example.fusedlocation.utils.PermissionUtils
13+
import kotlinx.android.synthetic.main.activity_main.*
514

615
class MainActivity : AppCompatActivity() {
716

17+
companion object {
18+
private const val LOCATION_PERMISSION_REQUEST_CODE = 999
19+
}
820
override fun onCreate(savedInstanceState: Bundle?) {
921
super.onCreate(savedInstanceState)
1022
setContentView(R.layout.activity_main)
1123
}
12-
}
24+
25+
private fun setUpLocationListener() {
26+
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
27+
// for getting the current location update after every 2 seconds with high accuracy
28+
val locationRequest = LocationRequest().setInterval(2000).setFastestInterval(2000)
29+
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
30+
fusedLocationProviderClient.requestLocationUpdates(
31+
locationRequest,
32+
object : LocationCallback() {
33+
override fun onLocationResult(locationResult: LocationResult) {
34+
super.onLocationResult(locationResult)
35+
for (location in locationResult.locations) {
36+
latTextView.text = location.latitude.toString()
37+
lngTextView.text = location.longitude.toString()
38+
}
39+
// Few more things we can do here:
40+
// For example: Update the location of user on server
41+
}
42+
},
43+
Looper.myLooper()
44+
)
45+
}
46+
47+
override fun onStart() {
48+
super.onStart()
49+
when {
50+
PermissionUtils.isAccessFineLocationGranted(this) -> {
51+
when {
52+
PermissionUtils.isLocationEnabled(this) -> {
53+
setUpLocationListener()
54+
}
55+
else -> {
56+
PermissionUtils.showGPSNotEnabledDialog(this)
57+
}
58+
}
59+
}
60+
else -> {
61+
PermissionUtils.requestAccessFineLocationPermission(
62+
this,
63+
LOCATION_PERMISSION_REQUEST_CODE
64+
)
65+
}
66+
}
67+
}
68+
69+
override fun onRequestPermissionsResult(
70+
requestCode: Int,
71+
permissions: Array<out String>,
72+
grantResults: IntArray
73+
) {
74+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
75+
when (requestCode) {
76+
LOCATION_PERMISSION_REQUEST_CODE -> {
77+
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
78+
when {
79+
PermissionUtils.isLocationEnabled(this) -> {
80+
setUpLocationListener()
81+
}
82+
else -> {
83+
PermissionUtils.showGPSNotEnabledDialog(this)
84+
}
85+
}
86+
} else {
87+
Toast.makeText(
88+
this,
89+
getString(R.string.location_permission_not_granted),
90+
Toast.LENGTH_LONG
91+
).show()
92+
}
93+
}
94+
}
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.mindorks.example.fusedlocation.utils
2+
3+
import android.Manifest
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.content.pm.PackageManager
7+
import android.location.LocationManager
8+
import android.provider.Settings
9+
import androidx.appcompat.app.AlertDialog
10+
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.core.app.ActivityCompat
12+
import androidx.core.content.ContextCompat
13+
import com.mindorks.example.fusedlocation.R
14+
15+
object PermissionUtils {
16+
/**
17+
* Function to request permission from the user
18+
*/
19+
fun requestAccessFineLocationPermission(activity: AppCompatActivity, requestId: Int) {
20+
ActivityCompat.requestPermissions(
21+
activity,
22+
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
23+
requestId
24+
)
25+
}
26+
27+
/**
28+
* Function to check if the location permissions are granted or not
29+
*/
30+
fun isAccessFineLocationGranted(context: Context): Boolean {
31+
return ContextCompat
32+
.checkSelfPermission(
33+
context,
34+
Manifest.permission.ACCESS_FINE_LOCATION
35+
) == PackageManager.PERMISSION_GRANTED
36+
}
37+
38+
/**
39+
* Function to check if location of the device is enabled or not
40+
*/
41+
fun isLocationEnabled(context: Context): Boolean {
42+
val locationManager: LocationManager =
43+
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
44+
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
45+
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
46+
}
47+
48+
/**
49+
* Function to show the "enable GPS" Dialog box
50+
*/
51+
fun showGPSNotEnabledDialog(context: Context) {
52+
AlertDialog.Builder(context)
53+
.setTitle(context.getString(R.string.enable_gps))
54+
.setMessage(context.getString(R.string.required_for_this_app))
55+
.setCancelable(false)
56+
.setPositiveButton(context.getString(R.string.enable_now)) { _, _ ->
57+
context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
58+
}
59+
.show()
60+
}
61+
}

app/src/main/res/layout/activity_main.xml

+25-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,31 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
10-
android:layout_width="wrap_content"
9+
<LinearLayout
10+
android:id="@+id/linearLayout"
11+
android:layout_width="match_parent"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
1313
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
14+
app:layout_constraintEnd_toEndOf="parent"
15+
app:layout_constraintStart_toStartOf="parent"
16+
app:layout_constraintTop_toTopOf="parent"
17+
android:orientation="vertical">
18+
<TextView
19+
android:id="@+id/latTextView"
20+
android:layout_width="match_parent"
21+
android:layout_height="60sp"
22+
android:text="@string/default_lat"
23+
android:gravity="center"
24+
android:layout_marginBottom="8dp"
25+
android:textColor="@color/colorBlack"/>
26+
<TextView
27+
android:id="@+id/lngTextView"
28+
android:layout_width="match_parent"
29+
android:layout_height="60sp"
30+
android:text="@string/default_lng"
31+
android:gravity="center"
32+
android:layout_marginBottom="8dp"
33+
android:textColor="@color/colorBlack"/>
34+
</LinearLayout>
1735

18-
</androidx.constraintlayout.widget.ConstraintLayout>
36+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/colors.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<color name="colorPrimary">#6200EE</color>
44
<color name="colorPrimaryDark">#3700B3</color>
55
<color name="colorAccent">#03DAC5</color>
6+
<color name="colorBlack">#000000</color>
67
</resources>

app/src/main/res/values/strings.xml

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<resources>
22
<string name="app_name">FusedLocationAPIExample</string>
3+
<string name="default_lat">00.000000</string>
4+
<string name="default_lng">00.000000</string>
5+
<string name="enable_gps">Enable GPS</string>
6+
<string name="required_for_this_app">Required for this app</string>
7+
<string name="enable_now">Enable Now</string>
8+
<string name="location_permission_not_granted">Location Permission not granted</string>
39
</resources>
Loading

0 commit comments

Comments
 (0)