修改定位,修改提前看板

This commit is contained in:
squallzhjch
2023-04-28 16:03:40 +08:00
parent 3dc650a76b
commit 894732ee45
23 changed files with 435 additions and 164 deletions

View File

@@ -1,14 +1,14 @@
package com.navinfo.collect.library.map
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
data class GeoPoint(
var latitude: Double = 0.0,
var longitude: Double = 0.0
) : Parcelable {
fun toGeometry(): String {
return "POINT($longitude $latitude)"
}
}
//package com.navinfo.collect.library.map
//
//import android.os.Parcelable
//import kotlinx.parcelize.Parcelize
//
//@Parcelize
//data class GeoPoint(
// var latitude: Double = 0.0,
// var longitude: Double = 0.0
//) : Parcelable {
// fun toGeometry(): String {
// return "POINT($longitude $latitude)"
// }
//}

View File

@@ -1,14 +1,20 @@
package com.navinfo.collect.library.map
import android.content.Context
import android.util.Log
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.navinfo.collect.library.data.entity.NiLocation
import com.navinfo.collect.library.data.handler.DataNiLocationHandler
import com.navinfo.collect.library.map.NIMapView.OnMapClickListener
import com.navinfo.collect.library.map.handler.*
import com.navinfo.collect.library.map.maphandler.MeasureLayerHandler
import com.navinfo.collect.library.map.handler.MeasureLayerHandler
import com.navinfo.collect.library.map.handler.ViewportHandler
import com.navinfo.collect.library.system.Constant
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import org.oscim.core.GeoPoint
/**
* 地图控制器
@@ -25,19 +31,35 @@ class NIMapController {
lateinit var viewportHandler: ViewportHandler
lateinit var measureLayerHandler: MeasureLayerHandler
fun init(context: AppCompatActivity, mapView: NIMapView, options: NIMapOptions? = null, mapPath: String, tracePath: String) {
val onMapClickFlow = MutableSharedFlow<GeoPoint>()
fun init(
context: AppCompatActivity,
mapView: NIMapView,
options: NIMapOptions? = null,
mapPath: String,
tracePath: String
) {
Constant.MAP_PATH = mapPath
layerManagerHandler = LayerManagerHandler(context, mapView, tracePath)
locationLayerHandler = LocationLayerHandler(context, mapView)
animationHandler = AnimationHandler(context, mapView)
markerHandle = MarkHandler(context, mapView)
lineHandler = LineHandler(context, mapView)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
lineHandler = LineHandler(context, mapView)
}
polygonHandler = PolygonHandler(context, mapView)
viewportHandler = ViewportHandler(context, mapView)
measureLayerHandler = MeasureLayerHandler(context, mapView)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
measureLayerHandler = MeasureLayerHandler(context, mapView)
}
mMapView = mapView
mMapView.setOnMapClickListener {
context.lifecycleScope.launch {
onMapClickFlow.emit(it)
}
}
mapView.setOptions(options)
mMapView.vtmMap.viewport().maxZoomLevel = Constant.MAX_ZOOM // 设置地图的最大级别
}

View File

@@ -1,5 +1,6 @@
package com.navinfo.collect.library.map
import com.navinfo.collect.library.system.Constant
import org.json.JSONObject
@@ -8,6 +9,7 @@ data class NIMapOptions(
val showZoomControl: Boolean = true, //是否显示zoom按钮
val zoomLevel: Double = 13.0, /// 地图比例尺初始级别
val coordinate: NICoordinate = NICoordinate(39.907375, 116.391349),
val maxZoom: Int = Constant.MAX_ZOOM
) {
companion object {
fun fromJson(json: String): NIMapOptions {

View File

@@ -1,6 +1,7 @@
package com.navinfo.collect.library.map;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@@ -121,7 +122,7 @@ public final class NIMapView extends RelativeLayout {
*
* @param point
*/
void onMapClick(com.navinfo.collect.library.map.GeoPoint point);
void onMapClick(GeoPoint point);
/**
* 地图内 Poi 单击事件回调函数
@@ -358,6 +359,7 @@ public final class NIMapView extends RelativeLayout {
}
MapPosition mapPosition = getVtmMap().getMapPosition();
mapPosition.setZoom(options.getZoomLevel());
getVtmMap().viewport().setMaxZoomLevel(options.getMaxZoom());
mapPosition.setPosition(options.getCoordinate().getLatitude(), options.getCoordinate().getLongitude());
getVtmMap().animator().animateTo(100, mapPosition);
}
@@ -819,7 +821,9 @@ public final class NIMapView extends RelativeLayout {
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
if (layoutParams.getRules() != null) {
for (int i = 0; i < layoutParams.getRules().length; i++) {
layoutParams.removeRule(i);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
layoutParams.removeRule(i);
}
}
}
switch (position) {
@@ -926,7 +930,7 @@ public final class NIMapView extends RelativeLayout {
GeoPoint geoPoint = mMap.viewport().fromScreenPoint(e.getX(), e.getY());
if (g instanceof Gesture.Tap) { // 单击事件
if (mapClickListener != null) {
mapClickListener.onMapClick(new com.navinfo.collect.library.map.GeoPoint(geoPoint.getLatitude(), geoPoint.getLongitude()));
mapClickListener.onMapClick(geoPoint);
}
} else if (g instanceof Gesture.DoubleTap) { // 双击
if (mapDoubleClickListener != null) {

View File

@@ -20,11 +20,11 @@ abstract class BaseHandler(context: AppCompatActivity, mapView: NIMapView) {
mMapView.vtmMap.layers().remove(layer)
}
fun setOnMapClickListener(listener: NIMapView.OnMapClickListener) {
mMapView.setOnMapClickListener(listener)
}
fun removeOnMapClickListener() {
mMapView.setOnMapClickListener(null)
}
// fun setOnMapClickListener(listener: NIMapView.OnMapClickListener) {
// mMapView.setOnMapClickListener(listener)
// }
//
// fun removeOnMapClickListener() {
// mMapView.setOnMapClickListener(null)
// }
}

View File

@@ -24,7 +24,7 @@ import java.io.File
class LayerManagerHandler(context: AppCompatActivity, mapView: NIMapView,tracePath: String) : BaseHandler(context, mapView) {
private var baseGroupLayer // 用于盛放所有基础底图的图层组,便于统一管理
: GroupLayer? = null
protected val mTracePath:String = tracePath
private val mTracePath:String = tracePath
/**
* 轨迹渲染图层

View File

@@ -137,13 +137,12 @@ class LineHandler(context: AppCompatActivity, mapView: NIMapView) : BaseHandler(
fun showLine(geometry: String) {
try {
mDefaultPathLayer.clearPath()
mDefaultPathLayer.setPoints(GeometryTools.getGeoPoints(geometry))
mDefaultPathLayer.isEnabled = true
} catch (e: Exception) {
Toast.makeText(mContext, "高亮路线失败 ${e.message}", Toast.LENGTH_SHORT).show()
}
val g = GeometryTools.getGeoPoints(geometry)
mDefaultPathLayer.setPoints(g)
mDefaultPathLayer.isEnabled = true
}
fun removeLine() {

View File

@@ -1,27 +1,34 @@
package com.navinfo.collect.library.map.handler
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.location.LocationClientOption.LocationMode
import com.navinfo.collect.library.data.entity.NiLocation
import com.navinfo.collect.library.map.GeoPoint
import com.navinfo.collect.library.map.NIMapView
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import org.oscim.core.GeoPoint
import org.oscim.layers.LocationLayer
class LocationLayerHandler(context: AppCompatActivity, mapView: NIMapView) : BaseHandler(context, mapView) {
class LocationLayerHandler(context: AppCompatActivity, mapView: NIMapView) :
BaseHandler(context, mapView) {
private var mCurrentLocation: BDLocation? = null
private var bFirst = true
private val mLocationLayer: LocationLayer = LocationLayer(mMapView.vtmMap)
private lateinit var locationClient: LocationClient
private lateinit var niLocationListener: NiLocationListener
// private var niLocationListener: NiLocationListener by lazy{
//
// }
val niLocationFlow = MutableSharedFlow<NiLocation>(5)
init {
///添加定位图层到地图,[NIMapView.LAYER_GROUPS.NAVIGATION] 是最上层layer组
@@ -47,20 +54,29 @@ class LocationLayerHandler(context: AppCompatActivity, mapView: NIMapView) : Bas
//更多结果信息获取说明请参照类参考中BDLocation类中的说明
//获取纬度信息
val latitude = it.latitude
// val latitude = it.latitude
//获取经度信息
val longitude = it.longitude
// val longitude = it.longitude
//获取定位精度默认值为0.0f
val radius = it.radius
// val radius = it.radius
//获取经纬度坐标类型以LocationClientOption中设置过的坐标类型为准
val coorType = it.coorType
// val coorType = it.coorType
//获取定位类型、定位错误返回码具体信息可参照类参考中BDLocation类中的说明
val errorCode = it.locType
mCurrentLocation = it
mLocationLayer.setPosition(it.latitude, it.longitude, it.radius)
Log.e("qj","location==${it.longitude}==errorCode===$errorCode===${it.locTypeDescription}")
if(niLocationListener!=null){
getCurrentNiLocation()?.let { it1 -> niLocationListener.call(it1) }
// Log.e(
// "qj",
// "location==${it.longitude}==errorCode===$errorCode===${it.locTypeDescription}"
// )
// if (niLocationListener != null) {
getCurrentNiLocation()?.let { it1 ->
mContext.lifecycleScope.launch {
niLocationFlow.emit(it1)
}
// }// niLocationListener.call(it1) }
}
//第一次定位成功显示当前位置
if (this.bFirst) {
@@ -107,7 +123,7 @@ class LocationLayerHandler(context: AppCompatActivity, mapView: NIMapView) : Bas
locationClient.locOption = locationOption
} catch (e: Throwable) {
Toast.makeText(mContext, "定位初始化失败 $e", Toast.LENGTH_SHORT)
Log.e("qj","定位初始化失败$e")
Log.e("qj", "定位初始化失败$e")
}
}
@@ -160,8 +176,8 @@ class LocationLayerHandler(context: AppCompatActivity, mapView: NIMapView) : Bas
//获取当前定位对象
fun getCurrentNiLocation(): NiLocation? {
if(mCurrentLocation!=null){
val niLocation:NiLocation = NiLocation()
if (mCurrentLocation != null) {
val niLocation: NiLocation = NiLocation()
niLocation.longitude = mCurrentLocation!!.longitude
niLocation.latitude = mCurrentLocation!!.latitude
niLocation.direction = mCurrentLocation!!.direction.toDouble()
@@ -186,10 +202,10 @@ class LocationLayerHandler(context: AppCompatActivity, mapView: NIMapView) : Bas
return null
}
//设置定位回调
fun setNiLocationListener(listener: NiLocationListener){
niLocationListener = listener
}
// //设置定位回调
// fun setNiLocationListener(listener: NiLocationListener) {
// niLocationListener = listener
// }
}
/**
@@ -205,7 +221,7 @@ private class MyLocationListener(callback: (BDLocation) -> Unit) : BDAbstractLoc
/**
* 实现定位回调
*/
public class NiLocationListener(callback: (NiLocation) -> Unit){
class NiLocationListener(callback: (NiLocation) -> Unit) {
val call = callback;
fun onReceiveLocation(location: NiLocation) {
call(location)

View File

@@ -10,7 +10,6 @@ import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.lifecycleScope
import com.navinfo.collect.library.R
import com.navinfo.collect.library.data.entity.QsRecordBean
import com.navinfo.collect.library.map.GeoPoint
import com.navinfo.collect.library.map.NIMapView
import com.navinfo.collect.library.map.cluster.ClusterMarkerItem
import com.navinfo.collect.library.map.cluster.ClusterMarkerRenderer
@@ -27,6 +26,7 @@ import org.oscim.android.canvas.AndroidBitmap
import org.oscim.backend.CanvasAdapter
import org.oscim.backend.canvas.Bitmap
import org.oscim.backend.canvas.Paint
import org.oscim.core.GeoPoint
import org.oscim.layers.marker.*
import org.oscim.layers.marker.ItemizedLayer.OnItemGestureListener
import org.oscim.map.Map
@@ -90,9 +90,10 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
}
)
addLayer(mDefaultMarkerLayer, NIMapView.LAYER_GROUPS.OPERATE);
//初始化之间数据图层
initQsRecordDataLayer()
addLayer(mDefaultMarkerLayer, NIMapView.LAYER_GROUPS.OPERATE);
// 设置矢量图层均在12级以上才显示
mMapView.vtmMap.events.bind(Map.UpdateListener { e, mapPosition ->
if (e == Map.SCALE_EVENT) {
@@ -127,13 +128,13 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
val marker = MarkerItem(
tempTitle,
description,
org.oscim.core.GeoPoint(geoPoint.latitude, geoPoint.longitude)
geoPoint
)
mDefaultMarkerLayer.addItem(marker);
mMapView.vtmMap.updateMap(true)
} else {
marker.description = description
marker.geoPoint = org.oscim.core.GeoPoint(geoPoint.latitude, geoPoint.longitude)
marker.geoPoint = geoPoint
mDefaultMarkerLayer.removeItem(marker)
mDefaultMarkerLayer.addItem(marker)
mMapView.vtmMap.updateMap(true)

View File

@@ -1,19 +1,17 @@
package com.navinfo.collect.library.map.maphandler
package com.navinfo.collect.library.map.handler
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.os.Build
import android.text.TextPaint
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.navinfo.collect.library.R
import com.navinfo.collect.library.map.NIMapView
import com.navinfo.collect.library.map.handler.BaseHandler
import com.navinfo.collect.library.map.layers.NIPolygonLayer
import com.navinfo.collect.library.utils.DistanceUtil
import com.navinfo.collect.library.utils.GeometryTools
import com.navinfo.collect.library.utils.StringUtil.Companion.createUUID
import org.oscim.android.canvas.AndroidBitmap
import org.oscim.backend.CanvasAdapter
import org.oscim.backend.canvas.Bitmap
@@ -30,6 +28,7 @@ import org.oscim.layers.vector.geometries.Style
import org.oscim.map.Map
import java.math.BigDecimal
@RequiresApi(Build.VERSION_CODES.M)
open class MeasureLayerHandler(context: AppCompatActivity, mapView: NIMapView) :
BaseHandler(context, mapView), Map.UpdateListener {

View File

@@ -418,7 +418,7 @@ public class GeometryTools {
Geometry startGeo = createGeometry(startGeoPoint);
Geometry endGeo = createGeometry(endGeoPoint);
double d = startGeo.distance(endGeo);
return d * 100000;
return convertDistanceToDegree(d,startGeoPoint.getLatitude());
}
return 0;