一、概述
上一节中我们了解到地图的定位,图层切换,离线地图等基础操作,接下来学习地图的基本操作。
二、本章内容
--- 地图交互设置
--- 地图绘制
1.地图交互设置
在使用地图的时候,不可避免的会涉及到与地图交互问题,如滑动手势,地图缩放,地图旋转,地图logo位置等。我们可以根据需要来决定是否开启某些交互功能。主要用到的就是高德地图API中的 UiSettings 类。通过AMap实例可以获取到UiSettings的实例 uiSettings = aMap.getUiSettings();
①手势设置
//开启放缩手势
uiSettings.setZoomGesturesEnabled(true);
//开启滑动手势
uiSettings.setScrollGesturesEnabled(true);
//开启旋转手势
uiSettings.setRotateGesturesEnabled(true);
//开启双指倾斜手势
uiSettings.setTiltGesturesEnabled(true);
//开启全部手势
uiSettings.setAllGesturesEnabled(true);
//指定手势中心点
//aMap.setPointToCenter(100,100);
//开启中心为手势中心
uiSettings.setGestureScaleByMapCenter(true);
上面这些功能在一般情况下,默认是开启的。当我们有特殊的需求时可以适当的关闭某些手势。
②地图缩放
//是否允许显示地图缩放按钮
uiSettings.setZoomControlsEnabled(true);
//是否允许收拾手势缩放地图
uiSettings.setZoomGesturesEnabled(true);
//设置双击地图放大在地图中心位置放大,false则是在点击位置放大
uiSettings.setZoomInByScreenCenter(true);
//地图缩放按钮的位置
uiSettings.setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_BUTTOM);
//AMapOptions.ZOOM_POSITION_RIGHT_CENTER
//AMapOptions.ZOOM_POSITION_RIGHT_BUTTOM
//获取地图缩放按钮位置
Log.e(TAG, "settingZoom: " + uiSettings.getZoomPosition());
③将屏幕中心移动到指定经纬度
//这个类就是设置地图移动的参数,CameraPosition,参数1---要移动到的经纬度,
//参数2---地图的放缩级别zoom,参数3---地图倾斜度,参数4---地图的旋转角度
CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(
new CameraPosition(new LatLng(30.67, 104.07), 10, 0, 0));
//带动画的移动,aMap添加动画监听时,会有动画效果。不添加不会开启动画
aMap.animateCamera(mCameraUpdate, 5000, new AMap.CancelableCallback() {
@Override
public void onFinish() {
}
@Override
public void onCancel() {
}
});
//不带动画的移动
aMap.moveCamera(mCameraUpdate);
④其他设置
//是否显示指南针
uiSettings.setCompassEnabled(true);
//开启比例尺
uiSettings.setScaleControlsEnabled(true);
// 设置logo位置
uiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_CENTER);
//AMapOptions.LOGO_POSITION_BOTTOM_LEFTLOGO(左边)
//AMapOptions.LOGO_MARGIN_BOTTOMLOGO(底部)
//AMapOptions.LOGO_MARGIN_RIGHTLOGO(右边)
//AMapOptions.LOGO_POSITION_BOTTOM_CENTER(地图底部居中)
//AMapOptions.LOGO_POSITION_BOTTOM_LEFT(地图左下角)
//AMapOptions.LOGO_POSITION_BOTTOM_RIGHT (地图右下角)
//显示默认的定位按钮
uiSettings.setMyLocationButtonEnabled(true);
// 可触发定位并显示当前位置
aMap.setMyLocationEnabled(true);
//这里设置定位为了在点击定位按钮后,显示地图定位的位置方便查看
//注意这里有个坑,在点击定位后发现定位到了默认的位置(海里面),造成这种情况并不是权限和代码的问题,
//遇到这种情况时,需要手动将GPS定位打开就OK了
MyLocationStyle mls = new MyLocationStyle();
mls.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
aMap.setMyLocationEnabled(true);
aMap.setMyLocationStyle(mls);
代码
public class InteractiveMapActivity extends BaseActivity {
@BindView(R.id.a_map_view)
MapView aMapView;
@BindView(R.id.button)
Button button;
@BindView(R.id.button2)
Button button2;
@BindView(R.id.button3)
Button button3;
@BindView(R.id.button4)
Button button4;
private Unbinder binder;
private UiSettings uiSettings;
private AMap aMap;
private static final String TAG = "CF";
@Override
public void setContentView(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.acticity_interactive);
binder = ButterKnife.bind(this);
aMapView.onCreate(savedInstanceState);
}
@Override
public void initData() {
aMap = aMapView.getMap();
uiSettings = aMap.getUiSettings();
//设置地图缩放
settingZoom();
//是否显示指南针
uiSettings.setCompassEnabled(true);
//开启比例尺
uiSettings.setScaleControlsEnabled(true);
// 设置logo位置
uiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_CENTER);
//AMapOptions.LOGO_POSITION_BOTTOM_LEFTLOGO(左边)
//AMapOptions.LOGO_MARGIN_BOTTOMLOGO(底部)
//AMapOptions.LOGO_MARGIN_RIGHTLOGO(右边)
//AMapOptions.LOGO_POSITION_BOTTOM_CENTER(地图底部居中)
//AMapOptions.LOGO_POSITION_BOTTOM_LEFT(地图左下角)
//AMapOptions.LOGO_POSITION_BOTTOM_RIGHT (地图右下角)
//显示默认的定位按钮
uiSettings.setMyLocationButtonEnabled(true);
// 可触发定位并显示当前位置
aMap.setMyLocationEnabled(true);
//这里设置定位为了在点击定位按钮后,显示地图定位的位置方便查看
//注意这里有个坑,在点击定位后发现定位到了默认的位置(海里面),造成这种情况并不是权限和代码的问题,
//遇到这种情况时,需要手动将GPS定位打开就OK了
MyLocationStyle mls = new MyLocationStyle();
mls.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
aMap.setMyLocationEnabled(true);
aMap.setMyLocationStyle(mls);
setGestures();
}
private void setGestures() {
//开启放缩手势
uiSettings.setZoomGesturesEnabled(true);
//开启滑动手势
uiSettings.setScrollGesturesEnabled(true);
//开启旋转手势
uiSettings.setRotateGesturesEnabled(true);
//开启双指倾斜手势
uiSettings.setTiltGesturesEnabled(true);
//开启全部手势
uiSettings.setAllGesturesEnabled(true);
//指定手势中心点
// aMap.setPointToCenter(100,100);
//开启中心为手势中心
uiSettings.setGestureScaleByMapCenter(true);
}
private void settingZoom() {
//是否允许显示地图缩放按钮
uiSettings.setZoomControlsEnabled(true);
//是否允许收拾手势缩放地图
uiSettings.setZoomGesturesEnabled(true);
//设置双击地图放大在地图中心位置放大,false则是在点击位置放大
uiSettings.setZoomInByScreenCenter(true);
//地图缩放按钮的位置
uiSettings.setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_BUTTOM);
// AMapOptions.ZOOM_POSITION_RIGHT_CENTER
// AMapOptions.ZOOM_POSITION_RIGHT_BUTTOM
//获取地图缩放按钮位置
Log.e(TAG, "settingZoom: " + uiSettings.getZoomPosition());
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
aMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
aMapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
aMapView.onDestroy();
binder.unbind();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
aMapView.onSaveInstanceState(outState);
}
private void moveToTargetPosition() {
//这个类就是设置地图移动的参数,CameraPosition,参数1---要移动到的经纬度,参数2---地图的放缩级别zoom,参数3---地图倾斜度,参数4---地图的旋转角度
CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(
new CameraPosition(new LatLng(30.67, 104.07), 10, 0, 0));
//带动画的移动
aMap.animateCamera(mCameraUpdate, 5000, new AMap.CancelableCallback() {
@Override
public void onFinish() {
}
@Override
public void onCancel() {
}
});
//不带动画的移动
aMap.moveCamera(mCameraUpdate);
}
@OnClick({R.id.button, R.id.button2, R.id.button3, R.id.button4})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.button:
moveToTargetPosition();
break;
case R.id.button2:
showCustomMapBounds(true);
break;
case R.id.button3:
showCustomMapBounds(false);
break;
case R.id.button4:
screenCapture();
break;
}
}
private void showCustomMapBounds(boolean enable) {
if (enable) {
LatLng southwestLatLng = new LatLng(30.67, 104.07);
LatLng northeastLatLng = new LatLng(30.77, 104.17);
LatLngBounds latLngBounds = new LatLngBounds(southwestLatLng, northeastLatLng);
aMap.setMapStatusLimits(latLngBounds);
} else {
aMap.setMapStatusLimits(null);
}
}
/** 地图截屏 */
private void screenCapture() {
aMap.getMapScreenShot(new AMap.OnMapScreenShotListener() {
@Override
public void onMapScreenShot(Bitmap bitmap) {
}
@Override
public void onMapScreenShot(Bitmap bitmap, int i) {
if (null == bitmap) {
return;
}
try {