Android定位功能实现
本文源码:git@github.com:sueleeyu/android-maps.git
android的定位功能有两种方式:
- 使用第三方地图sdk提供的定位功能。
- 使用sdk的Location实现,借助GPS(高精度)和网络(低精度)实现定位。
国内应用的话最好使用百度或高德sdk,较多室外场景或者数据精度要求不严的情况下可以使用原生方法。
一、google map定位
参考Android接入google地图:https://blog.csdn.net/weixin_40239288/article/details/125169826
二、使用Android的Location定位
1. 在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
2.使用LocationManager获取信息。
LocationManager提供连接本地位置服务,这些本地位置允许应用程序获取定期更新设备的地理位置,或当设备进入一个特定的地理位置接近,触发一个特定意图,使用如下:
// 1需要检查权限,否则编译报错
if (Build.VERSION.SDK_INT >= 23 && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
//2.获取位置管理器
locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
//3.获取位置提供器,GPS或是NetWork
val providers = locationManager!!.getProviders(true)
if (providers.contains(LocationManager.GPS_PROVIDER)) {
//Provider是GPS
locationProvider = LocationManager.GPS_PROVIDER
Log.v("TAG", "定位方式GPS")
} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
//Provider是Network
locationProvider = LocationManager.NETWORK_PROVIDER
Log.v("TAG", "定位方式Network")
} else {
//Provider无
Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show()
return
}
//3.获取上次的位置,一般第一次运行,此值为null
val location = locationManager!!.getLastKnownLocation(
locationProvider!!
)
if (location != null) {
Toast.makeText(
this, location.longitude.toString() + " " +
location.latitude + "", Toast.LENGTH_SHORT
).show()
Log.v(
"TAG",
"获取上次的位置-经纬度:" + location.longitude + " " + location.latitude
)
tv_longitude?.setText("经度:"+location.longitude)
tv_latitude?.setText("纬度:"+location.latitude)
//getAddress(location)
} else {
//监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
//locationListener的onLocationChanged更新数据
locationManager!!.requestLocationUpdates(
locationProvider!!,
3000,
1f,
locationListener
)
}
3.获取地址信息: Geocoder反向地理编码的使用
//获取地址信息:城市、街道等信息
private fun getAddress(location: Location?): List<Address>? {
var result: List<Address>? = null
try {
if (location != null) {
//反向地理编码
val gc = Geocoder(this, Locale.getDefault())
result = gc.getFromLocation(
location.latitude,
location.longitude, 1
)
Toast.makeText(this, "获取地址信息:$result", Toast.LENGTH_LONG).show()
Log.v("TAG", "获取地址信息:$result")
}
} catch (e: Exception) {
e.printStackTrace()
}
return result
}
三、android打包运行
1.安装运行
四、常见问题
1. java.io.IOException: grpc failed
原因为:the service is not available 服务不可用 即设备不支持Geocoder
五、参考文献
1.https://www.jianshu.com/p/fb89ab396bf8
2.本项目示例源代码:
git@github.com:sueleeyu/android-maps.git