Location 是一个通过 Android 自带的 LocationManager 来实现的定位功能

Overview

Location

Image

Download MavenCentral JitPack CI CircleCI API License Blog QQGroup

Location 是一个通过 Android 自带的 LocationManager 来实现的定位功能。

之所以写这个库的主要原因还需要从下面的场景说起:在开发某个App的过程当中,可能有个需求需要用到定位功能,但是又不那么重要; 这个时候如果选择使用第三方的定位服务,需要先去申请相关的appKey和集成定位相关的SDK,显得太繁琐了。杀鸡焉用宰牛刀,我们只是需要一个简单的定位功能,使用自带的实现不香吗?因此就有了 Location

Gif 展示

Image

引入

Gradle:

  1. 在Project的 build.gradle 里面添加远程仓库
allprojects {
    repositories {
        //...
        mavenCentral()
    }
}
  1. 在Module的 build.gradle 里面添加引入依赖项
implementation 'com.github.jenly1314:location:1.0.0'

示例

主要方法调用示例

     
    //初始化实例
    val locationClient = LocationClient(this)

    //可根据具体需求设置定位配置参数
    val locationOption = locationClient.getLocationOption()
        .setAccuracy(Criteria.ACCURACY_FINE)//设置位置精度:高精度
        .setMinTime(10000)//设置位置更新最小时间间隔(单位:毫秒); 默认间隔:10000毫秒,最小间隔:1000毫秒
        .setOnceLocation(false)//设置是否只定位一次,默认为 false,当设置为 true 时,则只定位一次后,会自动停止定位

    //设置定位配置参数
    locationClient.setLocationOption(locationOption)

    //----------------------------------

    //设置定位监听
    locationClient.setOnLocationListener(object: OnLocationListener(){
        override fun onLocationChanged(location: Location) {
            //TODO 位置信息
        }

    })

    //----------------------------------

    //开始定位(建议先校验是否有定位权限,然后开始定位)
    locationClient.startLocation()

    //----------------------------------
    
    //停止定位
    locationClient.stopLocation()

完整示例

