Skip to content

Instantly share code, notes, and snippets.

@arkilis
Created November 30, 2017 06:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkilis/7aef93cd50cf372d3a7c3ed5391e8fbf to your computer and use it in GitHub Desktop.
Save arkilis/7aef93cd50cf372d3a7c3ed5391e8fbf to your computer and use it in GitHub Desktop.
Get location from google map on kotlin
// 1.
class LocationActivity : AppCompatActivity(), OnMapReadyCallback {
// 2.
private var mLocationRequest: LocationRequest? = null
private val UPDATE_INTERVAL = (10 * 1000).toLong() /* 10 secs */
private val FASTEST_INTERVAL: Long = 2000 /* 2 sec */
private var latitude = 0.0
private var longitude = 0.0
private lateinit var mGoogleMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_location)
val mapFragment = supportFragmentManager.findFragmentById(R.id.location) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onStart() {
super.onStart()
startLocationUpdates()
}
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap;
if (mGoogleMap != null) {
mGoogleMap!!.addMarker(MarkerOptions().position(LatLng(latitude, longitude)).title("Current Location"))
}
}
// 3.
protected fun startLocationUpdates() {
// initialize location request object
mLocationRequest = LocationRequest.create()
mLocationRequest!!.run {
setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
setInterval(UPDATE_INTERVAL)
setFastestInterval(FASTEST_INTERVAL)
}
// initialize location setting request builder object
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest!!)
val locationSettingsRequest = builder.build()
// initialize location service object
val settingsClient = LocationServices.getSettingsClient(this)
settingsClient!!.checkLocationSettings(locationSettingsRequest)
// call register location listener
registerLocationListner()
}
private fun registerLocationListner() {
// initialize location callback object
val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
onLocationChanged(locationResult!!.getLastLocation())
}
}
// 4. add permission if android version is greater then 23
if(Build.VERSION.SDK_INT >= 23 && checkPermission()) {
LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper())
}
}
//
private fun onLocationChanged(location: Location) {
// create message for toast with updated latitude and longitudefa
var msg = "Updated Location: " + location.latitude + " , " +location.longitude
// show toast message with updated location
//Toast.makeText(this,msg, Toast.LENGTH_LONG).show()
val location = LatLng(location.latitude, location.longitude)
mGoogleMap!!.clear()
mGoogleMap!!.addMarker(MarkerOptions().position(location).title("Current Location"))
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(location))
}
private fun checkPermission() : Boolean {
if (ContextCompat.checkSelfPermission(this , android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
requestPermissions()
return false
}
}
private fun requestPermissions() {
ActivityCompat.requestPermissions(this, arrayOf("Manifest.permission.ACCESS_FINE_LOCATION"),1)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode == 1) {
if (permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION ) {
registerLocationListner()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment