カスタマイズされた位置アイコン
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:mapbox="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"> <com.mapbox.mapboxsdk.maps.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_cameraZoom="15" /> <com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/back_to_camera_tracking_mode"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="end|bottom"android:layout_marginEnd="16dp"android:layout_marginRight="16dp"android:layout_marginBottom="16dp"app:srcCompat="@drawable/ic_my_location"mapbox:fab_size="normal" /> </FrameLayout>
LocationComponentOptionsActivity.java
package com.mapbox.mapboxandroiddemo.examples.location; import android.graphics.Color;import android.os.Bundle;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.view.View;import android.widget.Toast; import com.mapbox.android.core.permissions.PermissionsListener;import com.mapbox.android.core.permissions.PermissionsManager;import com.mapbox.mapboxandroiddemo.R;import com.mapbox.mapboxsdk.Mapbox;import com.mapbox.mapboxsdk.location.LocationComponent;import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions;import com.mapbox.mapboxsdk.location.LocationComponentOptions;import com.mapbox.mapboxsdk.location.OnCameraTrackingChangedListener;import com.mapbox.mapboxsdk.location.OnLocationClickListener;import com.mapbox.mapboxsdk.location.modes.CameraMode;import com.mapbox.mapboxsdk.location.modes.RenderMode;import com.mapbox.mapboxsdk.maps.MapView;import com.mapbox.mapboxsdk.maps.MapboxMap;import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;import com.mapbox.mapboxsdk.maps.Style; import java.util.List; /*** Use the LocationLayerOptions class to customize the LocationComponent's device location icon.*/public class LocationComponentOptionsActivity extends AppCompatActivity implementsOnMapReadyCallback, OnLocationClickListener, PermissionsListener, OnCameraTrackingChangedListener { private PermissionsManager permissionsManager;private MapView mapView;private MapboxMap mapboxMap;private LocationComponent locationComponent;private boolean isInTrackingMode; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); // Mapbox access token is configured here. This needs to be called either in your application// object or in the same activity which contains the mapview.Mapbox.getInstance(this, getString(R.string.access_token)); // This contains the MapView in XML and needs to be called after the access token is configured.setContentView(R.layout.activity_location_component_options); mapView = findViewById(R.id.mapView);mapView.onCreate(savedInstanceState);mapView.getMapAsync(this);} @Overridepublic void onMapReady(@NonNull MapboxMap mapboxMap) {this.mapboxMap = mapboxMap;mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {@Overridepublic void onStyleLoaded(@NonNull Style style) {enableLocationComponent(style);}});} @SuppressWarnings( {"MissingPermission"})private void enableLocationComponent(@NonNull Style loadedMapStyle) {// Check if permissions are enabled and if not requestif (PermissionsManager.areLocationPermissionsGranted(this)) { // Create and customize the LocationComponent's optionsLocationComponentOptions customLocationComponentOptions = LocationComponentOptions.builder(this).elevation(5).accuracyAlpha(.6f).accuracyColor(Color.RED).foregroundDrawable(R.drawable.android_custom_location_icon).build(); // Get an instance of the componentlocationComponent = mapboxMap.getLocationComponent(); LocationComponentActivationOptions locationComponentActivationOptions =LocationComponentActivationOptions.builder(this, loadedMapStyle).locationComponentOptions(customLocationComponentOptions).build(); // Activate with optionslocationComponent.activateLocationComponent(locationComponentActivationOptions); // Enable to make component visiblelocationComponent.setLocationComponentEnabled(true); // Set the component's camera modelocationComponent.setCameraMode(CameraMode.TRACKING); // Set the component's render modelocationComponent.setRenderMode(RenderMode.COMPASS); // Add the location icon click listenerlocationComponent.addOnLocationClickListener(this); // Add the camera tracking listener. Fires if the map camera is manually moved.locationComponent.addOnCameraTrackingChangedListener(this); findViewById(R.id.back_to_camera_tracking_mode).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (!isInTrackingMode) {isInTrackingMode = true;locationComponent.setCameraMode(CameraMode.TRACKING);locationComponent.zoomWhileTracking(16f);Toast.makeText(LocationComponentOptionsActivity.this, getString(R.string.tracking_enabled),Toast.LENGTH_SHORT).show();} else {Toast.makeText(LocationComponentOptionsActivity.this, getString(R.string.tracking_already_enabled),Toast.LENGTH_SHORT).show();}}}); } else {permissionsManager = new PermissionsManager(this);permissionsManager.requestLocationPermissions(this);}} @SuppressWarnings( {"MissingPermission"})@Overridepublic void onLocationComponentClick() {if (locationComponent.getLastKnownLocation() != null) {Toast.makeText(this, String.format(getString(R.string.current_location),locationComponent.getLastKnownLocation().getLatitude(),locationComponent.getLastKnownLocation().getLongitude()), Toast.LENGTH_LONG).show();}} @Overridepublic void onCameraTrackingDismissed() {isInTrackingMode = false;} @Overridepublic void onCameraTrackingChanged(int currentMode) {// Empty on purpose} @Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);} @Overridepublic void onExplanationNeeded(List<String> permissionsToExplain) {Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();} @Overridepublic void onPermissionResult(boolean granted) {if (granted) {mapboxMap.getStyle(new Style.OnStyleLoaded() {@Overridepublic void onStyleLoaded(@NonNull Style style) {enableLocationComponent(style);}});} else {Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();finish();}} @SuppressWarnings( {"MissingPermission"})protected void onStart() {super.onStart();mapView.onStart();} @Overrideprotected void onResume() {super.onResume();mapView.onResume();} @Overrideprotected void onPause() {super.onPause();mapView.onPause();} @Overrideprotected void onStop() {super.onStop();mapView.onStop();} @Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mapView.onSaveInstanceState(outState);} @Overrideprotected void onDestroy() {super.onDestroy();mapView.onDestroy();} @Overridepublic void onLowMemory() {super.onLowMemory();mapView.onLowMemory();}}