強調された線
<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns: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.query.HighlightedLineActivity"> <com.mapbox.mapboxsdk.maps.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_cameraTargetLat="50.844999"mapbox:mapbox_cameraTargetLng="4.356449"mapbox:mapbox_cameraZoom="15"/> </FrameLayout>
HighlightedLineActivity.java
package com.mapbox.mapboxandroiddemo.examples.query; import android.graphics.Color;import android.graphics.PointF;import android.graphics.RectF;import android.os.Bundle;import android.widget.Toast; import com.mapbox.geojson.Feature;import com.mapbox.mapboxandroiddemo.R;import com.mapbox.mapboxsdk.Mapbox;import com.mapbox.mapboxsdk.geometry.LatLng;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.LineLayer;import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; import java.net.URI;import java.net.URISyntaxException;import java.util.List; import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import timber.log.Timber; import static com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND;import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND;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.lineCap;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; /*** Detect a tap on a data-driven styled line and add a line behind it for a highlight effect*/public class HighlightedLineActivity extends AppCompatActivity implementsOnMapReadyCallback, MapboxMap.OnMapClickListener { private MapView mapView;private MapboxMap mapboxMap;private LineLayer backgroundLineLayer; @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_query_highlighted_line); mapView = findViewById(R.id.mapView);mapView.onCreate(savedInstanceState);mapView.getMapAsync(this);} @Overridepublic void onMapReady(@NonNull final MapboxMap mapboxMap) {this.mapboxMap = mapboxMap;mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {@Overridepublic void onStyleLoaded(@NonNull Style style) {initSource(style);initLayers(style);mapboxMap.addOnMapClickListener(HighlightedLineActivity.this);Toast.makeText(HighlightedLineActivity.this, getString(R.string.tap_on_line), Toast.LENGTH_SHORT).show();}});} @Overridepublic boolean onMapClick(@NonNull LatLng point) {mapboxMap.getStyle(new Style.OnStyleLoaded() {@Overridepublic void onStyleLoaded(@NonNull Style style) {// Detect whether a linestring was clicked onPointF pointf = mapboxMap.getProjection().toScreenLocation(point);RectF rectF = new RectF(pointf.x - 10, pointf.y - 10, pointf.x + 10, pointf.y + 10);List<Feature> featureList = mapboxMap.queryRenderedFeatures(rectF, "line-layer-id");if (featureList.size() > 0) {for (Feature feature : featureList) {GeoJsonSource source = style.getSourceAs("background-geojson-source-id");if (source != null) {source.setGeoJson(feature);backgroundLineLayer.setProperties(visibility(VISIBLE));}}}}});return true;} /*** Set up the line layer source*/private void initSource(@NonNull Style loadedMapStyle) {try {loadedMapStyle.addSource(new GeoJsonSource("source-id", new URI("asset://brussels_station_exits.geojson")));} catch (URISyntaxException exception) {Timber.d(exception);}loadedMapStyle.addSource(new GeoJsonSource("background-geojson-source-id"));} /*** Set up the main and background LineLayers*/private void initLayers(@NonNull Style loadedMapStyle) {// Add the regular LineLayerLineLayer routeLineLayer = new LineLayer("line-layer-id", "source-id");routeLineLayer.setProperties(lineWidth(9f),lineColor(Color.BLUE),lineCap(LINE_CAP_ROUND),lineJoin(LINE_JOIN_ROUND));loadedMapStyle.addLayer(routeLineLayer); // Add the background LineLayer that will act as the highlighting effectbackgroundLineLayer = new LineLayer("background-line-layer-id","background-geojson-source-id");backgroundLineLayer.setProperties(lineWidth(routeLineLayer.getLineWidth().value + 8),lineColor(Color.parseColor("#ff8402")),lineCap(LINE_CAP_ROUND),lineJoin(LINE_JOIN_ROUND),visibility(NONE));loadedMapStyle.addLayerBelow(backgroundLineLayer, "line-layer-id");} // Add the mapView lifecycle to the activity's lifecycle methods@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() {if (mapboxMap != null) {mapboxMap.removeOnMapClickListener(this);mapboxMap = null;backgroundLineLayer = null;}mapView.onDestroy();mapView = null;super.onDestroy();} @Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mapView.onSaveInstanceState(outState);}}