レイヤーの表示と非表示
<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:mapbox="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=".examples.styles.ShowHideLayersActivity"> <com.mapbox.mapboxsdk.maps.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_cameraTargetLat="-13.517379300798098"mapbox:mapbox_cameraTargetLng="-71.97722138410576"mapbox:mapbox_cameraZoom="15"/> <com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab_layer_toggle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="end|bottom"android:layout_margin="16dp"android:src="@drawable/ic_layers_24dp"tools:backgroundTint="@color/colorAccent"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
ShowHideLayersActivity.java
package com.mapbox.mapboxandroiddemo.examples.styles; import android.graphics.Color;import android.os.Bundle;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.view.View; import com.mapbox.mapboxandroiddemo.R;import com.mapbox.mapboxsdk.Mapbox;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 com.mapbox.mapboxsdk.style.layers.CircleLayer;import com.mapbox.mapboxsdk.style.layers.Layer;import com.mapbox.mapboxsdk.style.sources.VectorSource; import static com.mapbox.mapboxsdk.style.layers.Property.NONE;import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; /*** Toggle visibility of a dataset with a Button.*/public class ShowHideLayersActivity extends AppCompatActivity { private MapView mapView;private MapboxMap map; @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_style_show_hide_layers);mapView = findViewById(R.id.mapView);mapView.onCreate(savedInstanceState);mapView.getMapAsync(new OnMapReadyCallback() {@Overridepublic void onMapReady(@NonNull MapboxMap mapboxMap) {map = mapboxMap;mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {@Overridepublic void onStyleLoaded(@NonNull Style style) {style.addSource(new VectorSource("museums_source", "mapbox://mapbox.2opop9hr")); CircleLayer museumsLayer = new CircleLayer("museums", "museums_source");museumsLayer.setSourceLayer("museum-cusco");museumsLayer.setProperties(visibility(VISIBLE),circleRadius(8f),circleColor(Color.argb(255, 55, 148, 179)));style.addLayer(museumsLayer); findViewById(R.id.fab_layer_toggle).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {toggleLayer();}});}});}});} @Overridepublic void onResume() {super.onResume();mapView.onResume();} @Overrideprotected void onStart() {super.onStart();mapView.onStart();} @Overrideprotected void onStop() {super.onStop();mapView.onStop();} @Overridepublic void onPause() {super.onPause();mapView.onPause();} @Overridepublic void onLowMemory() {super.onLowMemory();mapView.onLowMemory();} @Overrideprotected void onDestroy() {super.onDestroy();mapView.onDestroy();} @Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mapView.onSaveInstanceState(outState);} private void toggleLayer() {map.getStyle(new Style.OnStyleLoaded() {@Overridepublic void onStyleLoaded(@NonNull Style style) {Layer layer = style.getLayer("museums");if (layer != null) {if (VISIBLE.equals(layer.getVisibility().getValue())) {layer.setProperties(visibility(NONE));} else {layer.setProperties(visibility(VISIBLE));}}}});}}