Advertisement
ldmohan

MapActivity (Lab9)

Oct 31st, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 7.60 KB | None | 0 0
  1. //APY KEY  AIzaSyDk496N2Iw6SntFGRfCWAvbrA8OvlLT6J4
  2.  
  3.  
  4. //TrackLocation.kt code
  5.  
  6.  
  7. package com.example.myapplication
  8. import android.content.Context
  9. import android.content.Context.LOCATION_SERVICE
  10. import android.content.pm.PackageManager
  11. import android.location.Location
  12. import android.location.LocationListener
  13. import android.location.LocationManager
  14. import android.os.Bundle
  15. import android.util.Log
  16. import androidx.core.app.ActivityCompat
  17.  
  18. class TrackLocation(private val mContext: Context) : LocationListener {
  19.  
  20.     // Flag for GPS status
  21.     var isGPSEnabled = false
  22.     // Flag for network status
  23.     var isNetworkEnabled = false
  24.     // Flag for GPS status
  25.     var canGetLocation = false
  26.     lateinit var location: Location  // Location
  27.     private val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 10 // 10 meters
  28.     // The minimum time between updates in milliseconds
  29.     private val MIN_TIME_BW_UPDATES = (1000 * 60 * 1).toLong() // 1 minute
  30.     // Declaring a Location Manager
  31.     protected var locationManager: LocationManager? = null
  32.  
  33.     init {
  34.         getCurrentLocation()
  35.     }
  36.  
  37.     fun getCurrentLocation(): Location {
  38.         try {
  39.             locationManager = mContext.getSystemService(LOCATION_SERVICE) as LocationManager?
  40.  
  41.             isGPSEnabled = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
  42.  
  43.             isNetworkEnabled = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
  44.  
  45.             //check permission
  46.             if (ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  47.                 Log.v("TAG ","No permission Granted")
  48.             }else{
  49.  
  50.                 if (!isNetworkEnabled) {
  51.                 } else {
  52.                     this.canGetLocation = true
  53.                     locationManager!!.requestLocationUpdates(
  54.                         LocationManager.NETWORK_PROVIDER,
  55.                         MIN_TIME_BW_UPDATES,
  56.                         MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
  57.                     )
  58.                     if (locationManager != null) {
  59.                         location = locationManager!!
  60.                             .getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
  61.                     }
  62.                 }
  63.  
  64.                 if (!isGPSEnabled) {
  65.                 } else {
  66.                     this.canGetLocation = true
  67.                     // If GPS enabled, get latitude/longitude using GPS Services
  68.                     locationManager!!.requestLocationUpdates(
  69.                         LocationManager.GPS_PROVIDER,
  70.                         MIN_TIME_BW_UPDATES,
  71.                         MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
  72.                     )
  73.                     if (locationManager != null) {
  74.                         location = locationManager!!
  75.                             .getLastKnownLocation(LocationManager.GPS_PROVIDER)
  76.                     }
  77.                 }
  78.             }
  79.         } catch (e: Exception) {
  80.             e.printStackTrace()
  81.         }
  82.         return location
  83.     }
  84.  
  85.     fun canWeGetLocation(): Boolean {
  86.         return this.canGetLocation
  87.     }
  88.  
  89.     override fun onLocationChanged(location: Location) {}
  90.     override fun onProviderDisabled(provider: String) {}
  91.     override fun onProviderEnabled(provider: String) {}
  92.     override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
  93.  
  94. }
  95.  
  96.  
  97.  
  98. //****************************MainActivity Code***********************************************************************
  99.  
  100.  
  101. package com.example.myapplication
  102.  
  103. import android.Manifest
  104. import android.content.pm.PackageManager
  105. import android.location.Geocoder
  106. import android.location.Location
  107. import androidx.appcompat.app.AppCompatActivity
  108. import android.os.Bundle
  109. import android.util.Log
  110. import androidx.core.app.ActivityCompat
  111.  
  112. import com.google.android.gms.maps.CameraUpdateFactory
  113. import com.google.android.gms.maps.GoogleMap
  114. import com.google.android.gms.maps.OnMapReadyCallback
  115. import com.google.android.gms.maps.SupportMapFragment
  116. import com.google.android.gms.maps.model.LatLng
  117. import com.google.android.gms.maps.model.MarkerOptions
  118. import com.google.android.gms.maps.model.PolylineOptions
  119.  
  120. class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMyLocationClickListener {
  121.  
  122.     private lateinit var mMap: GoogleMap
  123.     var latitude=0.0
  124.     var longitude=0.0
  125.     override fun onCreate(savedInstanceState: Bundle?) {
  126.         super.onCreate(savedInstanceState)
  127.         setContentView(R.layout.activity_maps)
  128.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  129.  
  130.         if (ActivityCompat.checkSelfPermission(
  131.                 this,
  132.                 Manifest.permission.ACCESS_FINE_LOCATION
  133.             ) !== PackageManager.PERMISSION_GRANTED
  134.         ) {
  135.             ActivityCompat.requestPermissions(
  136.                 this,
  137.                 arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
  138.                 1
  139.             )
  140.             return
  141.         }
  142.  
  143.  
  144.         val mapFragment = supportFragmentManager
  145.             .findFragmentById(R.id.map) as SupportMapFragment
  146.         mapFragment.getMapAsync(this)
  147.     }
  148.  
  149.     /**
  150.      * Manipulates the map once available.
  151.      * This callback is triggered when the map is ready to be used.
  152.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  153.      * we just add a marker near Sydney, Australia.
  154.      * If Google Play services is not installed on the device, the user will be prompted to install
  155.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  156.      * installed Google Play services and returned to the app.
  157.      */
  158.     override fun onMapReady(googleMap: GoogleMap) {
  159.         mMap = googleMap
  160.  
  161.  
  162.         //Using GPS****************************
  163.        /* val loc=getCurrentLocarion()
  164.         val currentLocation = LatLng(loc.latitude,loc.longitude)
  165.         mMap.addMarker(MarkerOptions().position(currentLocation).title("You are here "))
  166.         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,15F))*/
  167. //****************************************************
  168.  
  169.         //Using OnMyLocationClickListener
  170.  
  171.         mMap.setMyLocationEnabled(true)  //adding the location button
  172.         mMap.setOnMyLocationClickListener(this)
  173.  
  174.  
  175.  
  176.     }
  177.  
  178.  
  179.     fun getCurrentLocarion(): Location {
  180.         lateinit var location: Location
  181.         var currentGPS = TrackLocation(this)
  182.         if(currentGPS.canWeGetLocation()) {
  183.             location = currentGPS.getCurrentLocation()
  184.         }
  185.         return location
  186.     }
  187.  
  188. //click on the location to get the current coordinates
  189.     override fun onMyLocationClick(location: Location) {
  190.         latitude=location.latitude
  191.         longitude=location.longitude
  192.         var nLocation=LatLng(latitude,longitude)
  193.         val geocoder = Geocoder(this)
  194.         val addresses = geocoder.getFromLocation(latitude, longitude, 1) //get the address
  195.         Log.i("Add",addresses.toString())
  196.         val loc=addresses[0].getAddressLine(0)
  197.         mMap.addMarker(MarkerOptions().position(nLocation).title("You are here $loc"))
  198.         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(nLocation,15F))
  199.         drawPolyLine()
  200.     }
  201.  
  202.  
  203.     fun drawPolyLine(){
  204.  
  205.         var start = LatLng(58.385254, 26.725064 )
  206.         var end=LatLng(latitude,longitude)
  207.         mMap.addPolyline(PolylineOptions().add(start,end))
  208.     }
  209.  
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement