Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //APY KEY AIzaSyDk496N2Iw6SntFGRfCWAvbrA8OvlLT6J4
- //TrackLocation.kt code
- package com.example.myapplication
- import android.content.Context
- import android.content.Context.LOCATION_SERVICE
- import android.content.pm.PackageManager
- import android.location.Location
- import android.location.LocationListener
- import android.location.LocationManager
- import android.os.Bundle
- import android.util.Log
- import androidx.core.app.ActivityCompat
- class TrackLocation(private val mContext: Context) : LocationListener {
- // Flag for GPS status
- var isGPSEnabled = false
- // Flag for network status
- var isNetworkEnabled = false
- // Flag for GPS status
- var canGetLocation = false
- lateinit var location: Location // Location
- private val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 10 // 10 meters
- // The minimum time between updates in milliseconds
- private val MIN_TIME_BW_UPDATES = (1000 * 60 * 1).toLong() // 1 minute
- // Declaring a Location Manager
- protected var locationManager: LocationManager? = null
- init {
- getCurrentLocation()
- }
- fun getCurrentLocation(): Location {
- try {
- locationManager = mContext.getSystemService(LOCATION_SERVICE) as LocationManager?
- isGPSEnabled = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
- isNetworkEnabled = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
- //check permission
- 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) {
- Log.v("TAG ","No permission Granted")
- }else{
- if (!isNetworkEnabled) {
- } else {
- this.canGetLocation = true
- locationManager!!.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,
- MIN_TIME_BW_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
- )
- if (locationManager != null) {
- location = locationManager!!
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
- }
- }
- if (!isGPSEnabled) {
- } else {
- this.canGetLocation = true
- // If GPS enabled, get latitude/longitude using GPS Services
- locationManager!!.requestLocationUpdates(
- LocationManager.GPS_PROVIDER,
- MIN_TIME_BW_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
- )
- if (locationManager != null) {
- location = locationManager!!
- .getLastKnownLocation(LocationManager.GPS_PROVIDER)
- }
- }
- }
- } catch (e: Exception) {
- e.printStackTrace()
- }
- return location
- }
- fun canWeGetLocation(): Boolean {
- return this.canGetLocation
- }
- override fun onLocationChanged(location: Location) {}
- override fun onProviderDisabled(provider: String) {}
- override fun onProviderEnabled(provider: String) {}
- override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
- }
- //****************************MainActivity Code***********************************************************************
- package com.example.myapplication
- import android.Manifest
- import android.content.pm.PackageManager
- import android.location.Geocoder
- import android.location.Location
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.util.Log
- import androidx.core.app.ActivityCompat
- import com.google.android.gms.maps.CameraUpdateFactory
- import com.google.android.gms.maps.GoogleMap
- import com.google.android.gms.maps.OnMapReadyCallback
- import com.google.android.gms.maps.SupportMapFragment
- import com.google.android.gms.maps.model.LatLng
- import com.google.android.gms.maps.model.MarkerOptions
- import com.google.android.gms.maps.model.PolylineOptions
- class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMyLocationClickListener {
- private lateinit var mMap: GoogleMap
- var latitude=0.0
- var longitude=0.0
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_maps)
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- if (ActivityCompat.checkSelfPermission(
- this,
- Manifest.permission.ACCESS_FINE_LOCATION
- ) !== PackageManager.PERMISSION_GRANTED
- ) {
- ActivityCompat.requestPermissions(
- this,
- arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
- 1
- )
- return
- }
- val mapFragment = supportFragmentManager
- .findFragmentById(R.id.map) as SupportMapFragment
- mapFragment.getMapAsync(this)
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- override fun onMapReady(googleMap: GoogleMap) {
- mMap = googleMap
- //Using GPS****************************
- /* val loc=getCurrentLocarion()
- val currentLocation = LatLng(loc.latitude,loc.longitude)
- mMap.addMarker(MarkerOptions().position(currentLocation).title("You are here "))
- mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,15F))*/
- //****************************************************
- //Using OnMyLocationClickListener
- mMap.setMyLocationEnabled(true) //adding the location button
- mMap.setOnMyLocationClickListener(this)
- }
- fun getCurrentLocarion(): Location {
- lateinit var location: Location
- var currentGPS = TrackLocation(this)
- if(currentGPS.canWeGetLocation()) {
- location = currentGPS.getCurrentLocation()
- }
- return location
- }
- //click on the location to get the current coordinates
- override fun onMyLocationClick(location: Location) {
- latitude=location.latitude
- longitude=location.longitude
- var nLocation=LatLng(latitude,longitude)
- val geocoder = Geocoder(this)
- val addresses = geocoder.getFromLocation(latitude, longitude, 1) //get the address
- Log.i("Add",addresses.toString())
- val loc=addresses[0].getAddressLine(0)
- mMap.addMarker(MarkerOptions().position(nLocation).title("You are here $loc"))
- mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(nLocation,15F))
- drawPolyLine()
- }
- fun drawPolyLine(){
- var start = LatLng(58.385254, 26.725064 )
- var end=LatLng(latitude,longitude)
- mMap.addPolyline(PolylineOptions().add(start,end))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement