使用导航地图完成了圆形小地图的功能开发

This commit is contained in:
董宏宇
2020-12-18 17:53:54 +08:00
parent ce15133f21
commit 64a156657e
8 changed files with 180 additions and 68 deletions

View File

@@ -1,7 +1,9 @@
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;
@@ -17,10 +19,14 @@ import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.CustomMapStyleOptions;
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.module.small.map.utils.MapAssetStyleUtils;
import java.io.IOException;
import java.io.InputStream;
@@ -34,7 +40,7 @@ import java.io.InputStream;
public class SmallMapDirectionView extends RoundLayout {
private ImageView mIvMapBorder;
private TextureMapView mTextureMapView;
private AMapNaviView mAMapNaviView;
private AMap mAMap;
private UiSettings mUiSettings;
private CameraUpdate mCameraUpdate;
@@ -63,73 +69,127 @@ public class SmallMapDirectionView extends RoundLayout {
LayoutInflater.from(context).inflate(R.layout.module_small_map_view, this);
mIvMapBorder = findViewById(R.id.ivMapBorder);
mTextureMapView = findViewById(R.id.textureMapView);
mAMapNaviView = findViewById(R.id.aMapNaviView);
mAMapNaviView.onCreate(null);
// 车头朝上
mAMapNaviView.setNaviMode(AMapNaviView.CAR_UP_MODE);
mAMap = mAMapNaviView.getMap();
mTextureMapView.onCreate(null);
mAMap = mTextureMapView.getMap();
// 导航地图
AMapNaviViewOptions options = mAMapNaviView.getViewOptions();
//设置导航界面UI是否显示。
if (options != null) {
// 设置是否开启自动黑夜模式切换默认为false不自动切换
options.setAutoNaviViewNightMode(false);
// 设置6秒后是否自动锁车
options.setAutoLockCar(true);
// 设置路线上的摄像头气泡是否显示
options.setCameraBubbleShow(false);
// 设置路线相关的配置属性,如:路线的路况颜色,路线上是否显示摄像头气泡等。
// options.setRouteOverlayOptions( MapStyleUtils.getRouteOverlayOptions() );
// 设置自车的图片对象
options.setCarBitmap(BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.module_small_map_view_my_location_logo));
// 设置罗盘位图对象
options.setFourCornersBitmap(BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.icon_module_small_map_four_corners));
// 设置指南针图标否在导航界面显示默认显示。true显示false隐藏。
options.setCompassEnabled(false);
// 黑夜模式
options.setNaviNight(true);
//设置路况光柱条是否显示(只适用于驾车导航,需要联网)。
options.setTrafficBarEnabled(false);
// 设置[实时交通图层开关按钮]是否显示(只适用于驾车导航,需要联网)。
options.setTrafficLayerEnabled(false);
// 设置导航界面是否显示路线全览按钮。
options.setRouteListButtonShow(false);
// 设置屏幕是否常亮,默认开启
options.setScreenAlwaysBright(false);
// 设置交通播报是否打开(只适用于驾车导航,需要联网)。
options.setTrafficInfoUpdateEnabled(false);
// 设置摄像头播报是否打开(只适用于驾车导航)。
options.setCameraInfoUpdateEnabled(false);
// 设置菜单按钮是否在导航界面显示。
options.setSettingMenuEnabled(false);
// 设置是否绘制显示交通路况的线路(彩虹线),拥堵-红色,畅通-绿色,缓慢-黄色,未知-蓝色。默认不绘制彩虹线。
options.setTrafficLine(false);
// 设置是否绘制牵引线(当前位置到目的地的指引线)。默认不绘制牵引线。
options.setLeaderLineEnabled(-1);
// 设置导航界面UI是否显示。
options.setLayoutVisible(false);
// 设置是否自动画路
options.setAutoDrawRoute(false);
// 设置是否显示路口放大图(实景图)
options.setRealCrossDisplayShow(false);
// 设置是否显示路口放大图(路口模型图)
options.setModeCrossDisplayShow(false);
// 设置是否显示道路信息view
options.setLaneInfoShow(false);
// 设置是否自动改变缩放等级
options.setAutoChangeZoom(false);
// 设置是否自动全览模式,即在算路成功后自动进入全览模式
options.setAutoDisplayOverview(false);
// 设置路线转向箭头隐藏和显示
options.setNaviArrowVisible(false);
// 通过路线是否自动置灰,仅支持驾车导航
options.setAfterRouteAutoGray(false);
//options.setZoom(((int) 9));
//options.setPointToCenter(0.7D, 0.5D);
// 2D模式
options.setTilt(0);
mAMapNaviView.setViewOptions(options);
}
// 设置电子眼所在路线的可见性
mAMapNaviView.setRouteOverlayVisible(false);
// 设置是否隐藏AMapNaviView上的CarOverlay包括自车、罗盘
mAMapNaviView.setCarOverlayVisible(true);
mAMapNaviView.setViewOptions(options);
// 设置自定义地图样式
try {
byte[] buffer1 = null;
byte[] buffer2 = null;
InputStream is1 = null;
InputStream is2 = null;
try {
is1 = context.getAssets().open("small_map_style.data");
int lenght1 = is1.available();
buffer1 = new byte[lenght1];
is1.read(buffer1);
is2 = context.getAssets().open("small_map_style_extra.data");
int lenght2 = is2.available();
buffer2 = new byte[lenght2];
is2.read(buffer2);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is1 != null)
is1.close();
if (is2 != null)
is2.close();
} catch (IOException e) {
e.printStackTrace();
//设置希望展示的地图缩放级别
mCameraUpdate = CameraUpdateFactory.zoomTo(12);
mAMap.moveCamera(mCameraUpdate);
// 设置当前位置的样式
// 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。
mAMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
if (cameraPosition != null) {
//Log.w("onCameraChange", "cameraPosition=" + cameraPosition.bearing);
changeAngle((int) cameraPosition.bearing);
}
}
CustomMapStyleOptions customMapStyleOptions = new CustomMapStyleOptions();
customMapStyleOptions.setStyleData(buffer1);
customMapStyleOptions.setStyleExtraData(buffer2);
customMapStyleOptions.setEnable(true);
mAMap.setCustomMapStyle(customMapStyleOptions);
} catch (Exception e) {
e.printStackTrace();
}
@Override
public void onCameraChangeFinish(CameraPosition cameraPosition) {
// 设置当前位置的样式
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。
//设置希望展示的地图缩放级别
mCameraUpdate = CameraUpdateFactory.zoomTo(17);
mAMap.moveCamera(mCameraUpdate);
}
});
// 关闭地图文字标注
mAMap.showMapText(false);
// 设置地图的样式
mUiSettings = mAMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(false);// 地图缩放级别的交换按钮
// mUiSettings.setZoomGesturesEnabled(false);// 缩放手势
// mUiSettings.setScrollGesturesEnabled(false);// 滑动手势
// mUiSettings.setRotateGesturesEnabled(false);// 旋转手势
// mUiSettings.setTiltGesturesEnabled(false);// 倾斜手势
mUiSettings.setAllGesturesEnabled(false);// 所有手势
mUiSettings.setMyLocationButtonEnabled(false); // 显示默认的定位按钮
mUiSettings.setLogoBottomMargin(-150); //设置Logo下边界距离屏幕底部的边距,设置为负值即可
CustomMapStyleOptions customMapStyleOptions = new CustomMapStyleOptions();
customMapStyleOptions.setStyleData(MapAssetStyleUtils.getAssetsStyle(context));
customMapStyleOptions.setStyleExtraData(MapAssetStyleUtils.getAssetsExtraStyle(context));
customMapStyleOptions.setEnable(true);
mAMap.setCustomMapStyle(customMapStyleOptions);
}
@@ -139,9 +199,10 @@ public class SmallMapDirectionView extends RoundLayout {
* @param angle 角度 0 - 359度旋转,相对于自身中心位置
*/
public void changeAngle(int angle) {
int tempAngle = angle;
if (angle <= 180) {
tempAngle = angle;
int tempAngle = 360 - angle;
if (tempAngle <= 180) {
tempAngle = tempAngle;
} else {
tempAngle = -(360 - tempAngle);
}

View File

@@ -88,15 +88,6 @@ public class SmallMapService extends Service {
mSmallMapDirectionView = mWindowManagerView.findViewById(R.id.smallMapDirectionView);
mSmallMapDirectionView.postDelayed(new Runnable() {
@Override
public void run() {
Random random = new Random();
mSmallMapDirectionView.changeAngle(random.nextInt(360));
}
}, 1000);
}

View File

@@ -0,0 +1,60 @@
package com.mogo.module.small.map.utils;
import android.content.Context;
import java.io.IOException;
import java.io.InputStream;
/**
* @author donghongyu
* @date 12/18/20 5:37 PM
*/
public class MapAssetStyleUtils {
public static byte[] getAssetsStyle(Context context) {
byte[] buffer1 = null;
InputStream is1 = null;
try {
is1 = context.getResources().getAssets().open("small_map_style.data");
int lenght1 = is1.available();
buffer1 = new byte[lenght1];
is1.read(buffer1);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is1 != null) {
is1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer1;
}
public static byte[] getAssetsExtraStyle(Context context) {
byte[] buffer1 = null;
InputStream is1 = null;
try {
is1 = context.getResources().getAssets().open("small_map_style_extra.data");
int lenght1 = is1.available();
buffer1 = new byte[lenght1];
is1.read(buffer1);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is1 != null) {
is1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer1;
}
}