JAVA、PHP、前端、APP、网站开发 - 开发技术学习

开发技术学习 » APP开发 » android定位

android定位

此文被围观762日期: 2021-09-05 分类 : APP开发  标签:  ···


Android-通过GPS或者网络获取当前位置 kotlin

private fun getLocation(context: Context): Location {
        val locMan = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        val checkCameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        val checkCallPhonePermission =
            ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
        if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || checkCameraPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, permission, 2)
        }
        way.text = "通过GPS定位"
        val location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER)
        if (location == null) {
            way.text = "通过网络定位"
            locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
        }
        return location
    }

    private fun getGeoByLocation(location:Location){
        longitude.text ="longitude:${location.longitude}"
        latitude.text = "latitude:${location.latitude}"
        val ge =Geocoder(this)
        var addressList =ArrayList()
        try {
            addressList = ge.getFromLocation(location.latitude,location.longitude,1) as ArrayList            detail.text = addressList.toString()
        }catch (e:IOException){
            e.printStackTrace()
        }
        if (addressList.size>0){
            address.text = "${addressList[0].getAddressLine(0)}"
        }
    }


来源:https://www.cnblogs.com/lyj348990/p/11537410.html


另外还可以使用:

https://www.jianshu.com/p/0d52ca1cb85b?utm_campaign=haruki&utm_content=note&utm_medium=seo_notes&utm_source=recommendation


站点声明:部分内容源自互联网,为传播信息之用,如有侵权,请联系我们删除。

© Copyright 2011-2024 www.kfju.com. All Rights Reserved.
超级字帖 版权所有。蜀ICP备12031064号