Created
April 25, 2019 07:29
-
-
Save anta40/d0493e792a715464a17e8ab1d872fe09 to your computer and use it in GitHub Desktop.
Show current latitude & longitude in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.v4.content.ContextCompat | |
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 */ | |
private val MY_PERMISSIONS_REQUEST_ACCESS_LOCATION = 123 | |
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 (ContextCompat.checkSelfPermission(this@MainActivity, | |
Manifest.permission.ACCESS_FINE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
// Permission is not granted | |
// Should we show an explanation? | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this@MainActivity, | |
Manifest.permission.ACCESS_FINE_LOCATION)) { | |
// Show an explanation to the user *asynchronously* -- don't block | |
// this thread waiting for the user's response! After the user | |
// sees the explanation, try again to request the permission. | |
} else { | |
// No explanation needed, we can request the permission. | |
// ActivityCompat.requestPermissions(this@MainActivity, | |
// arrayOf(Manifest.permission.READ_CONTACTS), | |
// MY_PERMISSIONS_REQUEST_ACCESS_LOCATION) | |
showPermissionRequest() | |
} | |
} else { | |
// Permission has already been granted | |
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() | |
} | |
}) | |
} | |
startLocationUpdates(); | |
} | |
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 showPermissionRequest(){ | |
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 -> | |
ActivityCompat.requestPermissions(this@MainActivity, | |
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), | |
MY_PERMISSIONS_REQUEST_ACCESS_LOCATION); | |
}) | |
.setNegativeButton("Cancel", DialogInterface.OnClickListener { paramDialogInterface, paramInt -> }) | |
dialog.show() | |
} | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment