代码优化
This commit is contained in:
@@ -2,6 +2,7 @@ package com.mogo.module.common.drawer;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.commons.AbsMogoApplication;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
@@ -13,7 +14,6 @@ import com.mogo.module.common.R;
|
||||
import com.mogo.module.common.constants.DataTypes;
|
||||
import com.mogo.realtime.entity.ADASRecognizedResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -92,7 +92,20 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
|
||||
continue;
|
||||
}
|
||||
|
||||
final long start = System.currentTimeMillis();
|
||||
double[] matchedPoint = matchRoad( recognizedListResult.lon,
|
||||
recognizedListResult.lat,
|
||||
recognizedListResult.heading,
|
||||
recognizedListResult.dataAccuracy == 1
|
||||
);
|
||||
Log.i("match-road-timer", "cost " + (System.currentTimeMillis() - start) + "ms");
|
||||
if ( matchedPoint != null ) {
|
||||
recognizedListResult.lon = matchedPoint[0];
|
||||
recognizedListResult.lat = matchedPoint[1];
|
||||
}
|
||||
|
||||
IMogoMarker marker = mAdasRecognizedMarkersCaches.remove( uniqueKey );
|
||||
|
||||
ADASRecognizedResult lastPosition = mLastPositions.put( uniqueKey, recognizedListResult );
|
||||
if ( marker == null || marker.isDestroyed() ) {
|
||||
marker = drawAdasRecognizedDataMarker( recognizedListResult );
|
||||
@@ -101,17 +114,14 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
|
||||
}
|
||||
}
|
||||
newAdasRecognizedMarkersCaches.put( uniqueKey, marker );
|
||||
if ( lastPosition != null && !DebugConfig.isNotSmooth() ) {
|
||||
List< MogoLatLng > points = new ArrayList<>();
|
||||
if ( lastPosition != null ) {
|
||||
MogoLatLng endLatLon = new MogoLatLng( recognizedListResult.lat, recognizedListResult.lon );
|
||||
points.add( new MogoLatLng( lastPosition.lat, lastPosition.lon ) );
|
||||
points.add( endLatLon );
|
||||
long interval = recognizedListResult.systemTime - lastPosition.systemTime;
|
||||
if ( interval < 45 ) {
|
||||
interval = 45;
|
||||
}
|
||||
interval -= 25;
|
||||
marker.startSmoothInMs( points, interval );
|
||||
marker.addDynamicAnchorPosition( endLatLon, interval );
|
||||
} else {
|
||||
marker.setRotateAngle( ( ( float ) recognizedListResult.heading ) );
|
||||
marker.setPosition( recognizedListResult.lat, recognizedListResult.lon );
|
||||
|
||||
@@ -9,7 +9,9 @@ import android.text.TextUtils;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mogo.cloud.commons.utils.CoordinateUtils;
|
||||
import com.mogo.commons.AbsMogoApplication;
|
||||
import com.mogo.map.MogoLatLng;
|
||||
import com.mogo.map.marker.IMogoMarker;
|
||||
import com.mogo.module.common.MogoApisHandler;
|
||||
import com.mogo.module.common.constants.AdasRecognizedType;
|
||||
@@ -177,4 +179,22 @@ class BaseDrawer {
|
||||
}
|
||||
mMarkerCachesResMd5Values.put( id, md5 );
|
||||
}
|
||||
|
||||
/**
|
||||
* 道路匹配到车道中心点
|
||||
*
|
||||
* @param lon
|
||||
* @param lat
|
||||
* @param angle
|
||||
* @param isRtk
|
||||
* @return
|
||||
*/
|
||||
protected double[] matchRoad( double lon, double lat, double angle, boolean isRtk ) {
|
||||
double amap[] = CoordinateUtils.transformGcj02toWgs84( lat, lon );
|
||||
return MogoApisHandler.getInstance()
|
||||
.getApis()
|
||||
.getMapServiceApi()
|
||||
.getMapUIController()
|
||||
.matchRoad( amap[0], amap[1], angle, isRtk );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@ import com.mogo.module.common.MogoApisHandler;
|
||||
import com.mogo.module.common.R;
|
||||
import com.mogo.module.common.api.CallChatApi;
|
||||
import com.mogo.module.common.constants.DataTypes;
|
||||
import com.mogo.module.common.utils.PointInterpolatorUtil;
|
||||
import com.mogo.realtime.entity.CloudLocationInfo;
|
||||
import com.mogo.realtime.entity.CloudRoadData;
|
||||
import com.mogo.realtime.entity.MogoSnapshotSetData;
|
||||
import com.mogo.service.statusmanager.IMogoStatusChangedListener;
|
||||
@@ -32,13 +30,14 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020/10/28
|
||||
*
|
||||
* 云端数据绘制
|
||||
*/
|
||||
public class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListener, IMogoStatusChangedListener {
|
||||
public
|
||||
/*
|
||||
* @author congtaowang
|
||||
* @since 2020/10/28
|
||||
*
|
||||
* 云端数据绘制
|
||||
*/
|
||||
class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClickListener, IMogoStatusChangedListener {
|
||||
|
||||
private static final String TAG = "SnapshotSetDataDrawer";
|
||||
|
||||
@@ -162,6 +161,16 @@ public class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClic
|
||||
continue;
|
||||
}
|
||||
|
||||
double[] matchedPoint = matchRoad( cloudRoadData.getLon(),
|
||||
cloudRoadData.getLat(),
|
||||
cloudRoadData.getHeading(),
|
||||
true
|
||||
);
|
||||
if ( matchedPoint != null ) {
|
||||
cloudRoadData.setLon( matchedPoint[0] );
|
||||
cloudRoadData.setLat( matchedPoint[1] );
|
||||
}
|
||||
|
||||
IMogoMarker marker = mCloudSnapshotMarkersCaches.remove( uniqueKey );
|
||||
CloudRoadData lastPosition = mLastPositions.put( uniqueKey, cloudRoadData );
|
||||
if ( marker == null || marker.isDestroyed() ) {
|
||||
@@ -189,20 +198,11 @@ public class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClic
|
||||
}
|
||||
}
|
||||
|
||||
if ( lastPosition != null && !DebugConfig.isNotSmooth() ) {
|
||||
if ( lastPosition != null ) {
|
||||
if ( lastPosition.equals( cloudRoadData ) ) {
|
||||
marker.setRotateAngle( ( float ) cloudRoadData.getHeading() );
|
||||
marker.setPosition( cloudRoadData.getLat(), cloudRoadData.getLon() );
|
||||
} else {
|
||||
List< MogoLatLng > points = new ArrayList<>();
|
||||
points.add( new MogoLatLng( lastPosition.getLat(), lastPosition.getLon() ) );
|
||||
if ( cloudRoadData.getCoordinates() != null && !cloudRoadData.getCoordinates().isEmpty() ) {
|
||||
for ( CloudLocationInfo coordinate : cloudRoadData.getCoordinates() ) {
|
||||
points.add( new MogoLatLng( coordinate.getLat(), coordinate.getLon() ) );
|
||||
}
|
||||
} else {
|
||||
points.add( new MogoLatLng( cloudRoadData.getLat(), cloudRoadData.getLon() ) );
|
||||
}
|
||||
long interval = cloudRoadData.getSystemTime() - lastPosition.getSystemTime();
|
||||
long interval2 = cloudRoadData.getSatelliteTime() - lastPosition.getSatelliteTime();
|
||||
interval2 = interval < interval2 || interval2 == 0 ? interval : interval2;
|
||||
@@ -210,8 +210,7 @@ public class SnapshotSetDataDrawer extends BaseDrawer implements IMogoMarkerClic
|
||||
interval2 = 45;
|
||||
}
|
||||
interval2 -= 25;
|
||||
PointInterpolatorUtil.interpolate(points);
|
||||
marker.startSmoothInMs( points, interval2 );
|
||||
marker.addDynamicAnchorPosition( new MogoLatLng( cloudRoadData.getLat(), cloudRoadData.getLon() ), interval2 );
|
||||
}
|
||||
} else {
|
||||
marker.setRotateAngle( ( float ) cloudRoadData.getHeading() );
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package com.mogo.module.common.utils;
|
||||
|
||||
import com.mogo.cloud.commons.utils.CoordinateUtils;
|
||||
import com.mogo.map.MogoLatLng;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 点之间插值工具类
|
||||
*
|
||||
* @author tongchenfei
|
||||
*/
|
||||
public class PointInterpolatorUtil {
|
||||
private static final String TAG = "PointInterpolatorUtil";
|
||||
private static final int DISTANCE_THRESHOLD = 2;
|
||||
|
||||
public static void interpolate(List<MogoLatLng> points) {
|
||||
if (points.size() >= 2) {
|
||||
// 插值
|
||||
for (int i = 0; i < points.size() - 1; i++) {
|
||||
MogoLatLng current = points.get(i);
|
||||
MogoLatLng next = points.get(i + 1);
|
||||
float distance = CoordinateUtils.calculateLineDistance(current.lon, current.lat, next.lon, next.lat);
|
||||
Logger.d(TAG, i + ": " + distance);
|
||||
if (distance > DISTANCE_THRESHOLD) {
|
||||
int inter = (int) (distance / DISTANCE_THRESHOLD);
|
||||
for (int j = 0; j < inter; j++) {
|
||||
double newLat = current.lat + (next.lat - current.lat) * 2 * (j + 1) / distance;
|
||||
double newLon = current.lon + (next.lon - current.lon) * 2 * (j + 1) / distance;
|
||||
Logger.d(TAG, "distance: " + distance + ", j: " + j + ", nextLat: " + next.lat + ", nextLon: " + next.lon + ", newLat: " + newLat + ", newLon: " + newLon);
|
||||
points.add(i + 1, new MogoLatLng(newLat, newLon));
|
||||
current = points.get(++i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static MogoLatLng mergeToRoad(MogoLatLng current, List<MogoLatLng> road) {
|
||||
closeStart = 0;
|
||||
closeEnd = road.size() - 1;
|
||||
getCloseTwoPoint(current, road);
|
||||
MogoLatLng start = road.get(closeStart);
|
||||
MogoLatLng end = road.get(closeEnd);
|
||||
return getFoot(current, start, end);
|
||||
}
|
||||
|
||||
private static int closeStart = 0;
|
||||
private static int closeEnd = 0;
|
||||
|
||||
private static void getCloseTwoPoint(MogoLatLng current, List<MogoLatLng> road) {
|
||||
if (closeEnd - closeStart == 1) {
|
||||
return;
|
||||
}
|
||||
MogoLatLng start = road.get(closeStart);
|
||||
MogoLatLng end = road.get(closeEnd);
|
||||
float startDistance = CoordinateUtils.calculateLineDistance(start.lon, start.lat, current.lon, current.lat);
|
||||
float endDistance = CoordinateUtils.calculateLineDistance(end.lon, end.lat, current.lon, current.lat);
|
||||
if (startDistance > endDistance) {
|
||||
closeStart += (closeEnd - closeStart) / 2;
|
||||
} else {
|
||||
closeEnd -= (closeEnd - closeStart) / 2;
|
||||
}
|
||||
getCloseTwoPoint(current, road);
|
||||
}
|
||||
|
||||
private static MogoLatLng getFoot(MogoLatLng pt, MogoLatLng beginPt, MogoLatLng endPt) {
|
||||
double dx = beginPt.lat - endPt.lat;
|
||||
double dy = beginPt.lon - endPt.lon;
|
||||
|
||||
double u = (pt.lat - beginPt.lat) * (beginPt.lat - endPt.lat) +
|
||||
(pt.lon - beginPt.lon) * (beginPt.lon - endPt.lon);
|
||||
u = u / (dx * dx + dy * dy);
|
||||
return new MogoLatLng(beginPt.lat + u * dx, beginPt.lon + u * dy);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user