高德地图SDK Android版开发 3 地图图层
前言
本文主要介绍高德地图图层相关的功能和接口,以及使用方法。
高德地图图层
地图底图类型
2D地图图层
- 普通地图
- 卫星图(无注记)
- 英文地图(栅格+英文注记)
3D地图图层
- 普通地图(白昼地图)
- 卫星图
- 夜景地图
- 公交地图
- 导航地图
- 导航夜景地图
- 英文地图(栅格+英文注记)
地图类
- 高德地图有两个重要的类,一个是地图视图类,一个是地图类。
- 在前文中,通过创建
MapView
在应用中显示地图。 - 这里介绍下地图类的地图底图、路况相关的接口。
- 高德地图官方介绍
AMap
是地图的控制器类,用来操作地图。 AMap
所承载的工作包括:- 地图图层切换(如卫星图、黑夜地图)、
- 改变地图状态(地图旋转角度、俯仰角、中心点坐标和缩放级别)、
- 添加点标记(Marker)、
- 绘制几何图形(Polyline、Polygon、Circle)、
- 各类事件监听(点击、手势等)等,
- AMap 是地图 SDK 最重要的核心类,诸多操作都依赖它完成。。
- 下图列举了相关的接口。
图层类型常量
图层类型常量 | 说明 | 支持版本 |
---|---|---|
MAP_TYPE_NORMAL | 普通地图 | 2D、3D |
MAP_TYPE_SATELLITE | 卫星图 | 2D、3D |
MAP_TYPE_NIGHT | 夜景地图 | 3D |
MAP_TYPE_BUS | 公交地图 | 3D |
MAP_TYPE_NAVI | 导航地图 | 3D |
MAP_TYPE_NAVI_NIGHT | 导航夜景地图 | 3D |
接口
类型 | 方法 | 说明 |
---|---|---|
int | getMapType() | 获取地图当前类型 |
void | setMapType(int type) | 设置地图类型 |
路况图层
路况图依据实时路况数据渲染。
接口
类型 | 方法 | 说明 |
---|---|---|
boolean | isTrafficEnabled() | 获取是否打开交通图层 |
void | setTrafficEnable(boolean enabled) | 设置是否打开交通图层 |
英文地图
-
自 Android 3D 地图SDK V5.5.0起,地图支持切换中英文显示。
-
英文模式下,地图显示为栅格图数据
接口
类型 | 方法 | 说明 |
---|---|---|
boolean | setMapLanguage(String language) | 设置地图底图语言,目前支持中文底图AMap.CHINESE 和英文底图AMap.ENGLISH , |
关于3D地图
说明:
- 前文
高德地图SDK Android版开发 1 配置
已2D地图SDK为例,介绍使用高德地图SDK开发Android地图应用。 - 3D地图SDK与之类似,下载相应的地图包,并引入到项目中即可。
- 此外需注意在构造MapView之前必须进行合规检查,本文最后补充合规检查的接口。
示例代码
地图风格类
自定义MapStyle
类,实现以下方法。
2D地图地图风格类
package com.example.mapdemo;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
public class MapStyle {
private final BaiduMap map;
MapStyle(MapView mapView) {
map = mapView.getMap();
}
// TODO 添加地图底图类型方法
// TODO 添加实时路况方法
// TODO 添加英文地图方法
}
3D地图的地图风格类
package com.example.mapdemo;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
public class MapStyle {
private final BaiduMap map;
MapStyle(MapView mapView) {
map = mapView.getMap();
}
// TODO 添加地图底图类型方法
// TODO 添加实时路况方法
// TODO 添加英文地图方法
}
地图底图类型
2D地图仅包括普通地图和卫星图
/**
* 是否为普通地图
*/
public boolean isNormal() {
return map.getMapType() == AMap.MAP_TYPE_NORMAL;
}
/**
* 设置普通地图
*/
public void setNormal() {
map.setMapType(AMap.MAP_TYPE_NORMAL);
}
/**
* 是否为卫星图
*/
public boolean isSatellite() {
return map.getMapType() == AMap.MAP_TYPE_SATELLITE;
}
/**
* 设置卫星图
*/
public void setSatellite() {
map.setMapType(AMap.MAP_TYPE_SATELLITE);
}
/**
* 是否为夜景地图
*/
public boolean isNight() {
return map.getMapType() == AMap.MAP_TYPE_NIGHT;
}
/**
* 设置夜景地图
*/
public void setNight() {
map.setMapType(AMap.MAP_TYPE_NIGHT);
}
/**
* 是否为公交地图
*/
public boolean isBus() {
return map.getMapType() == AMap.MAP_TYPE_BUS;
}
/**
* 设置公交地图
*/
public void setBus() {
map.setMapType(AMap.MAP_TYPE_NAVI);
}
/**
* 是否为导航地图
*/
public boolean isNavi() {
return map.getMapType() == AMap.MAP_TYPE_NAVI;
}
/**
* 设置导航地图
*/
public void setNavi() {
map.setMapType(AMap.MAP_TYPE_NAVI);
}
/**
* 是否为导航夜景地图
*/
public boolean isNaviNight() {
return map.getMapType() == AMap.MAP_TYPE_NAVI_NIGHT;
}
/**
* 设置导航夜景地图
*/
public void setNaviNight() {
map.setMapType(AMap.MAP_TYPE_NAVI_NIGHT);
}
实时路况
/**
* 获取是否打开交通图层
* @return 是否打开交通图层
*/
public boolean isTrafficEnabled() {
return map.isTrafficEnabled();
}
/**
* 设置是否打开交通图层
* @param enabled 是否打开交通图层
*/
public void setTrafficEnable(boolean enabled) {
map.setTrafficEnabled(enabled);
}
英文地图
/**
* 设置是否显示英文地图
* @param enabled 是否显示英文地图
*/
public void setEnglishEnabled(boolean enabled) {
if (enabled)
map.setMapLanguage(AMap.ENGLISH);
else
map.setMapLanguage(AMap.CHINESE);
}
页面布局
- 以3D地图为例
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottomView"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/bottomView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/map">
<RadioGroup
android:id="@+id/RadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_dark"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="@+id/normal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="setMapMode"
android:text="普通"
android:textColor="@color/white"
android:textStyle="bold" />
<RadioButton
android:id="@+id/statellite"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="setMapMode"
android:text="卫星"
android:textColor="@color/white"
android:textStyle="bold" />
<RadioButton
android:id="@+id/night"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="setMapMode"
android:text="夜"
android:textColor="@color/white"
android:textStyle="bold" />
<RadioButton
android:id="@+id/bus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="setMapMode"
android:text="公交"
android:textColor="@color/white"
android:textStyle="bold" />
<RadioButton
android:id="@+id/navi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="setMapMode"
android:text="导航"
android:textColor="@color/white"
android:textStyle="bold" />
<RadioButton
android:id="@+id/navi_night"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:onClick="setMapMode"
android:text="导航夜"
android:textColor="@color/white"
android:textStyle="bold" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:background="@android:color/background_dark"
android:orientation="horizontal"
android:paddingHorizontal="10dp">
<CheckBox
android:id="@+id/traffice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:onClick="setTraffic"
android:text="路况图"
android:textColor="@color/white"
android:textStyle="bold" />
<CheckBox
android:id="@+id/english"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="setEnglish"
android:text="英文地图"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
控件响应事件
地图底图类型
- 以3D地图为例
/**
* 设置底图显示模式
*/
public void setMapMode(View view) {
boolean checked = ((RadioButton) view).isChecked();
if (!checked)
return;
int id = view.getId();
if (id == R.id.normal) {
// 普通图
mapStyle.setNormal();
} else if (id == R.id.statellite) {
// 卫星图
mapStyle.setSatellite();
} else if (id == R.id.night) {
// 夜景地图
mapStyle.setNight();
} else if (id == R.id.bus) {
// 公交地图
mapStyle.setBus();
} else if (id == R.id.navi) {
// 导航地图
mapStyle.setNavi();
} else if (id == R.id.navi_night) {
// 导航夜景地图
mapStyle.setNaviNight();
}
}
实时路况
/**
* 设置是否显示交通图
*/
public void setTraffic(View view) {
mapStyle.setTrafficEnable(((CheckBox) view).isChecked());
}
英文地图
/**
* 设置是否显示英文地图
*/
public void setEnglish(View view) {
mapStyle.setEnglishEnabled(((CheckBox) view).isChecked());
}
运行效果图
- 以3D地图为例
附 隐私合规接口说明
- 更新隐私合规状态
/**
* 更新隐私合规状态,需要在初始化地图之前完成
* @param context: 上下文
* @param isContains: 隐私权政策是否包含高德开平隐私权政策 true是包含
* @param isShow: 隐私权政策是否弹窗展示告知用户 true是展示
* @since 8.1.0
*/
public static void updatePrivacyShow(Context context, boolean isContains, boolean isShow);
- 更新同意隐私状态
/**
* 更新同意隐私状态,需要在初始化地图之前完成
* @param context: 上下文
* @param isAgree: 隐私权政策是否取得用户同意 true是用户同意
* @since 8.1.0
*/
public static void updatePrivacyAgree(Context context, boolean isAgree);
- 使用示例(在初始化地图是前调用)
// 更新隐私合规状态,需要在初始化搜索之前完成
MapsInitializer.updatePrivacyShow(context, true, true);
// 更新同意隐私状态,需要在初始化地图之前完成
MapsInitializer.updatePrivacyAgree(tcontexthis, true);