locationClient.getLocationOption().setOnceLocation(isChecked) } binding.cbLastKnownLocation.setOnCheckedChangeListener { buttonView, isChecked -> locationClient.getLocationOption().setLastKnownLocation(isChecked) } //可根据具体需求设置定位配置参数(这里只列出一些主要的参数) val locationOption = locationClient.getLocationOption() .setAccuracy(Criteria.ACCURACY_FINE)//设置位置精度:高精度 .setPowerRequirement(Criteria.POWER_LOW) //设置电量消耗:低电耗 .setMinTime(10000)//设置位置更新最小时间间隔(单位:毫秒); 默认间隔:10000毫秒,最小间隔:1000毫秒 .setMinDistance(0)//设置位置更新最小距离(单位:米);默认距离:0米 .setOnceLocation(false)//设置是否只定位一次,默认为 false,当设置为 true 时,则只定位一次后,会自动停止定位 .setLastKnownLocation(true)//设置是否获取最后一次缓存的已知位置,默认为 true //设置定位配置参数 locationClient.setLocationOption(locationOption) //设置定位监听 locationClient.setOnLocationListener(object: OnLocationListener(){ override fun onLocationChanged(location: Location) { //位置信息 Log.d(TAG,"onLocationChanged(location = ${location})") val builder = StringBuilder() builder.append("Longitude: \t${location.longitude}\n") .append("Latitude: \t${location.latitude}\n") //根据坐标经纬度获取位置地址信息(WGS-84坐标系) val list = geocoder.getFromLocation(location.latitude,location.longitude,1) if(list.isNotEmpty()){ builder.append("Address: \t${list[0].getAddressLine(0)}\n") } builder.append("Time: \t${simpleDateFormat.format(Date(location.time))}\n") .append("Provider: \t${location.provider}\n") binding.tvLocation.text = builder.toString() } override fun onProviderEnabled(provider: String) { super.onProviderEnabled(provider) Log.d(TAG,"onProviderEnabled(provider = ${provider})") } override fun onProviderDisabled(provider: String) { super.onProviderDisabled(provider) Log.d(TAG,"onProviderDisabled(provider = ${provider})") } }) //设置异常监听 locationClient.setOnExceptionListener(object : OnExceptionListener{ override fun onException(@LocationErrorCode errorCode: Int, e: Exception) { //定位出现异常 Log.w(TAG,"onException(errorCode = ${errorCode}, e = ${e})") binding.tvLocation.text = e.message } }) } override fun onResume() { super.onResume() if(!isPermissionGranted){ if(checkLocationPermission()){ isPermissionGranted = true binding.tvLocation.text = "" } } } /** * 开始定位 */ private fun startLocation(){ if(locationClient.isStarted()){//如果已经开始定位,则先停止定位 locationClient.stopLocation() } binding.tvLocation.text = "Start location..." locationClient.startLocation() } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array , grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if(requestCode == REQ_LOCATION_PERMISSION){ isPermissionGranted = verifyPermissions(grantResults) if(isPermissionGranted){//已授权 startLocation() }else{//未授权 binding.tvLocation.text = "Location permission has not been granted." showMissingPermissionDialog() } } } /** * 检测位置权限 */ private fun checkLocationPermission(): Boolean{ return ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED } /** * 校验授权结果 */ private fun verifyPermissions(grantResults: IntArray): Boolean{ for(result in grantResults){ if(result != PackageManager.PERMISSION_GRANTED){ return false } } return true } /** * 显示未授权提示对话框 */ private fun showMissingPermissionDialog(){ val builder = AlertDialog.Builder(this) builder.setMessage("Location permission has not been granted.") .setNegativeButton("Cancel" ) { dialog, which -> } .setPositiveButton("Setting" ) { dialog, which -> startAppSettings() } builder.show() } /** * 跳转到 App 的设置详情界面 */ private fun startAppSettings(){ val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) intent.data = Uri.parse("package:${packageName}") startActivity(intent) } } ">
class MainActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val locationClient by lazy {
        LocationClient(this)
    }

    private val geocoder by lazy {
        Geocoder(this)
    }

    private val simpleDateFormat by lazy {
        SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    }

    private var isPermissionGranted = false

    companion object{
        const val TAG = "MainActivity"
        const val REQ_LOCATION_PERMISSION = 0x01
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.btnStartLocation.setOnClickListener {
            isPermissionGranted = checkLocationPermission()
            if(isPermissionGranted){
                startLocation()
            }else{
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), REQ_LOCATION_PERMISSION)
            }
        }

        binding.btnStopLocation.setOnClickListener {
            locationClient.stopLocation()
            binding.tvLocation.text = ""
        }

        binding.cbOnceLocation.setOnCheckedChangeListener { buttonView, isChecked ->
            locationClient.getLocationOption().setOnceLocation(isChecked)
        }

        binding.cbLastKnownLocation.setOnCheckedChangeListener { buttonView, isChecked ->
            locationClient.getLocationOption().setLastKnownLocation(isChecked)
        }

        //可根据具体需求设置定位配置参数(这里只列出一些主要的参数)
        val locationOption = locationClient.getLocationOption()
            .setAccuracy(Criteria.ACCURACY_FINE)//设置位置精度:高精度
            .setPowerRequirement(Criteria.POWER_LOW) //设置电量消耗:低电耗
            .setMinTime(10000)//设置位置更新最小时间间隔(单位:毫秒); 默认间隔:10000毫秒,最小间隔:1000毫秒
            .setMinDistance(0)//设置位置更新最小距离(单位:米);默认距离:0米
            .setOnceLocation(false)//设置是否只定位一次,默认为 false,当设置为 true 时,则只定位一次后,会自动停止定位
            .setLastKnownLocation(true)//设置是否获取最后一次缓存的已知位置,默认为 true
        //设置定位配置参数
        locationClient.setLocationOption(locationOption)

        //设置定位监听
        locationClient.setOnLocationListener(object: OnLocationListener(){
            override fun onLocationChanged(location: Location) {
                //位置信息
                Log.d(TAG,"onLocationChanged(location = ${location})")
                val builder = StringBuilder()
                builder.append("Longitude: \t${location.longitude}\n")
                    .append("Latitude: \t${location.latitude}\n")

                //根据坐标经纬度获取位置地址信息(WGS-84坐标系)
                val list = geocoder.getFromLocation(location.latitude,location.longitude,1)
                if(list.isNotEmpty()){
                    builder.append("Address: \t${list[0].getAddressLine(0)}\n")
                }

                builder.append("Time: \t${simpleDateFormat.format(Date(location.time))}\n")
                    .append("Provider: \t${location.provider}\n")

                binding.tvLocation.text = builder.toString()
            }

            override fun onProviderEnabled(provider: String) {
                super.onProviderEnabled(provider)
                Log.d(TAG,"onProviderEnabled(provider = ${provider})")
            }

            override fun onProviderDisabled(provider: String) {
                super.onProviderDisabled(provider)
                Log.d(TAG,"onProviderDisabled(provider = ${provider})")
            }

        })

        //设置异常监听
        locationClient.setOnExceptionListener(object : OnExceptionListener{
            override fun onException(@LocationErrorCode errorCode: Int, e: Exception) {
                //定位出现异常
                Log.w(TAG,"onException(errorCode = ${errorCode}, e = ${e})")
                binding.tvLocation.text = e.message
            }
        })
    }

    override fun onResume() {
        super.onResume()
        if(!isPermissionGranted){
            if(checkLocationPermission()){
                isPermissionGranted = true
                binding.tvLocation.text = ""
            }
        }
    }

    /**
     * 开始定位
     */
    private fun startLocation(){
        if(locationClient.isStarted()){//如果已经开始定位,则先停止定位
            locationClient.stopLocation()
        }
        binding.tvLocation.text = "Start location..."
        locationClient.startLocation()

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(requestCode == REQ_LOCATION_PERMISSION){
            isPermissionGranted = verifyPermissions(grantResults)
            if(isPermissionGranted){//已授权
                startLocation()
            }else{//未授权
                binding.tvLocation.text = "Location permission has not been granted."
                showMissingPermissionDialog()
            }
        }
    }

    /**
     * 检测位置权限
     */
    private fun checkLocationPermission(): Boolean{
        return ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    }

    /**
     * 校验授权结果
     */
    private fun verifyPermissions(grantResults: IntArray): Boolean{
        for(result in grantResults){
            if(result != PackageManager.PERMISSION_GRANTED){
                return false
            }
        }
        return true
    }

    /**
     * 显示未授权提示对话框
     */
    private fun showMissingPermissionDialog(){
        val builder = AlertDialog.Builder(this)
        builder.setMessage("Location permission has not been granted.")
            .setNegativeButton("Cancel"
            ) { dialog, which ->

            }
            .setPositiveButton("Setting"
            ) { dialog, which ->
                startAppSettings()
            }
        builder.show()
    }


    /**
     * 跳转到 App 的设置详情界面
     */
    private fun startAppSettings(){
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.parse("package:${packageName}")
        startActivity(intent)
    }

}

在你项目的 AndroidManifest.xml 清单文件中添加定位相关权限

">
    
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

更多使用详情,请查看Demo中的源码使用示例或直接查看API帮助文档

版本记录

v1.0.0:2021-9-9

  • Location初始版本

赞赏

如果你喜欢Location,或感觉Location帮助到了你,可以点右上角“Star”支持一下,你的支持就是我的动力,谢谢 😃

你也可以扫描下面的二维码,请作者喝杯咖啡

关于我

Name: Jenly

Email: jenly1314#gmail.com / jenly1314#vip.qq.com

CSDN: jenly121

CNBlogs: jenly

GitHub: jenly1314

Gitee: jenly1314

加入QQ群: 20867961

You might also like...
This Andoird project is about classifying garbage pictures using a CNN-based Tensorflowlite model and fetching location using GPS to mark its area on the map.
This Andoird project is about classifying garbage pictures using a CNN-based Tensorflowlite model and fetching location using GPS to mark its area on the map.

Grbage_Detector_And_Locator Description As per some of the recent reports published, in India, the pollution levels are increasing at a colossal pace

An application to find out the weather according to the current location of the phone

Weather App An application to find out the weather according to the current loca

Real time gps location based alarming system tracking the road accident and providing medical assitance plus concern from near by police station.
Real time gps location based alarming system tracking the road accident and providing medical assitance plus concern from near by police station.

