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

Commit 0a004ce

Browse files
author
Langston Smith
authored
refactoring to replace style.isFullyLoaded() with getStyle callback (#1192)
1 parent c1509af commit 0a004ce

12 files changed

+240
-215
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import android.graphics.Color;
44
import android.os.Bundle;
5-
import androidx.annotation.NonNull;
6-
import androidx.appcompat.app.AppCompatActivity;
75
import android.widget.Toast;
86

97
import com.mapbox.api.directions.v5.DirectionsCriteria;
@@ -26,6 +24,8 @@
2624
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
2725
import com.mapbox.mapboxsdk.utils.BitmapUtils;
2826

27+
import androidx.annotation.NonNull;
28+
import androidx.appcompat.app.AppCompatActivity;
2929
import retrofit2.Call;
3030
import retrofit2.Callback;
3131
import retrofit2.Response;
@@ -74,7 +74,6 @@ protected void onCreate(Bundle savedInstanceState) {
7474
mapView.getMapAsync(new OnMapReadyCallback() {
7575
@Override
7676
public void onMapReady(@NonNull MapboxMap mapboxMap) {
77-
7877
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
7978
@Override
8079
public void onStyleLoaded(@NonNull Style style) {
@@ -89,7 +88,7 @@ public void onStyleLoaded(@NonNull Style style) {
8988
initLayers(style);
9089

9190
// Get the directions route from the Mapbox Directions API
92-
getRoute(style, origin, destination);
91+
getRoute(mapboxMap, origin, destination);
9392
}
9493
});
9594
}
@@ -139,12 +138,11 @@ private void initLayers(@NonNull Style loadedMapStyle) {
139138
/**
140139
* Make a request to the Mapbox Directions API. Once successful, pass the route to the
141140
* route layer.
142-
*
141+
* @param mapboxMap the Mapbox map object that the route will be drawn on
143142
* @param origin the starting point of the route
144143
* @param destination the desired finish point of the route
145144
*/
146-
private void getRoute(@NonNull final Style style, Point origin, Point destination) {
147-
145+
private void getRoute(MapboxMap mapboxMap, Point origin, Point destination) {
148146
client = MapboxDirections.builder()
149147
.origin(origin)
150148
.destination(destination)
@@ -176,17 +174,23 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons
176174
getString(R.string.directions_activity_toast_message),
177175
currentRoute.distance()), Toast.LENGTH_SHORT).show();
178176

179-
if (style.isFullyLoaded()) {
180-
// Retrieve and update the source designated for showing the directions route
181-
GeoJsonSource source = style.getSourceAs(ROUTE_SOURCE_ID);
182-
183-
// Create a LineString with the directions route's geometry and
184-
// reset the GeoJSON source for the route LineLayer source
185-
if (source != null) {
186-
Timber.d("onResponse: source != null");
187-
source.setGeoJson(FeatureCollection.fromFeature(
188-
Feature.fromGeometry(LineString.fromPolyline(currentRoute.geometry(), PRECISION_6))));
189-
}
177+
if (mapboxMap != null) {
178+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
179+
@Override
180+
public void onStyleLoaded(@NonNull Style style) {
181+
182+
// Retrieve and update the source designated for showing the directions route
183+
GeoJsonSource source = style.getSourceAs(ROUTE_SOURCE_ID);
184+
185+
// Create a LineString with the directions route's geometry and
186+
// reset the GeoJSON source for the route LineLayer source
187+
if (source != null) {
188+
Timber.d("onResponse: source != null");
189+
source.setGeoJson(FeatureCollection.fromFeature(
190+
Feature.fromGeometry(LineString.fromPolyline(currentRoute.geometry(), PRECISION_6))));
191+
}
192+
}
193+
});
190194
}
191195
}
192196

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33

44
import android.os.Bundle;
5-
import androidx.annotation.NonNull;
6-
import androidx.appcompat.app.AppCompatActivity;
75
import android.view.View;
86
import android.widget.Button;
97
import android.widget.ImageView;
@@ -23,7 +21,8 @@
2321
import com.mapbox.mapboxsdk.utils.MathUtils;
2422
import com.squareup.picasso.Picasso;
2523

26-
import timber.log.Timber;
24+
import androidx.annotation.NonNull;
25+
import androidx.appcompat.app.AppCompatActivity;
2726

2827
/**
2928
* Use the Mapbox Static Image API found inside the Mapbox Java SDK to create a URL. After receiving the newly
@@ -36,7 +35,6 @@ public class StaticImageActivity extends AppCompatActivity implements
3635
private MapView mapView;
3736
private MapboxMap mapboxMap;
3837
private ImageView staticMapImageView;
39-
private Button createStaticImageButton;
4038
private RadioGroup mapStyleRadioGroup;
4139

4240
@Override
@@ -64,7 +62,7 @@ public void onMapReady(@NonNull final MapboxMap mapboxMap) {
6462
private void initViews() {
6563
staticMapImageView = findViewById(R.id.static_map_imageview);
6664

67-
createStaticImageButton = findViewById(R.id.create_static_image_button);
65+
Button createStaticImageButton = findViewById(R.id.create_static_image_button);
6866
createStaticImageButton.setOnClickListener(new View.OnClickListener() {
6967
@Override
7068
public void onClick(View view) {
@@ -75,10 +73,9 @@ public void onClick(View view) {
7573
@Override
7674
public void onStyleLoaded(@NonNull Style style) {
7775
// one of the radio buttons is checked
78-
Timber.d("onClick: initViews style.getUrl() = %s", style.getUrl());
7976
Picasso.with(StaticImageActivity.this).load(takeSnapshot(
8077
mapboxMap.getCameraPosition(),
81-
style.getUrl().contains("mapbox/dark-v")
78+
style.getUri().contains("mapbox/dark-v")
8279
? StaticMapCriteria.DARK_STYLE :
8380
StaticMapCriteria.STREET_STYLE,
8481
(int) MathUtils.clamp(findViewById(R.id.static_map_imageview).getMeasuredWidth(), 0, 1280),

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/labs/CalendarIntegrationActivity.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
import android.os.Build;
1414
import android.os.Bundle;
1515
import android.provider.CalendarContract;
16-
import androidx.annotation.NonNull;
17-
import androidx.core.app.ActivityCompat;
18-
import androidx.core.content.ContextCompat;
19-
import androidx.appcompat.app.AppCompatActivity;
2016
import android.widget.Toast;
2117

2218
import com.mapbox.android.core.permissions.PermissionsListener;
@@ -42,6 +38,10 @@
4238
import java.util.Calendar;
4339
import java.util.List;
4440

41+
import androidx.annotation.NonNull;
42+
import androidx.appcompat.app.AppCompatActivity;
43+
import androidx.core.app.ActivityCompat;
44+
import androidx.core.content.ContextCompat;
4545
import retrofit2.Call;
4646
import retrofit2.Callback;
4747
import retrofit2.Response;
@@ -167,7 +167,7 @@ public void getCalendarData(@NonNull Style style) {
167167
while (cur.moveToNext()) {
168168
if (index <= 80) {
169169
if (cur.getString(EVENT_LOCATION_INDEX) != null && !cur.getString(EVENT_LOCATION_INDEX).isEmpty()) {
170-
makeMapboxGeocodingRequest(style, cur.getString(TITLE_INDEX), cur.getString(EVENT_LOCATION_INDEX));
170+
makeMapboxGeocodingRequest(cur.getString(TITLE_INDEX), cur.getString(EVENT_LOCATION_INDEX));
171171
} else {
172172
Timber.d("getCalendarData: location is null or empty");
173173
}
@@ -188,8 +188,7 @@ public void getCalendarData(@NonNull Style style) {
188188
* eventually displayed in a Toast when a calendar event icon is tapped on.
189189
* @param eventLocation the event's location. This text is used in the Mapbox geocoding search
190190
*/
191-
private void makeMapboxGeocodingRequest(@NonNull final Style style, final String eventTitle,
192-
final String eventLocation) {
191+
private void makeMapboxGeocodingRequest(final String eventTitle, final String eventLocation) {
193192
try {
194193
// Build a Mapbox geocoding request
195194
MapboxGeocoding client = MapboxGeocoding.builder()
@@ -206,20 +205,25 @@ public void onResponse(Call<GeocodingResponse> call,
206205
if (results.size() > 0) {
207206
// Get the first Feature from the successful geocoding response
208207
CarmenFeature feature = results.get(0);
209-
if (feature != null && style.isFullyLoaded()) {
210-
LatLng featureLatLng = new LatLng(feature.center().latitude(), feature.center().longitude());
211-
Feature singleFeature = Feature.fromGeometry(Point.fromLngLat(featureLatLng.getLongitude(),
212-
featureLatLng.getLatitude()));
213-
singleFeature.addStringProperty(PROPERTY_TITLE, eventTitle);
214-
singleFeature.addStringProperty(PROPERTY_LOCATION, eventLocation);
215-
featureList.add(singleFeature);
216-
featureCollection = FeatureCollection.fromFeatures(featureList);
217-
GeoJsonSource source = style.getSourceAs(geojsonSourceId);
218-
if (source != null) {
219-
source.setGeoJson(featureCollection);
220-
} else {
221-
Timber.d("onResponse: listOfCalendarEvents == null");
222-
}
208+
if (feature != null) {
209+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
210+
@Override
211+
public void onStyleLoaded(@NonNull Style style) {
212+
LatLng featureLatLng = new LatLng(feature.center().latitude(), feature.center().longitude());
213+
Feature singleFeature = Feature.fromGeometry(Point.fromLngLat(featureLatLng.getLongitude(),
214+
featureLatLng.getLatitude()));
215+
singleFeature.addStringProperty(PROPERTY_TITLE, eventTitle);
216+
singleFeature.addStringProperty(PROPERTY_LOCATION, eventLocation);
217+
featureList.add(singleFeature);
218+
featureCollection = FeatureCollection.fromFeatures(featureList);
219+
GeoJsonSource source = style.getSourceAs(geojsonSourceId);
220+
if (source != null) {
221+
source.setGeoJson(featureCollection);
222+
} else {
223+
Timber.d("onResponse: listOfCalendarEvents == null");
224+
}
225+
}
226+
});
223227
}
224228
} else {
225229
Toast.makeText(CalendarIntegrationActivity.this, R.string.no_results,

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/labs/LocationPickerActivity.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import android.graphics.BitmapFactory;
44
import android.os.Bundle;
5-
import androidx.annotation.NonNull;
6-
import androidx.core.content.ContextCompat;
7-
import androidx.appcompat.app.AppCompatActivity;
85
import android.view.Gravity;
96
import android.view.View;
107
import android.view.ViewGroup;
@@ -36,6 +33,9 @@
3633

3734
import java.util.List;
3835

36+
import androidx.annotation.NonNull;
37+
import androidx.appcompat.app.AppCompatActivity;
38+
import androidx.core.content.ContextCompat;
3939
import retrofit2.Call;
4040
import retrofit2.Callback;
4141
import retrofit2.Response;
@@ -54,7 +54,6 @@
5454
*/
5555
public class LocationPickerActivity extends AppCompatActivity implements PermissionsListener, OnMapReadyCallback {
5656

57-
private static final String TAG = "LocationPickerActivity";
5857
private static final String DROPPED_MARKER_LAYER_ID = "DROPPED_MARKER_LAYER_ID";
5958
private MapView mapView;
6059
private MapboxMap mapboxMap;
@@ -134,7 +133,7 @@ public void onClick(View view) {
134133
}
135134

136135
// Use the map camera target's coordinates to make a reverse geocoding search
137-
reverseGeocode(style, Point.fromLngLat(mapTargetLatLng.getLongitude(), mapTargetLatLng.getLatitude()));
136+
reverseGeocode(Point.fromLngLat(mapTargetLatLng.getLongitude(), mapTargetLatLng.getLatitude()));
138137

139138
} else {
140139

@@ -241,11 +240,10 @@ public void onPermissionResult(boolean granted) {
241240
/**
242241
* This method is used to reverse geocode where the user has dropped the marker.
243242
*
244-
* @param style style
245243
* @param point The location to use for the search
246244
*/
247245

248-
private void reverseGeocode(@NonNull final Style style, final Point point) {
246+
private void reverseGeocode(final Point point) {
249247
try {
250248
MapboxGeocoding client = MapboxGeocoding.builder()
251249
.accessToken(getString(R.string.access_token))
@@ -262,11 +260,16 @@ public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse>
262260
CarmenFeature feature = results.get(0);
263261

264262
// If the geocoder returns a result, we take the first in the list and show a Toast with the place name.
265-
if (style.isFullyLoaded() && style.getLayer(DROPPED_MARKER_LAYER_ID) != null) {
266-
Toast.makeText(LocationPickerActivity.this,
267-
String.format(getString(R.string.location_picker_place_name_result),
268-
feature.placeName()), Toast.LENGTH_SHORT).show();
269-
}
263+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
264+
@Override
265+
public void onStyleLoaded(@NonNull Style style) {
266+
if (style.getLayer(DROPPED_MARKER_LAYER_ID) != null) {
267+
Toast.makeText(LocationPickerActivity.this,
268+
String.format(getString(R.string.location_picker_place_name_result),
269+
feature.placeName()), Toast.LENGTH_SHORT).show();
270+
}
271+
}
272+
});
270273

271274
} else {
272275
Toast.makeText(LocationPickerActivity.this,

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/labs/MovingIconWithTrailingLineActivity.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public class MovingIconWithTrailingLineActivity extends AppCompatActivity {
6969
private List<Point> routeCoordinateList;
7070
private List<Point> markerLinePointList = new ArrayList<>();
7171
private int routeIndex;
72-
72+
private Point originPoint = Point.fromLngLat(38.7508, 9.0309);
73+
private Point destinationPoint = Point.fromLngLat(38.795902, 8.984467);
7374
private Animator currentAnimator;
7475

7576
@Override
@@ -94,10 +95,7 @@ public void onMapReady(MapboxMap mapboxMap) {
9495
@Override
9596
public void onStyleLoaded(@NonNull Style style) {
9697
// Use the Mapbox Directions API to get a directions route
97-
getRoute(style,
98-
Point.fromLngLat(38.7508, 9.0309), // coffee shop
99-
Point.fromLngLat(38.795902, 8.984467) // airport
100-
);
98+
getRoute(originPoint, destinationPoint);
10199
}
102100
});
103101
}
@@ -173,7 +171,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
173171
* @param origin the starting point of the route
174172
* @param destination the desired finish point of the route
175173
*/
176-
private void getRoute(@NonNull final Style style, final Point origin, final Point destination) {
174+
private void getRoute(final Point origin, final Point destination) {
177175
MapboxDirections client = MapboxDirections.builder()
178176
.origin(origin)
179177
.destination(destination)
@@ -199,17 +197,19 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons
199197

200198
// Get the directions route
201199
DirectionsRoute currentRoute = response.body().routes().get(0);
202-
203-
if (style.isFullyLoaded()) {
204-
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(
200+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
201+
@Override
202+
public void onStyleLoaded(@NonNull Style style) {
203+
mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(
205204
new LatLngBounds.Builder()
206-
.include(new LatLng(origin.latitude(), origin.longitude()))
207-
.include(new LatLng(destination.latitude(), destination.longitude()))
208-
.build(), 50), 5000);
205+
.include(new LatLng(origin.latitude(), origin.longitude()))
206+
.include(new LatLng(destination.latitude(), destination.longitude()))
207+
.build(), 50), 5000);
209208

210-
initData(style,FeatureCollection.fromFeature(
209+
initData(style,FeatureCollection.fromFeature(
211210
Feature.fromGeometry(LineString.fromPolyline(currentRoute.geometry(), PRECISION_6))));
212-
}
211+
}
212+
});
213213
}
214214

215215
@Override

0 commit comments

Comments
 (0)