MapboxMap自带了定位组件LocationComponent,本文就是基于此控件实现功能。
改之前: 改之后:
一.查看具体实现的代码发现其资源内置在各个drawable目录下
二. 可以通过LocationComponentOptions对LocationComponent进行自定义。
public class MapActivity extends AppCompatActivity {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView = findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
initLocationComponent(mapboxMap, style);
}
});
}
});
}
/**
* Initialize the Maps SDK's LocationComponent
*/
private void initLocationComponent(MapboxMap mapboxMap, Style loadedMapStyle) {
// Get an instance of the component
LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Check if permissions are enabled and if not request
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
/*定制在LocationComponent允许一些自定义。您可以设置drawable,不透明度,颜色等。*/
LocationComponentOptions customLocationComponentOptions = LocationComponentOptions.builder(this)
.accuracyColor(Color.TRANSPARENT)
.foregroundDrawable(R.drawable.mapbox_user_icon)
.foregroundDrawableStale(R.drawable.mapbox_user_icon_stale)
.bearingDrawable(R.drawable.mapbox_user_bearing_icon)
.build();
LocationComponentActivationOptions locationComponentActivationOptions =
LocationComponentActivationOptions.builder(this, loadedMapStyle)
.locationComponentOptions(customLocationComponentOptions)
.build();
locationComponent.activateLocationComponent(locationComponentActivationOptions);
locationComponent.setLocationComponentEnabled(true);
/* CameraMode 描述
* NONE 没有摄像头跟踪。
* NONE_COMPASS 相机不跟踪位置,但会跟踪罗盘方位。
* NONE_GPS 相机不会跟踪位置,但会跟踪GPS Location方位。
* TRACKING 摄像机跟踪设备位置,不考虑轴承。
* TRACKING_COMPASS 摄像机跟踪设备位置,跟踪设备指南针提供的轴承。
* TRACKING_GPS 摄像机跟踪设备位置,轴承由标准化提供Location#getBearing()。
* TRACKING_GPS_NORTH 摄像机跟踪设备位置,轴承始终设置为北(0)。
*/
locationComponent.setCameraMode(CameraMode.TRACKING);
/*
* RenderMode类包含所述装置位置图像预设选项
* NORMAL 此模式显示设备位置,忽略指南针和GPS方位(无箭头渲染)
* COMPASS 此模式显示设备位置,以及考虑设备指南针的箭头。
* GPS 此模式显示设备位置,图标轴承从Location提供的更新更LocationComponent。
*
*/
locationComponent.setRenderMode(RenderMode.COMPASS);
}
}
@Override
protected void onStart() {
super.onStart();
if (mapView != null) {
mapView.onStart();
}
}
@Override
public void onResume() {
super.onResume();
if (mapView != null) {
mapView.onResume();
}
}
@Override
public void onPause() {
super.onPause();
if (mapView != null) {
mapView.onPause();
}
}
@Override
protected void onStop() {
super.onStop();
if (mapView != null) {
mapView.onStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mapView != null) {
mapView.onDestroy();
}
}
@Override
public void onLowMemory() {
super.onLowMemory();
if (mapView != null) {
mapView.onLowMemory();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mapView != null) {
mapView.onSaveInstanceState(outState);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".ui.set.offline.MapActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
三.资源:
3.1 放在res的drawable-xxhdpi下
R.drawable.mapbox_user_bearing_icon
R.drawable.mapbox_user_icon
R.drawable.mapbox_user_icon_stale
3.2放在res的drawable-anydpi-v21下
mapbox_user_bearing_icon.xml
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="44dp"
android:height="44dp"
android:viewportHeight="36.0"
android:viewportWidth="36.0">
<path
android:fillColor="#40C4AA"
android:pathData="M18,0L23,7L13,7L18,0ZM22.8,7C21.33,6.36 19.71,6 18,6C16.29,6 14.67,6.36 13.2,7L22.8,7Z"/>
</vector>
mapbox_user_icon.xml
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="14dp"
android:height="14dp"
android:viewportHeight="14.0"
android:viewportWidth="14.0">
<path
android:fillColor="#40C4AA"
android:pathData="M7,7m-7,0a7,7 0,1 1,14 0a7,7 0,1 1,-14 0"/>
</vector>
mapbox_user_icon_stale.xml
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="14dp"
android:height="14dp"
android:viewportHeight="14.0"
android:viewportWidth="14.0">
<path
android:fillColor="#40C4AA"
android:pathData="M7,7m-7,0a7,7 0,1 1,14 0a7,7 0,1 1,-14 0"/>
</vector>