summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QuerySourceFeaturesActivity.java
blob: 13833a260bde4809b8af1d0d905aa06bcdbb92ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.mapbox.mapboxsdk.testapp.activity.feature;

import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import com.google.gson.JsonObject;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;

import java.util.List;

import static com.mapbox.mapboxsdk.style.expressions.Expression.eq;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
import static com.mapbox.mapboxsdk.style.expressions.Expression.neq;

/**
 * Test activity showcasing using the query source features API to query feature counts
 */
public class QuerySourceFeaturesActivity extends AppCompatActivity {

  public MapView mapView;
  private MapboxMap mapboxMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_query_source_features);

    // Initialize map as normal
    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(map -> {
      this.mapboxMap = map;
      mapboxMap.getStyle(this::initStyle);
      mapboxMap.setStyle(Style.MAPBOX_STREETS);
    });
  }

  private void initStyle(Style style) {
    JsonObject properties = new JsonObject();
    properties.addProperty("key1", "value1");
    final GeoJsonSource source = new GeoJsonSource("test-source",
      FeatureCollection.fromFeatures(new Feature[] {
        Feature.fromGeometry(Point.fromLngLat(17.1, 51), properties),
        Feature.fromGeometry(Point.fromLngLat(17.2, 51), properties),
        Feature.fromGeometry(Point.fromLngLat(17.3, 51), properties),
        Feature.fromGeometry(Point.fromLngLat(17.4, 51), properties),
      }));
    style.addSource(source);

    Expression visible = eq(get("key1"), literal("value1"));
    Expression invisible = neq(get("key1"), literal("value1"));

    CircleLayer layer = new CircleLayer("test-layer", source.getId())
      .withFilter(visible);
    style.addLayer(layer);

    // Add a click listener
    mapboxMap.addOnMapClickListener(point -> {
      // Query
      List<Feature> features = source.querySourceFeatures(eq(get("key1"), literal("value1")));
      Toast.makeText(QuerySourceFeaturesActivity.this, String.format("Found %s features",
        features.size()), Toast.LENGTH_SHORT).show();

      return false;
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setColorFilter(ContextCompat.getColor(this, R.color.primary));
    fab.setOnClickListener(view -> {
      Expression visibility = layer.getFilter();
      if (visibility != null && visibility.equals(visible)) {
        layer.setFilter(invisible);
        fab.setImageResource(R.drawable.ic_layers_clear);
      } else {
        layer.setFilter(visible);
        fab.setImageResource(R.drawable.ic_layers);
      }
    });
  }

  @Override
  protected void onStart() {
    super.onStart();
    mapView.onStart();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mapView.onResume();
  }

  @Override
  protected void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  protected void onStop() {
    super.onStop();
    mapView.onStop();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
  }

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }
}