高德地图定位优缺点:
- 优点:
自带地址解析,对比百度地图,不含额外的so库 - 缺点:
需要引入高德的sdk库
集成方法
- 修改build.gradle文件
implementation 'com.amap.api:location:latest.integration'
implementation 'com.afollestad:assent:2.3.1'
- 修改manifest文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.cxyzy.demo">
<!--用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!--用于访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="f79c5a4588621faaa90df09b736cae53" />
<!-- 定位需要的服务 -->
<service android:name="com.amap.api.location.APSService" />
</application>
</manifest>
- 调用
class MainActivity : AppCompatActivity() {
private lateinit var mLocationClient: AMapLocationClient
private var mLocationOption = AMapLocationClientOption()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initMap()
locateBtn.setOnClickListener { locate() }
}
private fun initMap() {
mLocationClient = AMapLocationClient(applicationContext)
mLocationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
mLocationOption.isOnceLocation = true
//获取最近3s内精度最高的一次定位结果:
//设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
mLocationOption.isOnceLocationLatest = true
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.isNeedAddress = true
//关闭缓存机制
mLocationOption.isLocationCacheEnable = false
//声明定位回调监听器
val locationListener = AMapLocationListener { loc ->
progressBar.visibility = GONE
if (null != loc) {
//解析定位结果
val result = Utils.getLocationStr(loc)
locationTv.text = result
} else {
locationTv.text = getString(R.string.locate_failed)
}
}
//设置定位回调监听
mLocationClient.setLocationListener(locationListener)
}
private fun locate() {
runWithPermissions(Permission.ACCESS_FINE_LOCATION) {
if (isLocationProviderEnabled(this@MainActivity)) {
locationTv.text = ""
progressBar.visibility = VISIBLE
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation()
} else {
Utils.showAlert("本应用需要获取地理位置,请打开获取位置的开关", this)
}
}
}
}
object Utils {
/**
* 根据定位结果返回定位信息的字符串
*/
fun getLocationStr(location: AMapLocation): String? {
val sb = StringBuffer()
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if (location.errorCode == 0) {
sb.append("定位成功" + "\n")
sb.append("定位类型: " + location.locationType + "\n")
sb.append("经 度 : " + location.longitude + "\n")
sb.append("纬 度 : " + location.latitude + "\n")
sb.append("精 度 : " + location.accuracy + "米" + "\n")
sb.append("提供者 : " + location.provider + "\n")
sb.append("速 度 : " + location.speed + "米/秒" + "\n")
sb.append("角 度 : " + location.bearing + "\n")
// 获取当前提供定位服务的卫星个数
sb.append("星 数 : " + location.satellites + "\n")
sb.append("国 家 : " + location.country + "\n")
sb.append("省 : " + location.province + "\n")
sb.append("市 : " + location.city + "\n")
sb.append("城市编码 : " + location.cityCode + "\n")
sb.append("区 : " + location.district + "\n")
sb.append("区域 码 : " + location.adCode + "\n")
sb.append("地 址 : " + location.address + "\n")
sb.append("兴趣点 : " + location.poiName + "\n")
//定位完成的时间
sb.append("定位时间: " + formatUTC(location.time, "yyyy-MM-dd HH:mm:ss") + "\n")
} else {
//定位失败
sb.append("定位失败" + "\n")
sb.append("错误码:" + location.errorCode + "\n")
sb.append("错误信息:" + location.errorInfo + "\n")
sb.append("错误描述:" + location.locationDetail + "\n")
}
//定位之后的回调时间
sb.append("回调时间: " + formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n")
return sb.toString()
}
private fun formatUTC(l: Long, strPattern: String): String {
val sdf = SimpleDateFormat(strPattern, Locale.CHINA)
return sdf.format(l)
}
/**
* 判断是否开启了GPS或网络定位开关
*
* @return
*/
fun isLocationProviderEnabled(context: Context): Boolean {
var result = false
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
result = true
}
return result
}
fun showAlert(message: String, context: Context) {
val alertDialog = AlertDialog.Builder(context).setMessage(message).setCancelable(false)
.setPositiveButton(android.R.string.ok)
{ dialog, _ ->
dialog.dismiss()
}
.setNegativeButton(android.R.string.cancel) { dialog, _ -> dialog.dismiss() }
.create()
alertDialog.show()
}
}
完整源代码
https://gitee.com/cxyzy1/geolocationDemo/tree/master/amapDemo
网友评论