Real time gps location based alarming system tracking the road accident and providing medical assitance plus concern from near by police station.

LocationPhoneMode - The app will ask the user to allow it to track the user location
LocationPhoneMode - The app will ask the user to allow it to track the user location

LocationPhoneMode The app will ask the user to allow it to track the user locati

Easliy to mange location in your app

Location_Helper Easliy to mange location in your app technology used [dagger hilt, coroutines, Mvvm,Channel,flow, clean architecture, clean code,gps p

A powerful library for easy implementation of HMS Location Kit.

AdvancedLocation A powerful library for easy implementation of HMS Location Kit. 💙 Request location with couple lines of code (no more boilerplate) C

Fused Sidecar App: Fused Location Provider sidecar app for ATAK
Fused Sidecar App: Fused Location Provider sidecar app for ATAK

Fused Sidecar App Fused Sidecar App is a Fused Location Provider for the Android Team Awareness Kit (ATAK). Unlike the out-of-the-box GPS Location Pro

android-delicious Delicious Android is an Android app which helps you access and save bookmarks via Delicious. It's available over at Google Play.
android-delicious Delicious Android is an Android app which helps you access and save bookmarks via Delicious. It's available over at Google Play.

Delicious Android Delicious Android is an Android app which helps you access and save bookmarks via Delicious. It's available over at Google Play. Fea

Android cutout screen support Android P. Android O support huawei, xiaomi, oppo and vivo.

CutoutScreenSupport Android cutout screen support Android P. Android O support huawei, xiaomi, oppo and vivo. Usage whether the mobile phone is cutout

Releases(1.0.0)
Owner
Jenly
人生如戏
Jenly
A Todo list app with location reminders that remind the user to do something when he reaches a specific location.

A Todo list app with location reminders that remind the user to do something when he reaches a specific location.

Virginia Redondo 0 Jan 6, 2022
Xposed module which will set location where you want without effect mock location.

GPS Setter Support/Discussion: XDA thread As most of GPS spoof app not working anymore coz some are old and some are not proper implement with current

Android1500 73 Dec 28, 2022
Android application that implements location and network related Android APIs

Location and network data collection Location and network data collection with Android's telephonyManager class. Introduction Technologies Android's A

jquk 0 Oct 31, 2021
Project BlueWeather is an android app that lists the closest locations to you and shows the 7-day weather forecast for the location you select.

Project BLUEWEATHER Description Project BlueWeather is a weather forecast application for android. It lists the locations closest to you. It then prov

Burak Unutmaz 4 May 12, 2021
Realtime SOS Android Application. Location (GPS + Cellular Network) tracing application by alerting guardians of the User.

WomenSaftey Women Safety Android Application: Realtime SOS Android Application. Designed a Location (GPS + Cellular Network) tracing application by al

jatin kasera 6 Nov 19, 2022
A news app made using android studio in Java with features like favourite news, Location detector for local news, and especially made with HUAWEI APIs

HuaweiGlobalNewsApp A news app made using android studio in Java with features like favourite news, Location detector for local news, and especially m

Christian Imanuel Hadiwidjaja 1 Oct 30, 2021
SkillHub - A location based native android app built for the University of Nigeria

SkillHub SkillHub is a location based native android app built for the Universit

Nnamani Chinonso   1 Jan 23, 2022
📰👓Android App that fetches fresh news based on your language and location

News App that fetches fresh news based on your language and location. You can share and browse news in one click! How to install You can open it with

Valeria 1 Feb 21, 2022
☁️🌤🌧☀ Weather Android application that gives you the weather information of your saved location or any region you are looking for☁️🌤🌧☀

☁️????☀ Weather Android application that gives you the weather information of your saved location or any region you are looking for☁️????☀

Nisa Efendioğlu 4 Apr 6, 2022
This app to track realtime location

✨ hacktoberfest2021 ?? ?? Contributing to hacktoberfest 2021 Welcome to hacktoberfest 2021 Public Repository. ????‍?? A month-long celebration from Oc

aldhykohar 1 Nov 16, 2021