-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[android] - add style loading callback #8291
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -909,6 +909,52 @@ private void setApiBaseUrl(@NonNull MapboxMapOptions options) { | |
*/ | ||
@UiThread | ||
public void setStyleUrl(@NonNull String url) { | ||
setStyleUrl(url, null); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Loads a new map style from the specified URL and receive a callback when the style has finished loading. | ||
* </p> | ||
* {@code url} can take the following forms: | ||
* <ul> | ||
* <li>{@code Style.*}: load one of the bundled styles in {@link Style}.</li> | ||
* <li>{@code mapbox://styles/<user>/<style>}: | ||
* retrieves the style from a <a href="https://www.mapbox.com/account/">Mapbox account.</a> | ||
* {@code user} is your username. {@code style} is the ID of your custom | ||
* style created in <a href="https://www.mapbox.com/studio">Mapbox Studio</a>.</li> | ||
* <li>{@code http://...} or {@code https://...}: | ||
* retrieves the style over the Internet from any web server.</li> | ||
* <li>{@code asset://...}: | ||
* reads the style from the APK {@code assets/} directory. | ||
* This is used to load a style bundled with your app.</li> | ||
* <li>{@code null}: loads the default {@link Style#MAPBOX_STREETS} style.</li> | ||
* </ul> | ||
* <p> | ||
* This method is asynchronous and will return immediately before the style finishes loading. | ||
* If you wish to wait for the map to finish loading listen for the {@link MapView#DID_FINISH_LOADING_MAP} event. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a |
||
* </p> | ||
* If the style fails to load or an invalid style URL is set, the map view will become blank. | ||
* An error message will be logged in the Android logcat and {@link MapView#DID_FAIL_LOADING_MAP} event will be | ||
* sent. | ||
* | ||
* @param url The URL of the map style | ||
* @param callback Callback that is invoked when the style has loaded. | ||
* @see Style | ||
*/ | ||
@UiThread | ||
public void setStyleUrl(@NonNull final String url, @Nullable final OnStyleLoadedListener callback) { | ||
if (callback != null) { | ||
nativeMapView.addOnMapChangedListener(new MapView.OnMapChangedListener() { | ||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be my Android newbness speaking, but do you need to call the superclass implementation if you are overriding here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @incanus It's an interface, not a class |
||
public void onMapChanged(@MapView.MapChange int change) { | ||
if (change == MapView.DID_FINISH_LOADING_STYLE) { | ||
callback.onStyleLoaded(url); | ||
nativeMapView.removeOnMapChangedListener(this); | ||
} | ||
} | ||
}); | ||
} | ||
nativeMapView.setStyleUrl(url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not call this class's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @incanus That would be a circular reference and would cause a stack overflow |
||
} | ||
|
||
|
@@ -2071,6 +2117,16 @@ public interface SnapshotReadyCallback { | |
void onSnapshotReady(Bitmap snapshot); | ||
} | ||
|
||
/** | ||
* Interface definintion for a callback to be invoked when the style has finished loading. | ||
*/ | ||
public interface OnStyleLoadedListener { | ||
/** | ||
* Invoked when the style has finished loading. | ||
*/ | ||
void onStyleLoaded(String style); | ||
} | ||
|
||
// | ||
// Used for instrumentation testing | ||
// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This (and the similar comment(s) above) could be worded better—makes it sound like immediately before instead of what actually happens, which is immediately and before the style finishes loading.