Code Monkey home page Code Monkey logo

locationservice's Introduction

LocationService

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.app.Service
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.os.IBinder
import android.provider.Settings
import android.util.Log
import androidx.core.app.ActivityCompat


@SuppressLint("Registered")
class Gps(private val mContext: Context) : Service(),
   LocationListener {

   // flag for GPS status
   var isGPSEnabled = false

   // flag for network status
   var isNetworkEnabled = false

   // flag for GPS status
   private var canGetLocation = false
   private var loca : Location? = null
   private var lat = 0.0
   private var longe= 0.0

   // Declaring a Location Manager
   private var locationManager: LocationManager? = null
   fun getLocation(): Location? {
       try {
           locationManager =
               mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager

           // getting GPS status
           isGPSEnabled = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)

           // getting network status
           isNetworkEnabled = locationManager!!
               .isProviderEnabled(LocationManager.NETWORK_PROVIDER)
           if (!isGPSEnabled && !isNetworkEnabled) {
               // no network provider is enabled
           } else {
               canGetLocation = true
               // First get location from Network Provider
               if (isNetworkEnabled) {
                   //check the network permission
                   if (ActivityCompat.checkSelfPermission(
                           mContext,
                           Manifest.permission.ACCESS_FINE_LOCATION
                       ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                           mContext,
                           Manifest.permission.ACCESS_COARSE_LOCATION
                       ) != PackageManager.PERMISSION_GRANTED
                   ) {
                       ActivityCompat.requestPermissions(
                           (mContext as Activity),
                           arrayOf(
                               Manifest.permission.ACCESS_FINE_LOCATION,
                               Manifest.permission.ACCESS_COARSE_LOCATION
                           ),
                           101
                       )
                   }
                   locationManager!!.requestLocationUpdates(
                       LocationManager.NETWORK_PROVIDER,
                       MIN_TIME_BW_UPDATES,
                       MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
                   )
                   Log.d("Network", "Network")
                   if (locationManager != null) {
                       loca = locationManager!!
                           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
                       if (loca != null) {
                           lat = loca!!.latitude
                           longe = loca!!.longitude
                       }
                   }
               }

               // if GPS Enabled get lat/long using GPS Services
               if (isGPSEnabled) {
                   if (loca == null) {
                       //check the network permission
                       if (ActivityCompat.checkSelfPermission(
                               mContext,
                               Manifest.permission.ACCESS_FINE_LOCATION
                           ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                               mContext,
                               Manifest.permission.ACCESS_COARSE_LOCATION
                           ) != PackageManager.PERMISSION_GRANTED
                       ) {
                           ActivityCompat.requestPermissions(
                               (mContext as Activity),
                               arrayOf(
                                   Manifest.permission.ACCESS_FINE_LOCATION,
                                   Manifest.permission.ACCESS_COARSE_LOCATION
                               ),
                               101
                           )
                       }
                       locationManager!!.requestLocationUpdates(
                           LocationManager.GPS_PROVIDER,
                           MIN_TIME_BW_UPDATES,
                           MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
                       )
                       Log.d("GPS Enabled", "GPS Enabled")
                       if (locationManager != null) {
                           loca = locationManager!!
                               .getLastKnownLocation(LocationManager.GPS_PROVIDER)
                           if (loca != null) {
                               lat = loca!!.latitude
                               longe = loca!!.longitude
                           }
                       }
                   }
               }
           }
       } catch (e: Exception) {
           e.printStackTrace()
       }
       return loca
   }

   /**
    * Stop using GPS listener
    * Calling this function will stop using GPS in your app
    */
   fun stopUsingGPS() {
       if (locationManager != null) {
           locationManager!!.removeUpdates(this@Gps)
       }
   }

   /**
    * Function to get latitude
    */
   fun getLatitude(): Double {
       if (loca != null) {
           lat = loca!!.latitude
       }

       // return latitude
       return lat
   }

   /**
    * Function to get longitude
    */
   fun getLongitude(): Double {
       if (loca != null) {
           longe = loca!!.longitude
       }

       // return longitude
       return longe
   }

   /**
    * Function to check GPS/wifi enabled
    * @return boolean
    */
   fun canGetLocation(): Boolean {
       return canGetLocation
   }

   /**
    * Function to show settings alert dialog
    * On pressing Settings button will lauch Settings Options
    */
   fun showSettingsAlert() {
       val alertDialog= AlertDialog.Builder(mContext)

       // Setting Dialog Title
       alertDialog.setTitle("GPS is settings")

       // Setting Dialog Message
       alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?")

       // On pressing Settings button
       alertDialog.setPositiveButton("Settings",
           DialogInterface.OnClickListener { dialog, which ->
               val intent =
                   Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
               mContext.startActivity(intent)
           })

       // on pressing cancel button
       alertDialog.setNegativeButton("Cancel",
           DialogInterface.OnClickListener { dialog, which -> dialog.cancel() })
       alertDialog.show()
   }

   override fun onLocationChanged(location: Location) {
       Log.e("oo",location.latitude.toString())
   }
   override fun onProviderDisabled(provider: String) {}
   override fun onProviderEnabled(provider: String) {}
   override fun onStatusChanged(
       provider: String,
       status: Int,
       extras: Bundle
   ) {
   }

   override fun onBind(arg0: Intent): IBinder? {
       return null
   }

   companion object {
       // The minimum distance to change Updates in meters
       private const val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 10 // 10 meters

       // The minimum time between updates in milliseconds
       private const val MIN_TIME_BW_UPDATES = 1000 * 60 * 1 // 1 minute
           .toLong()
   }

   init {
       getLocation()
   }
}

locationservice's People

Contributors

noor1yasser9 avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.