MapBox地图——改变定位点颜色

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>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值