Query Features in box

This example shows how to Query features in the box on MapView

  • Query features about existing layer id and filter

  • Query features about custom layer id

Query Features in box

For all code examples, refer to Android Maps SDK Code Examples

activity_query_feature_and_symbol.xml view source

1
<?xml version="1.0" encoding="utf-8"?>
2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
xmlns:app="http://schemas.android.com/apk/res-auto"
4
android:layout_width="match_parent"
5
android:layout_height="match_parent">
6
7
<ai.nextbillion.maps.core.MapView
8
android:id="@+id/mapView"
9
android:layout_width="match_parent"
10
android:layout_height="match_parent"
11
app:nbmap_cameraTargetLat="52.0907"
12
app:nbmap_cameraTargetLng="5.1214"
13
app:nbmap_cameraZoom="16" />
14
15
<FrameLayout
16
android:id="@+id/selection_box"
17
android:layout_width="150dp"
18
android:layout_height="150dp"
19
android:layout_centerInParent="true"
20
android:alpha="0.3"
21
android:background="@color/palette_mint_100" />
22
23
</RelativeLayout>

QueryFeatureAndSymbolActivity view source

1
package ai.nextbillion;
2
3
import android.graphics.Bitmap;
4
import android.graphics.BitmapFactory;
5
import android.graphics.Color;
6
import android.graphics.RectF;
7
import android.os.Bundle;
8
import android.view.View;
9
import android.widget.Toast;
10
11
import java.io.IOException;
12
import java.util.List;
13
14
import ai.nextbillion.kits.geojson.Feature;
15
import ai.nextbillion.kits.geojson.FeatureCollection;
16
import ai.nextbillion.maps.camera.CameraPosition;
17
import ai.nextbillion.maps.camera.CameraUpdateFactory;
18
import ai.nextbillion.maps.core.MapView;
19
import ai.nextbillion.maps.core.NextbillionMap;
20
import ai.nextbillion.maps.core.Style;
21
import ai.nextbillion.maps.geometry.LatLng;
22
import ai.nextbillion.maps.style.expressions.Expression;
23
import ai.nextbillion.maps.style.layers.FillLayer;
24
import ai.nextbillion.maps.style.layers.Layer;
25
import ai.nextbillion.maps.style.layers.SymbolLayer;
26
import ai.nextbillion.maps.style.sources.GeoJsonSource;
27
import ai.nextbillion.utils.ResourceUtils;
28
import androidx.appcompat.app.AppCompatActivity;
29
30
import static ai.nextbillion.maps.style.expressions.Expression.get;
31
import static ai.nextbillion.maps.style.expressions.Expression.literal;
32
import static ai.nextbillion.maps.style.expressions.Expression.lt;
33
import static ai.nextbillion.maps.style.expressions.Expression.toNumber;
34
import static ai.nextbillion.maps.style.layers.PropertyFactory.fillColor;
35
import static ai.nextbillion.maps.style.layers.PropertyFactory.iconImage;
36
37
/**
38
* Demo's query rendered features
39
*/
40
public class QueryFeatureAndSymbolActivity extends AppCompatActivity {
41
42
public MapView mapView;
43
private NextbillionMap nextbillionMap;
44
45
@Override
46
protected void onCreate(Bundle savedInstanceState) {
47
super.onCreate(savedInstanceState);
48
setContentView(R.layout.activity_query_feature_and_symbol);
49
50
final View selectionBox = findViewById(R.id.selection_box);
51
52
// Initialize map as normal
53
mapView = (MapView) findViewById(R.id.mapView);
54
mapView.onCreate(savedInstanceState);
55
mapView.getMapAsync(nextbillionMap -> {
56
QueryFeatureAndSymbolActivity.this.nextbillionMap = nextbillionMap;
57
58
CameraPosition position = new CameraPosition.Builder()
59
.target(new LatLng(14.589503119868022, 120.98188196701062))
60
.zoom(16)
61
.bearing(0)
62
.tilt(30)
63
.build();
64
nextbillionMap.animateCamera(CameraUpdateFactory.newCameraPosition(position));
65
66
// Add layer / source
67
final GeoJsonSource source = new GeoJsonSource("highlighted-shapes-source");
68
final Layer layer = new FillLayer("highlighted-shapes-layer", "highlighted-shapes-source")
69
.withProperties(fillColor(Color.YELLOW));
70
71
selectionBox.setOnClickListener(view -> {
72
// Query
73
int top = selectionBox.getTop() - mapView.getTop();
74
int left = selectionBox.getLeft() - mapView.getLeft();
75
RectF box = new RectF(left, top, left + selectionBox.getWidth(), top + selectionBox.getHeight());
76
77
Expression filter = lt(toNumber(get("height")), literal(10));
78
List<Feature> features = nextbillionMap.queryRenderedFeatures(box, filter, "building");
79
List<Feature> symbols = nextbillionMap.queryRenderedFeatures(box, "symbols-layer");
80
81
// Show count
82
Toast.makeText(
83
QueryFeatureAndSymbolActivity.this,
84
String.format("%s buildings in box\n%s symbols in box", features.size(), symbols.size()),
85
Toast.LENGTH_SHORT).show();
86
87
// Update source data
88
source.setGeoJson(FeatureCollection.fromFeatures(features));
89
});
90
91
nextbillionMap.setStyle(new Style.Builder()
92
.fromUri(StyleConstants.NBMAP_STREETS)
93
.withSource(source)
94
.withLayer(layer)
95
, style -> addSymbolLayer(style));
96
97
});
98
}
99
100
public void addSymbolLayer(Style style) {
101
try {
102
String testPoints = ResourceUtils.readRawResource(mapView.getContext(), R.raw.test_points_utrecht);
103
Bitmap markerImage = BitmapFactory.decodeResource(getResources(), R.drawable.nbmap_marker_icon_default);
104
105
style.addImage("test-icon", markerImage);
106
style.addSource(new GeoJsonSource("symbols-source", testPoints));
107
style.addLayer(new SymbolLayer("symbols-layer", "symbols-source")
108
.withProperties(
109
iconImage("test-icon")
110
));
111
} catch (IOException exception) {
112
exception.printStackTrace();
113
}
114
}
115
116
public NextbillionMap getNextbillionMap() {
117
return nextbillionMap;
118
}
119
120
@Override
121
protected void onStart() {
122
super.onStart();
123
mapView.onStart();
124
}
125
126
@Override
127
protected void onResume() {
128
super.onResume();
129
mapView.onResume();
130
}
131
132
@Override
133
protected void onPause() {
134
super.onPause();
135
mapView.onPause();
136
}
137
138
@Override
139
protected void onStop() {
140
super.onStop();
141
mapView.onStop();
142
}
143
144
@Override
145
protected void onSaveInstanceState(Bundle outState) {
146
super.onSaveInstanceState(outState);
147
mapView.onSaveInstanceState(outState);
148
}
149
150
@Override
151
protected void onDestroy() {
152
super.onDestroy();
153
mapView.onDestroy();
154
}
155
156
@Override
157
public void onLowMemory() {
158
super.onLowMemory();
159
mapView.onLowMemory();
160
}
161
}
162

The given code is an Android activity that demonstrates how to initialize a MapView, query features on a map, and display the results.

Initializing the MapView:

  • The onCreate method initializes the MapView by setting the content view and obtaining a reference to it using findViewById.

  • The onCreate method also calls mapView.onCreate(savedInstanceState) to create the MapView and mapView.getMapAsync(this) to get a reference to the NextbillionMap instance when it is ready.

Querying Features of an Existing Layer:

  • The code sets up a map and adds a layer called "highlighted-shapes-layer" with a source named "highlighted-shapes-source".

  • The selectionBox view is used to define a rectangular area on the map.

  • When the selectionBox is clicked, the queryRenderedFeatures method is called to query the features within the defined area.

  • The queried features are filtered based on a condition (in this case, the height is less than 10) using an Expression.

  • The queried features are displayed in a toast message.

Querying Features of a Custom Layer:

  • The code adds a custom symbol layer to the map, which is based on a GeoJSON source named "symbols-source".

  • The addSymbolLayer method reads GeoJSON data from a resource file and adds it as a source and layer to the map style.

  • The queryRenderedFeatures method is used to query the features within the defined area for the "symbols-layer" specifically.

  • The queried features are displayed in a toast message.

Other Lifecycle Methods: The code also includes various lifecycle methods (onStart, onResume, onPause, onStop, onSaveInstanceState, onDestroy, onLowMemory) that should be implemented when using the MapView to properly manage its lifecycle and handle configuration changes.

Note: The code uses the Nextbillion Maps SDK and NextbillionMap class to handle map-related operations. It also utilizes the Style class to define the map's visual style, GeoJsonSource class to add data sources, and various layer classes to display map features.

© 2024 NextBillion.ai all rights reserved.