修复了小地图自定义样式没有生效的bug,原因是高德地图的问题,解决方案如下

aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {

            @Override
            public void onMapLoaded() {
                //Log.w("onMapLoaded", "===onMapLoaded====");
                // 高德地图有bug,所以需要多次调用设置皮肤才能成功
                if (options != null) {
                    options.setCustomMapStylePath(styleFilePath);
                    mAMapNaviView.setViewOptions(options);
                }

                //设置希望展示的地图缩放级别
                CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(12);
                aMap.moveCamera(cameraUpdate);
            }
        });
This commit is contained in:
董宏宇
2020-12-22 13:00:48 +08:00
parent eff36419b4
commit 0b8c5205ef
5 changed files with 319 additions and 111 deletions

View File

@@ -3,24 +3,26 @@ package com.mogo.module.small.map;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewOptions;
import com.mogo.module.common.view.RoundLayout;
import com.mogo.module.small.map.animation.DirectionRotateAnimation;
import com.mogo.utils.FileUtils;
import java.io.File;
import java.io.IOException;
/**
* 小地图的方向View
@@ -32,12 +34,6 @@ public class SmallMapDirectionView extends RoundLayout {
private ImageView mIvMapBorder;
private AMapNaviView mAMapNaviView;
private AMap mAMap;
private UiSettings mUiSettings;
private CameraUpdate mCameraUpdate;
private MyLocationStyle myLocationStyle;
private AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationClientOption;
private DirectionRotateAnimation mRotateAnimation;
private int lastAngle = 0;
@@ -56,6 +52,8 @@ public class SmallMapDirectionView extends RoundLayout {
}
private void initView(Context context) {
String styleFilePath = "/mnt/sdcard/amap/small_map_style.data";
mRotateAnimation = new DirectionRotateAnimation(context, null);
LayoutInflater.from(context).inflate(R.layout.module_small_map_view, this);
@@ -67,15 +65,15 @@ public class SmallMapDirectionView extends RoundLayout {
mAMapNaviView.setNaviMode(AMapNaviView.CAR_UP_MODE);
// 设置电子眼所在路线的可见性
mAMapNaviView.setRouteOverlayVisible(false);
mAMap = mAMapNaviView.getMap();
AMap aMap = mAMapNaviView.getMap();
// 关闭地图文字标注
mAMap.showMapText(false);
aMap.showMapText(false);
// 设置地图的样式
mUiSettings = mAMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(false);// 地图缩放级别的交换按钮
mUiSettings.setAllGesturesEnabled(false);// 所有手势
mUiSettings.setMyLocationButtonEnabled(false); // 显示默认的定位按钮
mUiSettings.setLogoBottomMargin(-150); //设置Logo下边界距离屏幕底部的边距,设置为负值即可
UiSettings uiSettings = aMap.getUiSettings();
uiSettings.setZoomControlsEnabled(false);// 地图缩放级别的交换按钮
uiSettings.setAllGesturesEnabled(false);// 所有手势
uiSettings.setMyLocationButtonEnabled(false); // 显示默认的定位按钮
uiSettings.setLogoBottomMargin(-150); //设置Logo下边界距离屏幕底部的边距,设置为负值即可
// 导航地图
AMapNaviViewOptions options = mAMapNaviView.getViewOptions();
@@ -133,22 +131,22 @@ public class SmallMapDirectionView extends RoundLayout {
options.setNaviArrowVisible(false);
// 通过路线是否自动置灰,仅支持驾车导航
options.setAfterRouteAutoGray(false);
//options.setZoom(((int) 9));
options.setZoom(((int) 9));
//options.setPointToCenter(0.7D, 0.5D);
// 2D模式
options.setTilt(0);
// 黑夜模式
options.setNaviNight(true);
// options.setNaviNight(true);
// 自定义地图样式
// options.setCustomMapStylePath("/mnt/sdcard/amap/style.data");
options.setCustomMapStylePath(styleFilePath);
mAMapNaviView.setViewOptions(options);
}
//设置希望展示的地图缩放级别
mCameraUpdate = CameraUpdateFactory.zoomTo(12);
mAMap.moveCamera(mCameraUpdate);
CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(12);
aMap.moveCamera(cameraUpdate);
mAMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
if (cameraPosition != null) {
@@ -163,20 +161,66 @@ public class SmallMapDirectionView extends RoundLayout {
}
});
// CustomMapStyleOptions customMapStyleOptions = new CustomMapStyleOptions();
// customMapStyleOptions.setStyleData(MapAssetStyleUtils.getAssetsStyle(context));
// customMapStyleOptions.setStyleExtraData(MapAssetStyleUtils.getAssetsExtraStyle(context));
// customMapStyleOptions.setEnable(true);
// mAMap.setCustomMapStyle(customMapStyleOptions);
aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
// 设置当前位置的样式
// myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类
// myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点定位点依照设备方向旋转并且会跟随设备移动。1秒1次定位如果不设置myLocationType默认也会执行此种模式。
// myLocationStyle.interval(1000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
// BitmapDescriptor location = BitmapDescriptorFactory.fromResource(R.drawable.module_small_map_view_my_location_logo);
// myLocationStyle.myLocationIcon(location);
// mAMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
// mAMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点false表示隐藏定位蓝点并不进行定位默认是false。
@Override
public void onMapLoaded() {
//Log.w("onMapLoaded", "===onMapLoaded====");
// 高德地图有bug所以需要多次调用设置皮肤才能成功
if (options != null) {
options.setCustomMapStylePath(styleFilePath);
mAMapNaviView.setViewOptions(options);
}
//设置希望展示的地图缩放级别
CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(12);
aMap.moveCamera(cameraUpdate);
}
});
try {
// 判断是否有样式文件存在
File styleExit = new File(styleFilePath);
if (!styleExit.exists()) {
FileUtils.copy(
context.getAssets().open("small_map_style.data"),
styleFilePath,
new FileUtils.FileCopyListener() {
@Override
public void onStart() {
Log.w("FileCopyListener", "onStart=");
}
@Override
public void onFail(Exception e) {
Log.w("FileCopyListener", "onFail=");
e.printStackTrace();
}
@Override
public void onProcess(int process) {
Log.w("FileCopyListener", "onProcess=" + process);
}
@Override
public void onFinish(String toPath) {
Log.w("FileCopyListener", "onFinish toPath=" + toPath);
// 高德地图有bug所以需要多次调用设置皮肤才能成功
if (options != null) {
options.setCustomMapStylePath(styleFilePath);
mAMapNaviView.setViewOptions(options);
}
}
}
);
}
} catch (IOException e) {
e.printStackTrace();
}
}

View File

@@ -17,6 +17,9 @@ import com.mogo.service.map.IMogoSmallMapProvider;
import com.mogo.service.module.ModuleType;
import com.mogo.service.statusmanager.IMogoStatusChangedListener;
import com.mogo.service.statusmanager.StatusDescriptor;
import com.mogo.utils.FileUtils;
import java.io.IOException;
/**
* @author donghongyu