@@ -51,6 +51,8 @@ public class TilequeryActivity extends AppCompatActivity implements
5151 private static final String RESULT_GEOJSON_SOURCE_ID = "RESULT_GEOJSON_SOURCE_ID" ;
5252 private static final String CLICK_CENTER_GEOJSON_SOURCE_ID = "CLICK_CENTER_GEOJSON_SOURCE_ID" ;
5353 private static final String LAYER_ID = "LAYER_ID" ;
54+ private static final String RESULT_ICON_ID = "RESULT_ICON_ID" ;
55+ private static final String CLICK_ICON_ID = "CLICK_ICON_ID" ;
5456 private PermissionsManager permissionsManager ;
5557 private MapboxMap mapboxMap ;
5658 private MapView mapView ;
@@ -95,14 +97,14 @@ public void onStyleLoaded(@NonNull Style style) {
9597 * Add a map layer which will show a marker icon where the map was clicked
9698 */
9799 private void addClickLayer (@ NonNull Style loadedMapStyle ) {
98- loadedMapStyle .addImage ("CLICK-ICON-ID" , BitmapFactory .decodeResource (
100+ loadedMapStyle .addImage (CLICK_ICON_ID , BitmapFactory .decodeResource (
99101 TilequeryActivity .this .getResources (), R .drawable .red_marker ));
100102
101103 loadedMapStyle .addSource (new GeoJsonSource (CLICK_CENTER_GEOJSON_SOURCE_ID ,
102104 FeatureCollection .fromFeatures (new Feature [] {})));
103105
104106 loadedMapStyle .addLayer (new SymbolLayer ("click-layer" , CLICK_CENTER_GEOJSON_SOURCE_ID ).withProperties (
105- iconImage ("CLICK-ICON-ID" ),
107+ iconImage (CLICK_ICON_ID ),
106108 iconOffset (new Float [] {0f , -12f }),
107109 iconIgnorePlacement (true ),
108110 iconAllowOverlap (true )
@@ -114,15 +116,14 @@ private void addClickLayer(@NonNull Style loadedMapStyle) {
114116 */
115117 private void addResultLayer (@ NonNull Style loadedMapStyle ) {
116118 // Add the marker image to map
117- loadedMapStyle .addImage ("RESULT-ICON-ID" , BitmapFactory .decodeResource (
119+ loadedMapStyle .addImage (RESULT_ICON_ID , BitmapFactory .decodeResource (
118120 TilequeryActivity .this .getResources (), R .drawable .blue_marker ));
119121
120122 // Retrieve GeoJSON information from the Mapbox Tilequery API
121- loadedMapStyle .addSource (new GeoJsonSource (RESULT_GEOJSON_SOURCE_ID ,
122- FeatureCollection .fromFeatures (new Feature [] {})));
123+ loadedMapStyle .addSource (new GeoJsonSource (RESULT_GEOJSON_SOURCE_ID ));
123124
124125 loadedMapStyle .addLayer (new SymbolLayer (LAYER_ID , RESULT_GEOJSON_SOURCE_ID ).withProperties (
125- iconImage ("RESULT-ICON-ID" ),
126+ iconImage (RESULT_ICON_ID ),
126127 iconOffset (new Float [] {0f , -12f }),
127128 iconIgnorePlacement (true ),
128129 iconAllowOverlap (true )
@@ -132,30 +133,28 @@ private void addResultLayer(@NonNull Style loadedMapStyle) {
132133
133134 @ Override
134135 public boolean onMapClick (@ NonNull LatLng point ) {
136+ mapboxMap .getStyle (new Style .OnStyleLoaded () {
137+ @ Override
138+ public void onStyleLoaded (@ NonNull Style style ) {
139+ // Move and display the click center layer's red marker icon to wherever the map was clicked on
140+ GeoJsonSource clickLocationSource = style .getSourceAs (CLICK_CENTER_GEOJSON_SOURCE_ID );
141+ if (clickLocationSource != null ) {
142+ clickLocationSource .setGeoJson (Point .fromLngLat (point .getLongitude (), point .getLatitude ()));
143+ }
135144
136- Style style = mapboxMap .getStyle ();
137- if (style != null ) {
138- // Move and display the click center layer's red marker icon to wherever the map was clicked on
139- GeoJsonSource clickLocationSource = style .getSourceAs (CLICK_CENTER_GEOJSON_SOURCE_ID );
140- if (clickLocationSource != null ) {
141- clickLocationSource .setGeoJson (Point .fromLngLat (point .getLongitude (), point .getLatitude ()));
145+ // Use the map click location to make a Tilequery API call
146+ makeTilequeryApiCall (point );
142147 }
143-
144- // Use the map click location to make a Tilequery API call
145- makeTilequeryApiCall (style , point );
146-
147- return true ;
148- }
149-
150- return false ;
148+ });
149+ return true ;
151150 }
152151
153152 /**
154153 * Use the Java SDK's MapboxTilequery class to build a API request and use the API response
155154 *
156155 * @param point the center point that the the tilequery will originate from.
157156 */
158- private void makeTilequeryApiCall (@ NonNull final Style style , @ NonNull LatLng point ) {
157+ private void makeTilequeryApiCall (@ NonNull LatLng point ) {
159158 MapboxTilequery tilequery = MapboxTilequery .builder ()
160159 .accessToken (getString (R .string .access_token ))
161160 .mapIds ("mapbox.mapbox-streets-v7" )
@@ -170,10 +169,24 @@ private void makeTilequeryApiCall(@NonNull final Style style, @NonNull LatLng po
170169 tilequery .enqueueCall (new Callback <FeatureCollection >() {
171170 @ Override
172171 public void onResponse (Call <FeatureCollection > call , Response <FeatureCollection > response ) {
173- tilequeryResponseTextView .setText (response .body ().toJson ());
174- GeoJsonSource resultSource = style .getSourceAs (RESULT_GEOJSON_SOURCE_ID );
175- if (resultSource != null && response .body ().features () != null ) {
176- resultSource .setGeoJson (FeatureCollection .fromFeatures (response .body ().features ()));
172+ if (response .body () != null ) {
173+ FeatureCollection responseFeatureCollection = response .body ();
174+ tilequeryResponseTextView .setText (responseFeatureCollection .toJson ());
175+ mapboxMap .getStyle (new Style .OnStyleLoaded () {
176+ @ Override
177+ public void onStyleLoaded (@ NonNull Style style ) {
178+ GeoJsonSource resultSource = style .getSourceAs (RESULT_GEOJSON_SOURCE_ID );
179+ if (resultSource != null && responseFeatureCollection .features () != null ) {
180+ List <Feature > featureList = responseFeatureCollection .features ();
181+ if (featureList .isEmpty ()) {
182+ Toast .makeText (TilequeryActivity .this ,
183+ getString (R .string .no_tilequery_response_features_toast ), Toast .LENGTH_SHORT ).show ();
184+ } else {
185+ resultSource .setGeoJson (FeatureCollection .fromFeatures (featureList ));
186+ }
187+ }
188+ }
189+ });
177190 }
178191 }
179192
0 commit comments