(电商中需要定位需求)百度地图定位sdk集成,及城市列表展示,选择,搜索,26字母快速定位
下面集成步骤:
主要还是依靠百度定位文档一步一步
1,先去百度开发者平台,创建你的应用,
根据你的sha1和包名获取app在百度定位中的AK
如下图:
![在这里插入图片描述](javascript:void(0)
添加SDK(jar + so)
下载Android定位SDK并解压,将libs中的jar和so放置到工程中相应的位置。
注意,Android定位SDK提供了多种CPU架构的so文件(jar通用,只有一个),开发者可根据实际使用需求,放置所需so到对应的工程文件夹内。
,配置build.gradle文件
配置build.gradle文件,注意设置sourceSets。
sourceSets{
main{
jniLibs.srcDir ‘libs’
jni.srcDirs = [] //disable automatic ndk-build
}
}
然后带上你的AK去你的manifest中注册应用
,添加定位权限
使用定位SDK,需在AndroidManifest.xml文件中Application标签中声明service组件,每个App拥有自己单独的定位service,代码如下:
除添加service组件外,使用定位SDK还需添加如下权限:
看不懂可进入[百度定位sdk,studio配置]看文档
下面代码中可直接定位:
在哪里定位 直接调用即可:
/**定位**/
BDLocationUtils.instance.startLocation(this, object : BDLocationUtils.ResultCallback {
override fun fail() {
location.text = (resources.getText(R.string.location_default))
BDLocationUtils.instance.stopLocation()
showSelectAddrDialog()
}
override fun onPermissionError() {
location.text = (resources.getText(R.string.location_default))
BDLocationUtils.instance.stopLocation()
showSelectAddrDialog()
}
override fun success(city: String) {
var addr = ""
if (city.isEmpty()) {
location.text = (resources.getText(R.string.location_default))
showSelectAddrDialog()
} else {
if (city.contains("市")) {
addr = city.substringBefore("市", city)
}
location?.text = if (addr.length > 2) addr.substring(0, 2) + "..." else addr
BDLocationUtils.instance.stopLocation()
hideSelectAddrDialog()
}
}
})
BDLocationUtils 定位类—
package com.example.administrator.kotlintest.util
import android.Manifest
import android.annotation.SuppressLint
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.example.baselibrary.MyApplication
import com.example.baselibrary.widgets.TLog
import com.tbruyelle.rxpermissions2.RxPermissions
/**
* Created by HaoBoy on 2018/8/26.
* 定位
*/
class BDLocationUtils : BDAbstractLocationListener() {
private var rxPermissions: RxPermissions? = null
// 关于定位的参数
private var mLocClient: LocationClient? = null
private var mOption: LocationClientOption? = null
private var callback: ResultCallback? = null
//单例
companion object {
@JvmStatic
val instance: BDLocationUtils by lazy { BDLocationUtils() }
}
/**
* @return DefaultLocationClientOption setScanSpan=0是只定位一次
*/
private val defaultLocationClientOption: LocationClientOption
get() {
if (mOption == null) {
mOption = LocationClientOption()
mOption!!.locationMode = LocationClientOption.LocationMode.Hight_Accuracy
mOption!!.setCoorType("bd09ll")
mOption!!.setScanSpan(0)
mOption!!.setIsNeedAddress(true)
mOption!!.setIgnoreKillProcess(true)
mOption!!.isOpenGps = true
mOption!!.setIsNeedAltitude(false)
}
return mOption!!
}
fun startLocation(context: FragmentActivity, callback: ResultCallback) {
this.callback = callback
rxPermissions = RxPermissions(context)
location(rxPermissions!!)
}
fun startLocation(context: Fragment, callback: ResultCallback) {
this.callback = callback
rxPermissions = RxPermissions(context)
location(rxPermissions!!)
}
@SuppressLint("CheckResult")
fun location(rxPermissions: RxPermissions) {
rxPermissions
.requestEach(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION)
.subscribe({}, {}, {
if (rxPermissions.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)
&& rxPermissions.isGranted(Manifest.permission.ACCESS_COARSE_LOCATION)) {
if (mLocClient == null) {
// 定位初始化
mLocClient = LocationClient(MyApplication.cxt)
mLocClient!!.registerLocationListener(this)
mLocClient!!.locOption = defaultLocationClientOption
}
mLocClient!!.start()
} else {
callback?.onPermissionError()
}
})
}
/**
* 再不需要定位的时候调用(必须)
*/
fun stopLocation() {
if (mLocClient != null) {
mLocClient!!.unRegisterLocationListener(instance)
mLocClient!!.stop()
mLocClient = null
}
}
fun restartLocation() {
if(mLocClient != null)
if(!mLocClient!!.isStarted) mLocClient!!.restart()
}
override fun onReceiveLocation(bdLocation: BDLocation?) {
if (null != bdLocation) {
TLog.i("定位完成类型:-------" + bdLocation.locType)
if (callback != null) {
if (bdLocation.city != null) {
callback!!.success(bdLocation.city)
} else {
callback!!.fail()
}
}
} else {
callback!!.fail()
}
}
interface ResultCallback {
fun success(city: String)//定位成功的返回
fun onPermissionError()//未允许定位权限返回
fun fail()//定位失败
}
}
/**获取城市列表**/
private fun getCity() {
httpManager.doHttpDeal(this,
httpManager.createService(CommonService::class.java).getAllCity().bindToLifecycle(this),
httpOnNextListener)
}`
城市适配器展示
`import android.content.Context
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.TextView
import com.example.administrator.kotlintest.R
import com.example.administrator.kotlintest.adapter.BaseRvAdapter
import com.example.administrator.kotlintest.entity.CityEntity
/**
*@author : HaoBoy
*@date : 2018/8/24
*@description :主目录adapter
**/
class CitySelectAdapter(list: ArrayList<CityEntity.DataBean.ListBean>, context: Context) :
BaseRvAdapter<CityEntity.DataBean.ListBean>(list, R.layout.item_select_location_city, context) {
override fun onBindView(holder: Companion.BaseRvHolder, data: CityEntity.DataBean.ListBean) {
//分组标记
val sectionTag = holder.findViewById<TextView>(R.id.tvItemCitySectionName)
if(data.sectionTag != null){
sectionTag.text = data.sectionTag
sectionTag.visibility = View.VISIBLE
}else{
sectionTag.visibility = View.GONE
}
val cityName = holder.findViewById<TextView>(R.id.tvItemCityName)
cityName.text = data.name
if(data.isLocationCity){
cityName.setTextColor(ContextCompat.getColor(context,R.color.color_orange))
}else{
cityName.setTextColor(ContextCompat.getColor(context,R.color.text_color_light))
}
}
}
源码可去github源码上下载,观看效果