Last active
January 13, 2019 14:16
-
-
Save piumimaheshika/25ee9b0d8467f99028770d7841756359 to your computer and use it in GitHub Desktop.
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.example.piumi.currentlocation | |
| import android.Manifest | |
| import android.content.pm.PackageManager | |
| import android.location.Location | |
| import android.os.Bundle | |
| import android.support.v4.app.ActivityCompat | |
| import android.support.v7.app.AppCompatActivity | |
| import android.widget.Toast | |
| import com.example.piumi.location.R | |
| import com.google.android.gms.location.* | |
| import kotlinx.android.synthetic.main.activity_main.* | |
| class MainActivity : AppCompatActivity() | |
| { | |
| private var mFusedLocationClient: FusedLocationProviderClient? = null | |
| private var locationRequest: LocationRequest? = null | |
| private var mCurrentLocation: Location? = null | |
| private var locationCallback: LocationCallback? = null | |
| private val locationRequestCode = 1000 | |
| override fun onCreate(savedInstanceState: Bundle?) | |
| { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this) | |
| locationRequest = LocationRequest.create() | |
| locationRequest?.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY | |
| locationRequest?.interval = (10 * 1000).toLong() // 10 seconds | |
| locationRequest?.fastestInterval = (5 * 1000).toLong() // 5 seconds | |
| locationCallback = object : LocationCallback() { | |
| override fun onLocationResult(locationResult: LocationResult?) { | |
| if (locationResult == null) { | |
| return | |
| } | |
| for (location in locationResult.locations) { | |
| if (location != null) { | |
| mCurrentLocation = location | |
| if (mFusedLocationClient != null) { | |
| mFusedLocationClient?.removeLocationUpdates(locationCallback) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| //Check user permission at run time | |
| if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && | |
| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
| ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), | |
| locationRequestCode) | |
| } | |
| else | |
| { | |
| requestLocation() | |
| } | |
| button_location.setOnClickListener { | |
| var location = getCurrentLocation() | |
| text_location.text = location?.latitude.toString() + " " + location?.longitude.toString() | |
| } | |
| } | |
| //request location | |
| private fun requestLocation() | |
| { | |
| mFusedLocationClient?.lastLocation?.addOnSuccessListener(this) { location -> | |
| if (location != null) { | |
| mCurrentLocation = location | |
| } else { | |
| mFusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback, null) | |
| } | |
| } | |
| } | |
| //on request permission result | |
| override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { | |
| when (requestCode) { | |
| 1000 -> { | |
| // If request is cancelled, the result arrays are empty. | |
| if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
| requestLocation() | |
| } else { | |
| Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show() | |
| } | |
| } | |
| } | |
| } | |
| private fun getCurrentLocation(): Location? { | |
| return mCurrentLocation ?: null | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment