Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[android] - add style loading callback #8291

Merged
merged 2 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

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.

* If you wish to wait for the map to finish loading listen for the {@link MapView#DID_FINISH_LOADING_MAP} event.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a , after loading.

* </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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call this class's setStyleUrl() in order to gate the native call through fewer (i.e. a single) places?

Copy link
Contributor

Choose a reason for hiding this comment

The 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

}

Expand Down Expand Up @@ -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
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ public void onClick(View view) {
if (currentStyleIndex == STYLES.length) {
currentStyleIndex = 0;
}
mapboxMap.setStyleUrl(STYLES[currentStyleIndex]);
mapboxMap.setStyleUrl(STYLES[currentStyleIndex], new MapboxMap.OnStyleLoadedListener() {
@Override
public void onStyleLoaded(String style) {
Timber.d("Style loaded %s", style);
}
});
}
}
});
Expand Down