Commit 4169eef4 authored by p x's avatar p x
Browse files

封装定位数据

parent 10c3f320
......@@ -7,10 +7,12 @@ import com.sd.maplibrary.MAP_TYPE
import com.sd.maplibrary.MSDKInitializer
import com.sd.maplibrary.OnSdkInitCb
import com.sd.maplibrary.bean.MSLatLng
import com.sd.maplibrary.bean.MSLocBean
import com.sd.maplibrary.bean.PoiSearchRes
import com.sd.maplibrary.bean.RegeocodeRes
import com.sd.maplibrary.core.MSCanvesInMap
import com.sd.maplibrary.core.MSGestures
import com.sd.maplibrary.core.MSGpsLocation
import com.sd.maplibrary.core.MSMethodAdv
import com.sd.maplibrary.core.MSPoiSearch
import com.sd.maplibrary.core.MSRegeoCode
......@@ -50,7 +52,10 @@ class MainActivity : AppCompatActivity() {
private fun jumpAct() {
binding.btSeartch.setOnClickListener {
//到搜索页面
}
binding.btLoc.setOnClickListener {
testGpsLoc()
}
}
......@@ -127,6 +132,17 @@ class MainActivity : AppCompatActivity() {
})
}
//定位
fun testGpsLoc() {
MSGpsLocation.initLoc(this)
MSGpsLocation.setOnMsGpsLoc(object : MSGpsLocation.OnMsGpsLoc{
override fun onMsGpsLoc(mSLocBean: MSLocBean) {
println("---------mSLocBean = ${mSLocBean}")
}
})
MSGpsLocation.starLoc()
}
fun initMap() {
MSDKInitializer.initializeMap(this, MAP_TYPE.AMAP, object : OnSdkInitCb {
......@@ -139,5 +155,10 @@ class MainActivity : AppCompatActivity() {
})
}
override fun onDestroy() {
super.onDestroy()
MSGpsLocation.stopLoc()
}
}
\ No newline at end of file
......@@ -13,7 +13,13 @@
android:id="@+id/bt_seartch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"/>
android:text="搜索" />
<Button
android:id="@+id/bt_loc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="定位" />
</LinearLayout>
......
package com.sd.maplibrary.bean
/***定位数据回调**/
class MSLocBean {
//当前经纬度
var lat = 0.0
var lng = 0.0
//速度
var speed = 0f
var bearing = 0f
var accuracy = 0f
//高程
var altitude = 0.0
companion object {
val instance: MSLocBean by lazy { MSLocBean() }
}
}
\ No newline at end of file
package com.sd.maplibrary.core
import android.content.Context
import android.os.Bundle
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode
import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol
import com.amap.api.location.AMapLocationListener
import com.minedata.minenavi.location.MineLocation
import com.minedata.minenavi.location.MineLocationListener
import com.minedata.minenavi.location.MineLocationManager
import com.minedata.minenavi.location.MineLocationOptions
import com.minedata.minenavi.mapdal.CoordType
import com.sd.maplibrary.MAP_TYPE
import com.sd.maplibrary.MSDKInitializer
import com.sd.maplibrary.bean.MSLocBean
/******/
object MSGpsLocation {
// 设置四维定位参数,并开始定位。
private val options: MineLocationOptions by lazy {
MineLocationOptions().apply {
setCoordType(CoordType.GCJ02)
setGpsInterval(1000)//GPS定位更新时间,最小1000
setNetWorkInterval(3000)//WiFi定位更新时间,最小3000
setStationInterval(5000)//基站定位更新时间,最小500
}
}
// 设置高德定位参数
private val locationOption: AMapLocationClientOption by lazy {
val mOption = AMapLocationClientOption()
mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒
mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可选,设置逆地理信息的语言,默认值为默认语言(根据所在地区选择语言)
return@lazy mOption
}
private var locationClient: AMapLocationClient? = null
private var onMsGpsLoc: OnMsGpsLoc? = null
/***初始化监听定位**/
fun initLoc(context: Context) {
when (MSDKInitializer.getMapType()) {
MAP_TYPE.MINE -> {
MineLocationManager.getInstance()
.init(context, MineLocationManager.LocationSource.all)
MineLocationManager.getInstance().addListener(mineLocationListener)
}
MAP_TYPE.AMAP -> {
locationClient = AMapLocationClient(context)
//设置定位参数
locationClient!!.setLocationOption(locationOption)
// 设置定位监听
locationClient!!.setLocationListener(locationListener)
}
}
}
fun setOnMsGpsLoc(onMsGpsLoc: OnMsGpsLoc) {
this.onMsGpsLoc = onMsGpsLoc
}
/***
* 开启定位
*/
fun starLoc() {
when (MSDKInitializer.getMapType()) {
MAP_TYPE.MINE -> {
MineLocationManager.getInstance().start(options)
}
MAP_TYPE.AMAP -> {
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
//启动定位
locationClient?.startLocation()
}
}
}
/****停止定位***/
fun stopLoc() {
when (MSDKInitializer.getMapType()) {
MAP_TYPE.MINE -> {
MineLocationManager.getInstance().removeAllListener()
MineLocationManager.getInstance().stop()
MineLocationManager.getInstance().cleanup()
}
MAP_TYPE.AMAP -> {
locationClient?.onDestroy()
locationClient = null
}
}
}
/****清理资源***/
fun cleans() {
when (MSDKInitializer.getMapType()) {
MAP_TYPE.MINE -> {
MineLocationManager.getInstance().cleanup()
}
MAP_TYPE.AMAP -> {
}
}
}
//----------------自定义回调---------
interface OnMsGpsLoc {
fun onMsGpsLoc(mSLocBean: MSLocBean)
}
//---------------四维回调---------------
private var mineLocationListener = object : MineLocationListener {
override fun onSimLocationChanged(location: MineLocation?) {
}
override fun onLocationChanged(location: MineLocation?) {
// println("-------四维定位改变 = ${location?.latitude} curLng=${location?.longitude} speed=${location?.speed}")
var loc = MSLocBean.instance.apply {
lat = location?.latitude ?: 0.0
lng = location?.longitude ?: 0.0
speed = location?.speed ?: 0f
bearing = location?.bearing ?: 0f
accuracy = location?.accuracy ?: 0f
altitude = location?.altitude ?: 0.0
}
onMsGpsLoc?.onMsGpsLoc(loc)
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
}
//-------高德回调------
var locationListener: AMapLocationListener = object : AMapLocationListener {
override fun onLocationChanged(location: AMapLocation?) {
if (null != location) {
val sb = StringBuffer()
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if (location.getErrorCode() == 0) {
// println("-------高德定位改变 = ${location?.latitude} curLng=${location?.longitude} speed=${location?.speed}")
var loc = MSLocBean.instance.apply {
lat = location?.latitude ?: 0.0
lng = location?.longitude ?: 0.0
speed = location?.speed ?: 0f
bearing = location?.bearing ?: 0f
accuracy = location?.accuracy ?: 0f
altitude = location?.altitude ?: 0.0
}
onMsGpsLoc?.onMsGpsLoc(loc)
}
} else {
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment