Examples
Getting started (3)Create a simple map viewDynamically build a map viewSupport map fragmentDynamic styling (30)HillshadingAdd a vector tile sourceAdd a new layer below labelsAnimate marker positionAdjust a layer's opacityShow time lapseAnimated image source (GIF)Icon update based on API responseChange a layer's colorChange a map's languageColor dependent on zoom levelClick to add photoIndoor MapMarker following routeStyle with missing iconSymbol layer iconsPicture in picturePulsing layer opacityAdd a WMS sourceText anchor positionButton interaction stylingOpacity fadeShow and hide layersSymbolLayer iconsAdjust text labelsMultiple text formatsTransparent render surfaceUse an image sourceAnimated icon movementVariable label placementExtrusions (5)Adjust light location and colorDisplay 3D building height based on vector dataExtrude polygons for 3D indoor mappingRotate and tilt with 3D buildingsUse GeoJSON data to set extrusion heightCamera (5)Animate the map cameraFit camera in bounding boxRestrict map panningRotating cameraZoom to show a cluster's leavesData visualization (33)Baseball spray chartDisplay water depthBiometric fingerprintCalendar integrationCircle icon toggleCircleLayer clustersCircle radiusCircle to icon transitionCreate a line layerCreate hotspots from pointsData time lapseShow heatmap dataDraw a GeoJSON lineDraw a polygon with holesDraw a polygonDraw multiple geometriesJoin local JSON data with vector tile geometriesLine gradientLine behind moving iconMultiple expressionsIcon setting based on Feature propertyOutlined polygon holeSatellite land selectShared preferencesSpinning iconStyle circles categoricallyStyle lines using an identity property functionToggle collision detectionSymbolLayer clusteringSymbol layer info windowZoom-based icon switchUpdate a choropleth layer by zoom levelStyling heatmapsOffline (3)Cache managementA simple offline mapOffline managerQuerying the map (3)Building outlineQuery a map featureSelect a buildingMap style (4)Style attributionLocal style or custom raster styleDefault stylesMapbox Studio styleUser interaction (12)Click on single layerFeature countDrawing search areaHighlighted lineSymbol layer icon size changeInset mapLocation pickerBackground fogRecyclerView DirectionsSearch again in an areaSnaking directionsRecyclerView interactionImage generation (2)Snapshot NotificationShare snapshot imageDevice location (7)Track device locationBasic pulsing locationLocation camera optionsCustom pulsing locationCustomized location iconShow a user's location on a map fragmentShow a user's location

Search again in an area

activity_query_redo_search_in_area
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="41.017538"
mapbox:mapbox_cameraTargetLng="28.974306"
mapbox:mapbox_cameraZoom="10.218183"
/>
<Button
android:id="@+id/redo_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@color/mapboxPurpleDark"
android:elevation="4dp"
android:padding="16dp"
android:text="@string/redo_search_button"
android:textColor="@color/mapboxWhite"
android:visibility="visible" />
</FrameLayout>
RedoSearchInAreaActivity.java
package com.mapbox.mapboxandroiddemo.examples.query;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
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.FillLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import java.util.List;
import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential;
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity;
/**
* Use MapboxMap.queryRenderedFeatures() to find and highlight certain features within the map viewport.
* The search button re-appears when the map is moved.
*/
public class RedoSearchInAreaActivity extends AppCompatActivity implements OnMapReadyCallback,
MapboxMap.OnMoveListener {
private static final String FILL_LAYER_ID = "FILL_LAYER_ID";
private static final String GEO_JSON_SOURCE_ID = "GEO_JSON_SOURCE_ID";
private static final String ID_OF_LAYER_TO_HIGHLIGHT = "landuse";
private MapView mapView;
private MapboxMap mapboxMap;
private GeoJsonSource dataGeoJsonSource;
private Button redoSearchButton;
private boolean moveMapInstructionShown;
@Override
protected 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_redo_search_in_area);
redoSearchButton = findViewById(R.id.redo_search_button);
moveMapInstructionShown = false;
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull final Style style) {
mapboxMap.addOnMoveListener(RedoSearchInAreaActivity.this);
style.addSource(new GeoJsonSource(GEO_JSON_SOURCE_ID,
FeatureCollection.fromFeatures(new Feature[] {})));
style.addLayer(new FillLayer(FILL_LAYER_ID,
GEO_JSON_SOURCE_ID).withProperties(
fillOpacity(interpolate(exponential(1f), zoom(),
stop(3, 0f),
stop(8, .5f),
stop(15f, 1f))),
fillColor(Color.parseColor("#00F7FF"))
));
redoSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!moveMapInstructionShown) {
Toast.makeText(RedoSearchInAreaActivity.this,
R.string.move_the_map_and_research, Toast.LENGTH_SHORT).show();
moveMapInstructionShown = true;
}
FeatureCollection featureCollection = null;
if (style.getLayer(ID_OF_LAYER_TO_HIGHLIGHT) != null) {
featureCollection = FeatureCollection.fromFeatures(getFeaturesInViewport(ID_OF_LAYER_TO_HIGHLIGHT));
} else {
Toast.makeText(RedoSearchInAreaActivity.this,
String.format(getString(R.string.layer_not_found), ID_OF_LAYER_TO_HIGHLIGHT),
Toast.LENGTH_SHORT).show();
}
// Retrieve and update the GeoJSON source used in the FillLayer
dataGeoJsonSource = style.getSourceAs(GEO_JSON_SOURCE_ID);
if (dataGeoJsonSource != null && featureCollection != null) {
dataGeoJsonSource.setGeoJson(featureCollection);
}
redoSearchButton.setVisibility(View.INVISIBLE);
}
});
}
});
}
/**
* Perform feature query within the viewport.
*/
private List<Feature> getFeaturesInViewport(String layerName) {
RectF rectF = new RectF(mapView.getLeft(),
mapView.getTop(), mapView.getRight(), mapView.getBottom());
return mapboxMap.queryRenderedFeatures(rectF, layerName);
}
@Override
public void onMoveEnd(MoveGestureDetector detector) {
redoSearchButton.setVisibility(View.VISIBLE);
}
@Override
public void onMoveBegin(MoveGestureDetector detector) {
// Left empty on purpose
}
@Override
public void onMove(MoveGestureDetector detector) {
// Left empty on purpose
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mapboxMap != null) {
mapboxMap.removeOnMoveListener(this);
}
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Was this page helpful?