Advertisement
Guest User

MapsActivity

a guest
Nov 5th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.90 KB | None | 0 0
  1. import android.content.Context
  2. import android.content.pm.PackageManager
  3. import android.location.Location
  4. import android.location.LocationListener
  5. import android.location.LocationManager
  6. import android.os.Build
  7. import android.os.Bundle
  8. import android.support.v4.app.ActivityCompat
  9. import android.support.v4.app.FragmentActivity
  10. import android.widget.Toast
  11.  
  12. import com.google.android.gms.maps.CameraUpdateFactory
  13. import com.google.android.gms.maps.GoogleMap
  14. import com.google.android.gms.maps.OnMapReadyCallback
  15. import com.google.android.gms.maps.SupportMapFragment
  16. import com.google.android.gms.maps.model.BitmapDescriptorFactory
  17. import com.google.android.gms.maps.model.LatLng
  18. import com.google.android.gms.maps.model.MarkerOptions
  19. import java.lang.Exception
  20.  
  21.  
  22. class MapsActivity : FragmentActivity(), OnMapReadyCallback {
  23.  
  24.     private var mMap: GoogleMap? = null
  25.  
  26.     private val USER_LOCATION_REQUEST_CODE = 1000
  27.  
  28.     override fun onCreate(savedInstanceState: Bundle?) {
  29.         super.onCreate(savedInstanceState)
  30.         setContentView(R.layout.activity_maps)
  31.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  32.         val mapFragment = supportFragmentManager
  33.             .findFragmentById(R.id.map) as SupportMapFragment
  34.         mapFragment.getMapAsync(this)
  35.  
  36.         requestLocationPermission()
  37.     }
  38.  
  39.     /**
  40.      * Manipulates the map once available.
  41.      * This callback is triggered when the map is ready to be used.
  42.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  43.      * we just add a marker near Sydney, Australia.
  44.      * If Google Play services is not installed on the device, the user will be prompted to install
  45.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  46.      * installed Google Play services and returned to the app.
  47.      */
  48.     override fun onMapReady(googleMap: GoogleMap) {
  49.         mMap = googleMap
  50.     }
  51.  
  52.     //ask permission
  53.     private fun requestLocationPermission() {
  54.         if (Build.VERSION.SDK_INT >= 23) {
  55.             if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
  56.                 PackageManager.PERMISSION_GRANTED) {
  57.  
  58.                 requestPermissions(
  59.                     arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
  60.                     USER_LOCATION_REQUEST_CODE
  61.                 )
  62.             }
  63.         }
  64.     }
  65.  
  66.  
  67.     fun GetPlayerLocation() {
  68.         Toast.makeText(this, "User location access on", Toast.LENGTH_LONG).show()
  69.  
  70.         var playerLocation = PlayerLocationListener()
  71.  
  72.         var locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
  73.  
  74.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 3f, playerLocation)
  75.  
  76.         var mythread = myThread()
  77.         mythread.start()
  78.     }
  79.  
  80.     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  81.  
  82.         when (requestCode) {
  83.  
  84.             USER_LOCATION_REQUEST_CODE -> {
  85.  
  86.                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  87.                     GetPlayerLocation()
  88.                 } else {
  89.                     Toast.makeText(this, "We cannot access to your location", Toast.LENGTH_LONG).show()
  90.                 }
  91.             }
  92.         }
  93.         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  94.     }
  95.  
  96.     var location: Location? = null
  97.  
  98.     // Get player location
  99.     inner class PlayerLocationListener : LocationListener {
  100.  
  101.         constructor() {
  102.             location = Location("Start")
  103.             location!!.latitude = 0.0
  104.             location!!.longitude = 0.0
  105.         }
  106.  
  107.         override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
  108.         }
  109.  
  110.         override fun onProviderEnabled(p0: String?) {
  111.         }
  112.  
  113.         override fun onProviderDisabled(p0: String?) {
  114.         }
  115.  
  116.         override fun onLocationChanged(p0: Location?) {
  117.             location = p0
  118.         }
  119.  
  120.     }
  121.  
  122.     inner class myThread : Thread {
  123.         constructor() : super(){
  124.  
  125.         }
  126.         override fun run() {
  127.  
  128.             while (true) {
  129.  
  130.                 try {
  131.                     runOnUiThread {
  132.  
  133.                         mMap!!.clear()
  134.                         val sydney = LatLng(location!!.latitude, location!!.longitude)
  135.                         mMap!!.addMarker(
  136.                             MarkerOptions().position(sydney).title("Hi!")
  137.                                 .snippet("Let's go!")
  138.                                 .icon(BitmapDescriptorFactory.fromResource(R.drawable.player)))
  139.                         mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 14f))
  140.                     }
  141.                     Thread.sleep(1000)
  142.  
  143.                 } catch (ex: Exception) {
  144.                 }
  145.             }
  146.         }
  147.  
  148.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement