Reputation: 6743
I want to retrieve the current position of my phone on Kotlin.
This is my main code (taken from https://androidteachers.com/kotlin-for-android/get-location-in-android-with-kotlin):
package com.anta40.app.locationtest;
import android.Manifest
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v7.app.AlertDialog
import android.widget.Toast
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.OnSuccessListener
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private val TAG = "MainActivity"
private lateinit var mGoogleApiClient: GoogleApiClient
private var mLocationManager: LocationManager? = null
lateinit var mLocation: Location
private var mLocationRequest: LocationRequest? = null
private val listener: com.google.android.gms.location.LocationListener? = null
private val UPDATE_INTERVAL = (2 * 1000).toLong() /* 10 secs */
private val FASTEST_INTERVAL: Long = 2000 /* 2 sec */
lateinit var locationManager: LocationManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mGoogleApiClient = GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build()
mLocationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager
checkLocation()
}
override fun onStart() {
super.onStart()
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
override fun onStop() {
super.onStop()
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
override fun onConnected(p0: Bundle?) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
startLocationUpdates();
var fusedLocationProviderClient :
FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient .getLastLocation()
.addOnSuccessListener(this, OnSuccessListener<Location> { location ->
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
mLocation = location;
txt_latitude.setText("" + mLocation.latitude)
txt_longitude.setText("" + mLocation.longitude)
Toast.makeText(
applicationContext, "Position: (" + mLocation.latitude.toString() + "," +
mLocation.longitude.toString(),
Toast.LENGTH_SHORT
).show()
}
})
}
override fun onConnectionSuspended(p0: Int) {
mGoogleApiClient.connect();
Toast.makeText(applicationContext, "Connection suspended...", Toast.LENGTH_SHORT).show()
}
override fun onConnectionFailed(p0: ConnectionResult) {
Toast.makeText(applicationContext, "Connection error: "+p0.errorMessage, Toast.LENGTH_SHORT).show()
}
override fun onLocationChanged(p0: Location?) {
if (p0 != null) {
Toast.makeText(
applicationContext, "Position: (" + p0.latitude.toString() + "," + p0.longitude.toString(),
Toast.LENGTH_SHORT
).show()
txt_latitude.setText("" + mLocation.latitude)
txt_longitude.setText("" + mLocation.longitude)
}
}
private fun checkLocation(): Boolean {
if(!isLocationEnabled())
showAlert();
return isLocationEnabled();
}
private fun isLocationEnabled(): Boolean {
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
private fun showAlert() {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " + "use this app")
.setPositiveButton("Location Settings", DialogInterface.OnClickListener { paramDialogInterface, paramInt ->
val myIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(myIntent)
})
.setNegativeButton("Cancel", DialogInterface.OnClickListener { paramDialogInterface, paramInt -> })
dialog.show()
}
protected fun startLocationUpdates() {
// Create the location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);
// Request location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
}
I run the code on an unrooted Android phone running Android 8 (no SIM card, WiFi only). Location access is already enabled. Yet, the app doesn't display the location. Debugging didn't show any specifc error.
What's wrong here?
Upvotes: 1
Views: 3129
Reputation: 1756
Location info needs run time permission after Android SDK 6.0. I couldn't see it in the code you sent. (https://androidteachers.com/kotlin-for-android/get-location-in-android-with-kotlin)
You should get run time permission in this case
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; }
You can sample here; (https://developer.android.com/training/permissions/requesting)
Or
You can give permission on devices application setting. (Only Test Usage)
Upvotes: 1