-
Notifications
You must be signed in to change notification settings - Fork 0
/
get last location()
172 lines (155 loc) · 6.12 KB
/
get last location()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.example.maps
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Geocoder
import android.location.LocationManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.android.gms.location.*
import com.google.android.gms.maps.model.LatLng
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
// variable that we need
lateinit var fusedLocationProviderClient: FusedLocationProviderClient
lateinit var locationRequest: LocationRequest
// just a unique number
var PERMISSION_ID = 1000
var Latitude: Double = 0.0
var Longitude: Double = 0.0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initial lateinit var
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
buttonGetLocation.setOnClickListener{
getLastLoacation()
}
buttonMaps.setOnClickListener{
val intent = Intent(this, MapsActivity::class.java)
intent.putExtra("LATITUDE", Latitude)
intent.putExtra("LONGITUDE", Longitude)
startActivity(intent)
}
}
// get last location
private fun getLastLoacation() {
// check permission
if (checkPermission()) {
// check location services is enable
if (isLocationEnable()) {
// get the location
fusedLocationProviderClient.lastLocation.addOnCompleteListener { task ->
var location = task.result
if (location == null) {
getNewLocation()
} else {
Latitude = location.latitude
Longitude = location.longitude
textViewLocation.text =
"Your Current Cordinate Are " +
"\nLat: $Latitude" +
"\nLong: $Longitude" +
"\nCountryName: ${getCountryName(location.latitude, location.longitude)}" +
"\nCityName: ${getCityName(location.latitude, location.longitude)}"
}
}
} else {
Toast.makeText(this, "Please Enable Your Location Services", Toast.LENGTH_SHORT)
.show()
}
} else {
requestPermission()
}
}
// get new location
private fun getNewLocation() {
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
checkPermission()
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallBack, Looper.myLooper())
}
// get city name
private fun getCityName(lat: Double, long: Double): String{
var cityName = ""
var geoCoder = Geocoder(this, Locale.getDefault())
var address = geoCoder.getFromLocation(lat, long, 1)
cityName = address.get(0).locality
return cityName
}
private fun getCountryName(lat: Double, long: Double): String{
var countryName = ""
var geoCoder = Geocoder(this, Locale.getDefault())
var address = geoCoder.getFromLocation(lat, long, 1)
countryName = address.get(0).countryName
return countryName
}
private val locationCallBack = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult) {
var lastLocation = p0.lastLocation
Latitude = lastLocation.latitude
Longitude = lastLocation.longitude
textViewLocation.text =
"Your Current Cordinate Are " +
"\nLat: $Latitude" +
"\nLong: $Longitude" +
"\nCountryName: ${getCountryName(Latitude, Longitude)}" +
"\nCityName: ${getCityName(Latitude, Longitude)}"
}
}
// check uses permission
private fun checkPermission(): Boolean {
if (
ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
return true
}
return false
}
// request get user permission
private fun requestPermission() {
ActivityCompat.requestPermissions(
this,
arrayOf(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
),
PERMISSION_ID
)
}
// check if location services is enable
private fun isLocationEnable(): Boolean {
var locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// built in function that check permission result
if (requestCode == PERMISSION_ID) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("Debug: ", "You have he permission")
}
}
}
}