代码优化

This commit is contained in:
wangcongtao
2021-03-02 19:29:47 +08:00
parent 4d62d6f58c
commit fe1be1667d
11 changed files with 139 additions and 68 deletions

View File

@@ -15,6 +15,7 @@ import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.TextView;
import com.mogo.cloud.commons.utils.CoordinateUtils;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.map.IMogoMap;
import com.mogo.map.IMogoMapView;
@@ -23,6 +24,7 @@ import com.mogo.map.impl.custom.location.GpsTester;
import com.mogo.map.impl.custom.navi.NaviClient;
import com.mogo.map.impl.custom.utils.MogoMapUtils;
import com.mogo.map.impl.custom.utils.ObjectUtils;
import com.mogo.map.impl.custom.utils.PointInterpolatorUtil;
import com.mogo.map.listener.MogoMapListenerHandler;
import com.mogo.map.marker.IMogoMarker;
import com.mogo.map.uicontroller.CarCursorOption;
@@ -44,6 +46,8 @@ import com.zhidaoauto.map.sdk.open.abs.OnMapTouchListener;
import com.zhidaoauto.map.sdk.open.camera.CameraPosition;
import com.zhidaoauto.map.sdk.open.camera.CameraUpdateFactory;
import com.zhidaoauto.map.sdk.open.camera.LatLngBounds;
import com.zhidaoauto.map.sdk.open.data.MapDataApi;
import com.zhidaoauto.map.sdk.open.data.SinglePointRoadInfo;
import com.zhidaoauto.map.sdk.open.location.LocationListener;
import com.zhidaoauto.map.sdk.open.location.MyLocationStyle;
import com.zhidaoauto.map.sdk.open.location.RTKAutopilotLocationBean;
@@ -1014,4 +1018,16 @@ public class AMapViewWrapper implements IMogoMapView,
e.printStackTrace();
}
}
@Override
public double[] matchRoad( double lon, double lat, double angle, boolean isRTK ) {
SinglePointRoadInfo singlePointRoadInfo = MapDataApi.INSTANCE.getSinglePointMatchRoad( ( ( float ) lon ), ( ( float ) lat ), ( ( float ) angle ), !isRTK, isRTK );
if ( singlePointRoadInfo != null
&& singlePointRoadInfo.getCoords() != null
&& !singlePointRoadInfo.getCoords().isEmpty() ) {
double matchedPoint[] = PointInterpolatorUtil.mergeToRoad(lon, lat, singlePointRoadInfo.getCoords());
return CoordinateUtils.transformWgsToGcj( matchedPoint[1], matchedPoint[0] );
}
return null;
}
}

View File

@@ -313,4 +313,12 @@ public class AMapUIController implements IMogoMapUIController {
mClient.openVrMode( zoomGestureEnable );
}
}
@Override
public double[] matchRoad( double lon, double lat, double angle, boolean isRTK ) {
if ( mClient != null ) {
return mClient.matchRoad( lon, lat, angle, isRTK );
}
return null;
}
}

View File

@@ -0,0 +1,79 @@
package com.mogo.map.impl.custom.utils;
import com.mogo.cloud.commons.utils.CoordinateUtils;
import com.mogo.map.MogoLatLng;
import com.mogo.utils.logger.Logger;
import com.zhidaoauto.map.sdk.open.query.LonLatPoint;
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 double[] mergeToRoad(double lon, double lat, List< LonLatPoint > road) {
closeStart = 0;
closeEnd = road.size() - 1;
getCloseTwoPoint(lon, lat, road);
LonLatPoint start = road.get(closeStart);
LonLatPoint end = road.get(closeEnd);
return getFoot(lon, lat, start, end);
}
private static int closeStart = 0;
private static int closeEnd = 0;
private static void getCloseTwoPoint(double lon, double lat, List<LonLatPoint> road) {
if (closeEnd - closeStart == 1) {
return;
}
LonLatPoint start = road.get(closeStart);
LonLatPoint end = road.get(closeEnd);
float startDistance = CoordinateUtils.calculateLineDistance(start.getLongitude(), start.getLatitude(), lon, lat);
float endDistance = CoordinateUtils.calculateLineDistance(end.getLongitude(), end.getLatitude(), lon, lat);
if (startDistance > endDistance) {
closeStart += (closeEnd - closeStart) / 2;
} else {
closeEnd -= (closeEnd - closeStart) / 2;
}
getCloseTwoPoint(lon, lat, road);
}
private static double[] getFoot(double lon, double lat, LonLatPoint beginPt, LonLatPoint endPt) {
double dx = beginPt.getLatitude() - endPt.getLatitude();
double dy = beginPt.getLongitude() - endPt.getLongitude();
double u = (lat - beginPt.getLatitude()) * (beginPt.getLatitude() - endPt.getLatitude()) +
(lon - beginPt.getLongitude()) * (beginPt.getLongitude() - endPt.getLongitude());
u = u / (dx * dx + dy * dy);
return new double[]{ beginPt.getLongitude() + u * dy, beginPt.getLatitude() + u * dx};
}
}