地图依赖重构

This commit is contained in:
wangcongtao
2020-12-13 11:04:44 +08:00
parent 6fee5deaca
commit 564b1356a1
59 changed files with 1033 additions and 626 deletions

View File

@@ -2,12 +2,6 @@ package com.zhidao.roadcondition.model
import android.os.Parcel
import android.os.Parcelable
import com.amap.api.maps.model.LatLng
fun Informations.toLatLng(): LatLng {
return LatLng(lat, lon)
}
class Informations(
var type: Int,

View File

@@ -1,15 +1,9 @@
package com.zhidao.roadcondition.model
import com.amap.api.maps.model.LatLng
import com.amap.api.services.core.LatLonPoint
import com.mogo.map.MogoLatLng
fun LocationInfo.toLatLng(): LatLng {
return LatLng(latitude,longitude)
}
fun LocationInfo.toLatLngPoint():LatLonPoint{
return LatLonPoint(latitude,longitude)
fun LocationInfo.toLatLngPoint():MogoLatLng{
return MogoLatLng(latitude,longitude)
}
class LocationInfo {

View File

@@ -1,47 +1,47 @@
package com.zhidao.roadcondition.util
import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.geocoder.*
import com.mogo.commons.AbsMogoApplication
import com.mogo.map.MogoLatLng
import com.mogo.map.location.MogoLocation
import com.mogo.map.search.geo.IMogoGeoSearchListener
import com.mogo.map.search.geo.MogoRegeocodeAddress
import com.mogo.map.search.geo.MogoRegeocodeResult
import com.mogo.map.search.geo.query.MogoRegeocodeQuery
import com.mogo.module.common.MogoApisHandler
import com.zhidao.roadcondition.model.LocationInfo
private fun RegeocodeAddress.toLocInfo(
address: RegeocodeAddress,
latlngPoint: LatLonPoint
private fun MogoRegeocodeAddress.toLocInfo(
address: MogoRegeocodeAddress,
latlngPoint: MogoLatLng
): LocationInfo {
return LocationInfo(
address.province,
address.city,
address.cityCode,
address.district,
address.adCode,
address.roads[0].name,
latlngPoint.longitude,
latlngPoint.latitude,
address.formatAddress,
System.currentTimeMillis()
address.province,
address.city,
address.cityCode,
address.district,
address.adCode,
address.roads[0].name,
latlngPoint.lon,
latlngPoint.lat,
address.formatAddress,
System.currentTimeMillis()
)
}
private fun AMapLocation.toLocInfo(location: AMapLocation): LocationInfo {
private fun MogoLocation.toLocInfo(location: MogoLocation): LocationInfo {
return LocationInfo(
location.province,
location.city,
location.cityCode,
location.district,
location.adCode,
location.street,
location.longitude,
location.latitude,
location.address,
location.time,
location.bearing
location.province,
location.cityName,
location.cityCode,
location.district,
location.adCode,
location.street,
location.longitude,
location.latitude,
location.address,
location.time,
location.bearing
)
}
@@ -69,89 +69,33 @@ class LocationUtil private constructor() {
private var init: Boolean = false
private var mContext = AbsMogoApplication.getApp().applicationContext
private var locationClient: AMapLocationClient? = null
private var locationOption: AMapLocationClientOption? = null
private var speedListener: SpeedListener? = null
private var locationInfo: LocationInfo? = null
private var speed: Float = 0.0f
fun initLocation() {
//初始化client
locationOption = getDefaultOption()
locationClient = AMapLocationClient(mContext)
//设置定位参数
locationClient!!.setLocationOption(locationOption)
// 设置定位监听
locationClient!!.setLocationListener(locationListener)
init = true
startLocation()
}
private fun getDefaultOption(): AMapLocationClientOption {
val mOption = AMapLocationClientOption()
mOption.locationMode =
AMapLocationClientOption.AMapLocationMode.Hight_Accuracy//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.isGpsFirst = true//可选设置是否gps优先只在高精度模式下有效。默认关闭
mOption.httpTimeOut = 30000//可选设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.interval = 5000//可选设置定位间隔。默认为2秒
mOption.isNeedAddress = true//可选设置是否返回逆地理地址信息。默认是true
mOption.isOnceLocation = false//可选设置是否单次定位。默认是false
mOption.isOnceLocationLatest = false//可选设置是否等待wifi刷新默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP)//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.isSensorEnable = true//可选设置是否使用传感器。默认是false
mOption.isWifiScan =
true //可选设置是否开启wifi扫描。默认为true如果设置为false会同时停止主动刷新停止以后完全依赖于系统刷新定位位置可能存在误差
mOption.isLocationCacheEnable = false //可选设置是否使用缓存定位默认为true
mOption.geoLanguage =
AMapLocationClientOption.GeoLanguage.DEFAULT//可选,设置逆地理信息的语言,默认值为默认语言(根据所在地区选择语言)
return mOption
}
private fun startLocation() {
if (checkInit()) {
// 设置定位参数
locationClient!!.setLocationOption(locationOption)
// 启动定位
locationClient!!.startLocation()
}
}
fun setOnMapLocationChangedListener(listener: OnMapLocationChangedListener) {
this.listener = listener
}
/**
* 定位监听
*/
private var locationListener: AMapLocationListener = AMapLocationListener { location ->
if (null != location) {
locationInfo = LocationInfo()
locationInfo!!.longitude = location.longitude
locationInfo!!.latitude = location.latitude
locationInfo!!.address = location.address
locationInfo!!.time = location.time
locationInfo!!.provinceName = location.province
locationInfo!!.cityName = location.city
locationInfo!!.cityCode = location.cityCode
locationInfo!!.areaName = location.district
locationInfo!!.areaCode = location.adCode
locationInfo!!.street = location.street
locationInfo!!.direction = location.bearing
listener?.onChanged(locationInfo!!)
speed = location.speed
speedListener?.onSpeedGet(location.speed)
} else {
Log.d(TAG, "定位失败 -> location is null")
}
}
private fun checkInit(): Boolean {
return if (!init) {
initLocation()
init
} else {
true
MogoApisHandler.getInstance().apis.mapServiceApi.getSingletonLocationClient(mContext).addLocationListener {location->
if (null != location) {
locationInfo = LocationInfo()
locationInfo!!.longitude = location.longitude
locationInfo!!.latitude = location.latitude
locationInfo!!.address = location.address
locationInfo!!.time = location.time
locationInfo!!.provinceName = location.province
locationInfo!!.cityName = location.cityName
locationInfo!!.cityCode = location.cityCode
locationInfo!!.areaName = location.district
locationInfo!!.areaCode = location.adCode
locationInfo!!.street = location.street
locationInfo!!.direction = location.bearing
listener?.onChanged(locationInfo!!)
speed = location.speed
speedListener?.onSpeedGet(location.speed)
} else {
Log.d(TAG, "定位失败 -> location is null")
}
}
}
@@ -159,40 +103,40 @@ class LocationUtil private constructor() {
return if (null != locationInfo) {
locationInfo!!
} else {
if (null == locationClient) {
locationClient = AMapLocationClient(mContext)
}
val location = locationClient!!.lastKnownLocation
val location = MogoApisHandler.getInstance().apis.mapServiceApi.getSingletonLocationClient(mContext).lastKnowLocation
location.toLocInfo(location)
}
}
//如果获取到的location address为空可以通过高德逆地理编码获得
fun geoCodeLocation(
latlngPoint: LatLonPoint,
latlngPoint: MogoLatLng,
locGeoCode: (((locInfo: LocationInfo) -> Unit)),
onError: ((msg: String) -> Unit)
) {
var geocoderSearch = GeocodeSearch(AbsMogoApplication.getApp().applicationContext)
var regeocodeQuery = RegeocodeQuery(latlngPoint, 200f, GeocodeSearch.AMAP)
var geocoderSearch = MogoApisHandler.getInstance().apis.mapServiceApi.getGeoSearch(AbsMogoApplication.getApp().applicationContext)
var regeocodeQuery = MogoRegeocodeQuery()
regeocodeQuery.latlngType = ""
regeocodeQuery.point = latlngPoint
regeocodeQuery.radius = 200
geocoderSearch.getFromLocationAsyn(regeocodeQuery)
geocoderSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
if (p1 == 1000) {
geocoderSearch.setGeoSearchListener(object : IMogoGeoSearchListener {
override fun onRegeocodeSearched(regeocodeResult: MogoRegeocodeResult?) {
super.onRegeocodeSearched(regeocodeResult)
if( regeocodeResult == null ){
trackUploadGeo(3);
onError.invoke("geoCode")
} else {
trackUploadGeo(2);
var regeocodeAddress = p0?.regeocodeAddress
var regeocodeAddress = regeocodeResult?.regeocodeAddress
regeocodeAddress?.let {
var locInfo = regeocodeAddress.toLocInfo(regeocodeAddress, latlngPoint)
locGeoCode.invoke(locInfo)
}
} else {
trackUploadGeo(3);
onError.invoke("geoCode -> $p1")
}
}
override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {
}
})
}