Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 275a628

Browse files
author
Langston Smith
authored
Refactored TilequeryActivity with appropriate null checks (#1185)
1 parent 19ebcee commit 275a628

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/TilequeryActivity.java

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
<!-- Tilequery API -->
312312
<string name="api_response">Tilequery API response:</string>
313313
<string name="api_error">Error with the request. Do you have internet access?</string>
314+
<string name="no_tilequery_response_features_toast">No features found where you tapped. Find and tap on a building outline.</string>
314315

315316
<!-- Magic window -->
316317
<string name="could_not_get_location">Couldn\'t retrieve the device\'s location</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<string name="activity_java_services_geocoding_url" translatable="false">https://i.imgur.com/9xl3EF8.png</string>
8989
<string name="activity_java_services_isochrone_url" translatable="false">https://i.imgur.com/eUByGvt.png</string>
9090
<string name="activity_java_services_isochrone_with_seekbar_url" translatable="false">https://i.imgur.com/hCVswtE.png</string>
91-
<string name="activity_java_services_tilequery_url" translatable="false">https://i.imgur.com/mjUbe2H.png</string>
91+
<string name="activity_java_services_tilequery_url" translatable="false">http://i.imgur.com/VkOCYwq.jpg</string>
9292
<string name="activity_java_services_simplify_polyline_url" translatable="false">http://i.imgur.com/uATgul1.png</string>
9393
<string name="activity_java_services_map_matching_url" translatable="false">https://i.imgur.com/ig8gGnY.png</string>
9494
<string name="activity_java_services_turf_ring_url" translatable="false">https://i.imgur.com/nG8xeXH.png</string>

0 commit comments

Comments
 (0)