百度地图定位优缺点:
- 优点: 定位比较快
- 缺点: 含有so库,定位不太准
- 从百度官方sdk中拷贝库到工程中,下载地址
- 修改build.gradle文件
android {
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
implementation files('libs/BaiduLBS_Android.jar')
implementation 'com.afollestad:assent:2.3.1'
}
- 修改manifest文件
- 调用
class MainActivity : AppCompatActivity() {
private lateinit var mLocationClient: LocationClient
private val myListener = MyLocationListener()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initMap()
locateBtn.setOnClickListener { locate() }
}
private fun initMap() {
mLocationClient = LocationClient(applicationContext)
//声明LocationClient类
mLocationClient.registerLocationListener(myListener)
val option = LocationClientOption()
//可选,是否需要地址信息,默认为不需要,即参数为false
//如果开发者需要获得当前点的地址信息,此处必须为true
option.setIsNeedAddress(true)
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置,请参照类参考中LocationClientOption类的详细说明
mLocationClient.locOption = option
}
private fun locate() {
locationTv.text = ""
runWithPermissions(Permission.ACCESS_FINE_LOCATION) {
progressBar.visibility = VISIBLE
mLocationClient.start()
}
}
inner class MyLocationListener : BDAbstractLocationListener() {
override fun onReceiveLocation(location: BDLocation) {
progressBar.visibility = GONE
locationTv.text = getLocationStr(location)
}
}
}
object Utils {
/**
* 根据定位结果返回定位信息的字符串
*/
fun getLocationStr(location: BDLocation): String? {
val sb = StringBuffer()
sb.append("time : ")
/**
* 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间;
* location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变
*/
sb.append(location.time)
sb.append("\nlocType : ")// 定位类型
sb.append(location.locType)
sb.append("\nlocType description : ")// *****对应的定位类型说明*****
sb.append(location.locTypeDescription)
sb.append("\nlatitude : ")// 纬度
sb.append(location.latitude)
sb.append("\nlontitude : ")// 经度
sb.append(location.longitude)
sb.append("\nradius : ")// 半径
sb.append(location.radius)
sb.append("\nCountryCode : ")// 国家码
sb.append(location.countryCode)
sb.append("\nCountry : ")// 国家名称
sb.append(location.country)
sb.append("\ncitycode : ")// 城市编码
sb.append(location.cityCode)
sb.append("\ncity : ")// 城市
sb.append(location.city)
sb.append("\nDistrict : ")// 区
sb.append(location.district)
sb.append("\nStreet : ")// 街道
sb.append(location.street)
sb.append("\naddr : ")// 地址信息
sb.append(location.addrStr)
sb.append("\nUserIndoorState: ")// *****返回用户室内外判断结果*****
sb.append(location.userIndoorState)
sb.append("\nDirection(not all devices have value): ")
sb.append(location.direction)// 方向
sb.append("\nlocationdescribe: ")
sb.append(location.locationDescribe)// 位置语义化信息
sb.append("\nPoi: ")// POI信息
if (location.poiList != null && !location.poiList.isEmpty()) {
for (i in 0 until location.poiList.size) {
val poi = location.poiList[i] as Poi
sb.append(poi.name + ";")
}
}
if (location.locType == BDLocation.TypeGpsLocation) {// GPS定位结果
sb.append("\nspeed : ")
sb.append(location.speed)// 速度 单位:km/h
sb.append("\nsatellite : ")
sb.append(location.satelliteNumber)// 卫星数目
sb.append("\nheight : ")
sb.append(location.altitude)// 海拔高度 单位:米
sb.append("\ngps status : ")
sb.append(location.gpsAccuracyStatus)// *****gps质量判断*****
sb.append("\ndescribe : ")
sb.append("gps定位成功")
} else if (location.locType == BDLocation.TypeNetWorkLocation) {// 网络定位结果
// 运营商信息
if (location.hasAltitude()) {// *****如果有海拔高度*****
sb.append("\nheight : ")
sb.append(location.altitude)// 单位:米
}
sb.append("\noperationers : ")// 运营商信息
sb.append(location.operators)
sb.append("\ndescribe : ")
sb.append("网络定位成功")
} else if (location.locType == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ")
sb.append("离线定位成功,离线定位结果也是有效的")
} else if (location.locType == BDLocation.TypeServerError) {
sb.append("\ndescribe : ")
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因")
} else if (location.locType == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ")
sb.append("网络不同导致定位失败,请检查网络是否通畅")
} else if (location.locType == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ")
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机")
}
return sb.toString()
}
}
完整源代码
https://gitee.com/cxyzy1/geolocationDemo/tree/master/baiduDemo