显示设备位置
以地图注释的形式显示用户当前位置是基于位置的应用程序的一个流行的、通常也是关键的功能。Maps SDK的 LocationComponent 利用 Maps SDK 的运行时样式化功能,将设备位置图标显示在地图本身内,而不是作为Android视图显示在顶部。 Mapbox map层和图层样式允许您精确控制如何在地图上显示设备的位置。
注:这个 LocationComponent 已经取代了现在已经废弃的 Location Layer 插件。 LocationComponent 集成到Android的 Maps SDK 中,因此不需要添加任何额外的依赖项。它带来了与插件相同的功能。
请求位置权限
在使用 LocationComponent 之前,您需要请求android系统的位置权限。如果您构建的Android项目以API级别23或更高为目标,那么您的应用程序将需要在运行时请求权限。在您的活动中直接处理这个问题会生成样板代码,并且通常很难管理。阅读更多关于在Android的PermissionsManager类中使用Mapbox核心库的信息
自定义
LocationComponent 可以通过许多不同的方式进行定制。您可以设置图像绘制、透明度、颜色等等。XML属性的完整列表,以此设计 LocationComponent 的样式。
如果您喜欢以编程方式自定义LocationComponent,可以使用 LocationComponentOptions 类。创建一个 LocationComponentOptions 对象,然后使用您喜欢的任何 LocationComponentOptions.builder() 的各种方法。然后在 LocationComponentActivationOptions#LocationComponentOptions() 中使用已构建的 LocationComponentOptions 对象作为参数,或者稍后将其作为 LocationComponent#applyStyle() 方法的参数传递。示例:
// 在 locationComponentOptions 中设计样式
LocationComponentOptions locationComponentOptions = LocationComponentOptions.builder(this)
.layerBelow(layerId)
.foregroundDrawable(R.drawable.drawable_name)
.bearingTintColor(int color)
.accuracyAlpha(float)
.build();
// 作为参数传入
LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
.builder(this, style)
.locationComponentOptions(locationComponentOptions)
.build();
locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(locationComponentActivationOptions);
激活
MapboxMap#getLocationComponent() 获取组件, LocationComponent#activateLocationComponent() 激活它。 activateLocationComponent() 方法需要一个已构建的 LocationComponentActivationOptions 类。LocationComponentActivationOptions类提供了一种方便的方法来设置激活选项,如是否使用默认的 LocationEngine 或构建的 LocationComponentOptions 对象。使用通过 Maps SDK 提供的构建器模式创建一个 LocationComponentActivationOptions 类。示例如下:
LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
.builder(this, style)
.locationComponentOptions(locationComponentOptions)
.useDefaultLocationEngine(true)
.build();
一旦用户授予了位置权限并且地图已完全加载,就检索并激活 LocationComponent 。
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent();
}
});
}
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent() {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Get an instance of the component
LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Activate with a built LocationComponentActivationOptions object
locationComponent.activateLocationComponent(LocationComponentActivationOptions.builder(this, style).build());
// Enable to make component visible
locationComponent.setLocationComponentEnabled(true);
// Set the component's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
// Set the component's render mode
locationComponent.setRenderMode(RenderMode.COMPASS);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
Visibility
有一个方法可以在激活后启用或禁用 LocationComponent 的可见性。方法 setLocationComponentEnabled()
需要一个真/假布尔参数。当设置为 false 时,该方法将隐藏设备位置图标,并停止地图相机动画的发生。
locationComponent.setLocationComponentEnabled(true);
渲染模式
RenderMode 类包含设备位置图像的预置选项,一个有三个选项:
- NORMAL:此模式显示设备位置,忽略罗盘和GPS方位(没有呈现箭头)。
- COMPASS:此模式显示设备位置,以及考虑到设备罗盘的箭头。
- GPS:此模式显示设备位置,带有从提供给 LocationComponent 的位置更新中更新的图标。
注:实际的设备位置图标可以通过 LocationComponentOptions#foregroundDrawable()
和 LocationComponentOptions#backgroundDrawable()
等方法进行高度定制。
相机模式
方法 LocationComponent # setCameraMode (@CameraMode.Mode int cameraMode)
允许开发人员在设备位置改变时设置特定的相机跟踪指令。
locationComponent.setCameraMode(CameraMode.TRACKING);
CameraMode 一共有7种可选的模式:
- NONE:没有摄像头跟踪。
- NONE_COMPASS:相机不跟踪位置,但跟踪罗盘方位。
- NONE_GPS:相机不跟踪位置,但跟踪GPS定位轴承。
- TRACKING:相机跟踪设备位置,不考虑轴承。
- TRACKING_COMPASS:摄像机跟踪设备位置,跟踪由设备罗盘提供的方位。
- TRACKING_GPS:摄像机跟踪设备位置,轴承由标准化位置 #getBearing() 提供。
- TRACKING_GPS_NORTH:相机跟踪设备位置,轴承始终设置为北(0)。
传统的相机切换将被取消时,任何相机模式,除了相机模式#NONE,是参与。使用 LocationComponent#zoomWhileTracking 和 LocationComponent#tiltWhileTracking 操作处于跟踪状态的相机。使用这两种方法结合传统的相机切换和 MapboxMap#CancelableCallback 来安排流体切换。
当第一次实例化 LocationComponent 时,map的最大/最小缩放级别将分别设置为 ocationcomponentoptions #MAX_ZOOM_DEFAULT 和 LocationComponentOptions#MIN_ZOOM_DEFAULT 。使用 LocationComponentOptions 类中的 LocationComponentOptions#maxZoom()
和LocationComponentOptions#minZoom()
方法调整缩放范围。
摄像头跟踪时的手势阈值
LocationComponent 与 Android库的Mapbox手势 集成在一起。该组件将调整相机的焦点,并增加阈值,使相机操作,如放大和缩小,而不打破跟踪。启用此功能是显式的opt-in,因为它覆盖了使用MapboxMap#setGesturesManager(AndroidGesturesManager, boolean, boolean)设置的自定义手势检测实现集。
要启用该特性,请使用LocationComponentOptions#trackingGesturesManagement(boolean)
。
您可以调整需要超过的阈值,以中断对一个指针手势(如平移地图、双击放大)和多个指针手势(如缩放手势、双击缩小)的跟踪:
LocationComponentOptions#trackingInitialMoveThreshold(float)
调整相机跟踪所需的最小单指针移动像素。LocationComponentOptions#trackingMultiFingerMoveThreshold(float)
调整需要打破相机跟踪的最小多点移动像素(例如在缩放手势)。- 如果超过了这两个阈值,并且取消跟踪,开发人员可以使用
OnCameraTrackingChangedListener
来侦听,使用方法如下:
LocationComponent locationComponent = mapboxMap.getLocationComponent();
locationComponent.addOnCameraTrackingChangedListener(new OnCameraTrackingChangedListener() {
@Override
public void onCameraTrackingDismissed() {
// Tracking has been dismissed
}
@Override
public void onCameraTrackingChanged(int currentMode) {
// CameraMode has been updated
}
});