[Fix]修复小地图加载失败问题

Signed-off-by: donghongyu <donghongyu@zhidaoauto.com>
This commit is contained in:
donghongyu
2021-11-15 20:02:20 +08:00
parent 8d29be3ba6
commit 26d92ba5d2
15 changed files with 273 additions and 383 deletions

View File

@@ -30,10 +30,12 @@ import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.mogo.cloud.commons.utils.CoordinateUtils;
import com.mogo.eagle.core.data.map.MogoLatLng;
import com.mogo.eagle.core.data.map.MogoLocation;
import com.mogo.eagle.core.function.api.map.listener.IMoGoMapLocationListener;
import com.mogo.eagle.core.function.call.map.CallerMapLocationListenerManager;
import com.mogo.eagle.core.function.smp.utils.MapAssetStyleUtils;
import com.mogo.eagle.core.function.smp.view.ISmallMapDirectionView;
import com.mogo.eagle.core.widget.RoundLayout;
import com.mogo.map.navi.IMogoCarLocationChangedListener2;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.module.common.utils.DrivingDirectionUtils;
import com.mogo.utils.logger.Logger;
@@ -49,10 +51,10 @@ import java.util.List;
*/
public class SmallMapDirectionView
extends RelativeLayout
implements IMogoCarLocationChangedListener2, ISmallMapDirectionView {
implements IMoGoMapLocationListener, ISmallMapDirectionView {
//小地图名称
public static final String MODULE_NAME = "SmallMap";
public static final String TAG = "SmallMapDirectionView";
private RoundLayout rlSmallMapBorder;
private TextureMapView mAMapNaviView;
@@ -67,6 +69,7 @@ public class SmallMapDirectionView
private Polyline mPolyline;
private CameraUpdate mCameraUpdate;
private Context mContext;
public SmallMapDirectionView(Context context) {
this(context, null);
}
@@ -85,18 +88,18 @@ public class SmallMapDirectionView
}
private void initView(Context context) {
Logger.d(TAG, "initView");
mContext = context;
View smpView = LayoutInflater.from(context).inflate(R.layout.module_small_map_view, this);
View smpView = LayoutInflater.from(context).inflate(R.layout.module_small_map_view, this);
mAMapNaviView = (TextureMapView)smpView.findViewById(R.id.aMapNaviView);
mAMapNaviView = (TextureMapView) smpView.findViewById(R.id.aMapNaviView);
// rlSmallMapBorder = findViewById(R.id.rlSmallMapBorder);
// rlSmallMapBorder.addView(mAMapNaviView);
initAMapView();
// 注册定位监听
SmpServiceManager.getMogoRegisterCenter()
.registerCarLocationChangedListener(MODULE_NAME, this);
CallerMapLocationListenerManager.INSTANCE.addListener(TAG, this);
setOnLongClickListener(view -> {
MogoApisHandler.getInstance().getApis()
@@ -117,9 +120,7 @@ public class SmallMapDirectionView
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// 注册定位监听
SmpServiceManager.getMogoRegisterCenter()
.unregisterCarLocationChangedListener(MODULE_NAME, this);
CallerMapLocationListenerManager.INSTANCE.removeListener(TAG);
}
private void initAMapView() {
@@ -166,7 +167,7 @@ public class SmallMapDirectionView
mAMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
@Override
public void onMapLoaded() {
mAMapNaviView.getMap().setPointToCenter(mAMapNaviView.getWidth()/2, mAMapNaviView.getHeight()/2);
mAMapNaviView.getMap().setPointToCenter(mAMapNaviView.getWidth() / 2, mAMapNaviView.getHeight() / 2);
}
});
}
@@ -177,10 +178,11 @@ public class SmallMapDirectionView
return true;
}
@Override
public void onCarLocationChanged2(Location latLng) {
//Logger.d(MODULE_NAME, "onCarLocationChanged2 latLng:" + latLng);
LatLng currentLatLng = new LatLng(latLng.getLatitude(), latLng.getLongitude());
public void onLocationChanged(@Nullable MogoLocation location) {
Logger.d(TAG, "onCarLocationChanged2 latLng:" + location);
LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
//更新车辆位置
if (mCarMarker != null) {
// mCarMarker.setRotateAngle(360 - latLng.getBearing());
@@ -209,8 +211,8 @@ public class SmallMapDirectionView
endLatLng.latitude, endLatLng.longitude,
currentLatLng.latitude, currentLatLng.longitude
);
Log.d(MODULE_NAME, "calculateDistance=" + calculateDistance);
Log.d(TAG, "calculateDistance=" + calculateDistance);
if (calculateDistance <= 20) {
clearPolyline();
} else {
@@ -220,13 +222,13 @@ public class SmallMapDirectionView
//设置希望展示的地图缩放级别
mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, zoomLevel));
}
CameraPosition cameraPosition = new CameraPosition.Builder().target(mCarMarker.getPosition()).bearing(latLng.getBearing()).tilt(0).zoom(zoomLevel).build();
CameraPosition cameraPosition = new CameraPosition.Builder().target(mCarMarker.getPosition()).bearing(location.getBearing()).tilt(0).zoom(zoomLevel).build();
mAMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void removeLoction(Location latLng) {
for (LatLng l: mCoordinatesLatLng){
if (!isPointOnCarFront(latLng,l)){
for (LatLng l : mCoordinatesLatLng) {
if (!isPointOnCarFront(latLng, l)) {
mCoordinatesLatLng.remove(l);
}
}
@@ -250,10 +252,6 @@ public class SmallMapDirectionView
}
}
@Override
public void onCarLocationChanged(MogoLatLng latLng) {
Logger.d(MODULE_NAME, "onCarLocationChanged latLng:" + latLng);
}
@Override
public void drawablePolyline() {
@@ -297,22 +295,23 @@ public class SmallMapDirectionView
}
public LatLng CoordinateConverterFrom84(Context mContext, MogoLatLng mogoLatLng){
public LatLng CoordinateConverterFrom84(Context mContext, MogoLatLng mogoLatLng) {
CoordinateConverter mCoordinateConverter = new CoordinateConverter(mContext);
mCoordinateConverter.from(CoordinateConverter.CoordType.GPS);
mCoordinateConverter.coord(new LatLng(mogoLatLng.lat,mogoLatLng.lon));
LatLng latLng = mCoordinateConverter.convert();
mCoordinateConverter.coord(new LatLng(mogoLatLng.lat, mogoLatLng.lon));
LatLng latLng = mCoordinateConverter.convert();
return latLng;
}
public List<LatLng> CoordinateConverterFrom84ForList(Context mContext, List<MogoLatLng> mogoLatLngList){
List<LatLng> list = new ArrayList<>();
for (MogoLatLng m:mogoLatLngList) {
LatLng mogoLatLng = CoordinateConverterFrom84(mContext,m);
public List<LatLng> CoordinateConverterFrom84ForList(Context mContext, List<MogoLatLng> mogoLatLngList) {
List<LatLng> list = new ArrayList<>();
for (MogoLatLng m : mogoLatLngList) {
LatLng mogoLatLng = CoordinateConverterFrom84(mContext, m);
list.add(mogoLatLng);
}
return list;
}
@Override
public void clearPolyline() {
// mCoordinatesLatLng.clear();
@@ -327,7 +326,7 @@ public class SmallMapDirectionView
}
}
public void resetPolyine(){
public void resetPolyine() {
mCoordinatesLatLng.clear();
if (mPolyline != null) {
mPolyline.remove();
@@ -341,32 +340,33 @@ public class SmallMapDirectionView
}
public void onCreateView(Bundle savedInstanceState) {
if(mAMapNaviView != null){
if (mAMapNaviView != null) {
mAMapNaviView.onCreate(savedInstanceState);
}
}
public void onResume() {
if(mAMapNaviView != null){
if (mAMapNaviView != null) {
mAMapNaviView.onResume();
}
}
public void onPause() {
if(mAMapNaviView != null){
if (mAMapNaviView != null) {
mAMapNaviView.onPause();
}
}
public void onDestroy() {
if(mAMapNaviView != null){
if (mAMapNaviView != null) {
mAMapNaviView.onDestroy();
}
}
public void convert(List<MogoLatLng> coordinates) {
mCoordinatesLatLng.clear();
List<LatLng> latLngs = CoordinateConverterFrom84ForList(mContext,coordinates);
List<LatLng> latLngs = CoordinateConverterFrom84ForList(mContext, coordinates);
mCoordinatesLatLng.addAll(latLngs);
}
}

View File

@@ -2,24 +2,29 @@ package com.mogo.eagle.core.function.smp;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.commons.mvp.BaseFragment;
import com.mogo.eagle.core.data.autopilot.ADASTrajectoryInfo;
import com.mogo.eagle.core.data.autopilot.AutopilotRouteInfo;
import com.mogo.eagle.core.data.constants.MoGoFragmentPaths;
import com.mogo.eagle.core.data.map.MogoLatLng;
import com.mogo.eagle.core.function.api.autopilot.IMoGoAutopilotPlanningListener;
import com.mogo.eagle.core.function.api.map.smp.IMogoSmallMapProvider;
import com.mogo.eagle.core.function.call.autopilot.CallerAutopilotPlanningListenerManager;
import java.util.ArrayList;
import java.util.List;
/**
* @author donghongyu
* @date 2021/5/19 10:50 上午
*/
public class SmallMapFragment extends Fragment {
protected View mRootView;
@Route(path = MoGoFragmentPaths.PATH_FRAGMENT_SMP)
public class SmallMapFragment extends BaseFragment implements IMogoSmallMapProvider, IMoGoAutopilotPlanningListener {
private final String TAG = "SmallMapFragment";
protected SmallMapDirectionView mSmallMapDirectionView;
@Override
@@ -27,22 +32,40 @@ public class SmallMapFragment extends Fragment {
super.onAttach(context);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null) {
mRootView = inflater.inflate(R.layout.module_small_map_fragment, container, false);
mSmallMapDirectionView = mRootView.findViewById(R.id.smallMapDirectionView);
mSmallMapDirectionView.onCreateView(savedInstanceState);
} else {
ViewGroup viewGroup = (ViewGroup) mRootView.getParent();
if (viewGroup != null) {
viewGroup.removeView(mRootView);
}
}
return mRootView;
protected int getLayoutId() {
return R.layout.module_small_map_fragment;
}
@Override
public String getTagName() {
return TAG;
}
@Override
protected void initViews() {
SmpServiceManager.init(getContext());
}
@Override
protected void initViews(Bundle savedInstanceState) {
super.initViews(savedInstanceState);
mSmallMapDirectionView = mRootView.findViewById(R.id.smallMapDirectionView);
mSmallMapDirectionView.onCreateView(savedInstanceState);
CallerAutopilotPlanningListenerManager.INSTANCE.addListener(TAG, this);
}
@Override
public void showPanel() {
mSmallMapDirectionView.setVisibility(View.VISIBLE);
}
@Override
public void hidePanel() {
mSmallMapDirectionView.setVisibility(View.GONE);
}
@Override
public void drawablePolyline(List<MogoLatLng> coordinates) {
if (mSmallMapDirectionView != null) {
mSmallMapDirectionView.convert(coordinates);
@@ -50,6 +73,7 @@ public class SmallMapFragment extends Fragment {
}
}
@Override
public void clearPolyline() {
if (mSmallMapDirectionView != null) {
mSmallMapDirectionView.clearPolyline();
@@ -73,6 +97,29 @@ public class SmallMapFragment extends Fragment {
public void onDestroy() {
super.onDestroy();
mSmallMapDirectionView.onDestroy();
CallerAutopilotPlanningListenerManager.INSTANCE.removeListener(TAG);
}
@Override
public void onAutopilotTrajectory(ArrayList<ADASTrajectoryInfo> trajectoryInfos) {
}
@Override
public void onAutopilotRotting(AutopilotRouteInfo routeList) {
if (routeList.getModels() == null || routeList.getModels().size() == 0) {
return;
}
List<MogoLatLng> latLngList = new ArrayList<>();
for (AutopilotRouteInfo.RouteModels routeModel : routeList.getModels()) {
latLngList.add(new MogoLatLng(routeModel.getLat(), routeModel.getLon()));
}
Log.e(TAG, "routeResult:" + latLngList.size());
if (latLngList.size() > 0) {
drawablePolyline(latLngList);
} else {
clearPolyline();
}
}
}

View File

@@ -1,176 +0,0 @@
package com.mogo.eagle.core.function.smp;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.eagle.core.data.autopilot.ADASTrajectoryInfo;
import com.mogo.eagle.core.data.autopilot.AutopilotRouteInfo;
import com.mogo.eagle.core.data.constants.MogoServicePaths;
import com.mogo.eagle.core.data.map.MogoLatLng;
import com.mogo.eagle.core.function.api.autopilot.IMoGoAutopilotPlanningListener;
import com.mogo.eagle.core.function.api.map.smp.IMogoSmallMapProvider;
import com.mogo.eagle.core.function.call.autopilot.CallerAutopilotPlanningListenerManager;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.service.statusmanager.IMogoStatusChangedListener;
import com.mogo.service.statusmanager.StatusDescriptor;
import java.util.ArrayList;
import java.util.List;
/**
* @author donghongyu
* @date 12/10/20 1:34 PM
*/
@Route(path = MogoServicePaths.PATH_SMALL_MAP)
public class SmallMapProvider implements IMogoSmallMapProvider, IMogoStatusChangedListener, IMoGoAutopilotPlanningListener {
private final String TAG = "SmallMapProvider";
private Context mContext;
private int mContainerId;
private FragmentActivity mActivity;
private SmallMapFragment mSmallMapFragment;
@Override
public void init(FragmentActivity activity, int containerId) {
Log.d(TAG, "小地图模块初始化……init(FragmentActivity activity, int containerId)");
mActivity = activity;
mContainerId = containerId;
SmpServiceManager.init(mActivity);
CallerAutopilotPlanningListenerManager.INSTANCE.addListener(TAG, this);
}
@Override
public void init(Context context) {
Log.d(TAG, "小地图模块初始化……init(Context context)");
mContext = context;
MogoApisHandler.getInstance()
.getApis()
.getStatusManagerApi()
.registerStatusChangedListener(
MogoServicePaths.PATH_SMALL_MAP,
StatusDescriptor.VR_MODE,
this);
MogoApisHandler.getInstance()
.getApis()
.getStatusManagerApi()
.registerStatusChangedListener(
MogoServicePaths.PATH_SMALL_MAP,
StatusDescriptor.MAIN_PAGE_RESUME,
this);
// MogoApisHandler.getInstance().getApis().getAdasControllerApi().addAdasAutopilotRouteCallBack(this);
if (MogoApisHandler.getInstance().getApis().getStatusManagerApi().isVrMode()) {
showPanel();
} else {
hidePanel();
}
}
@Override
public void showPanel() {
Log.d(TAG, "小地图模块触发展示……");
try {
if (mSmallMapFragment == null) {
Log.d(TAG, "准备add fragment======");
mSmallMapFragment = new SmallMapFragment();
mActivity.getSupportFragmentManager().beginTransaction().add(mContainerId, mSmallMapFragment).commitAllowingStateLoss();
return;
}
Log.d(TAG, "准备show fragment");
mActivity.getSupportFragmentManager().beginTransaction().show(mSmallMapFragment).commitAllowingStateLoss();
} catch (Exception e) {
Log.d(TAG, "add fragment 失败 ======" + e.getMessage());
e.printStackTrace();
}
}
@Override
public void hidePanel() {
Log.d(TAG, "小地图模块触发隐藏……");
try {
if (mSmallMapFragment != null) {
Log.d(TAG, "准备hide fragment");
mActivity.getSupportFragmentManager().beginTransaction().hide(mSmallMapFragment).commitAllowingStateLoss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void drawablePolyline(List<MogoLatLng> coordinates) {
if (mSmallMapFragment != null) {
mSmallMapFragment.drawablePolyline(coordinates);
}
}
@Override
public void clearPolyline() {
if (mSmallMapFragment != null) {
mSmallMapFragment.clearPolyline();
}
}
@Override
public void onStatusChanged(StatusDescriptor descriptor, boolean isTrue) {
Log.d(TAG, "onStatusChanged……descriptor=" + descriptor + "isTrue=" + isTrue);
if (descriptor == StatusDescriptor.VR_MODE) {
if (isTrue) {
showPanel();
} else {
hidePanel();
}
}
}
@Nullable
@Override
public Fragment createCoverage(@Nullable FragmentActivity activity, @Nullable Integer containerId) {
return null;
}
@NonNull
@Override
public String getFunctionName() {
return TAG;
}
@Override
public void onDestroy() {
CallerAutopilotPlanningListenerManager.INSTANCE.removeListener(TAG);
}
@Override
public void onAutopilotTrajectory(ArrayList<ADASTrajectoryInfo> trajectoryInfos) {
}
@Override
public void onAutopilotRotting(AutopilotRouteInfo routeList) {
if (routeList.getModels() == null || routeList.getModels().size() == 0) {
return;
}
List<MogoLatLng> latLngList = new ArrayList<>();
for (AutopilotRouteInfo.RouteModels routeModel : routeList.getModels()) {
latLngList.add(new MogoLatLng(routeModel.getLat(), routeModel.getLon()));
}
Log.e(TAG, "routeResult:" + latLngList.size());
if (latLngList.size() > 0) {
drawablePolyline(latLngList);
} else {
clearPolyline();
}
}
}