diff options
author | Tobrun <tobrun@mapbox.com> | 2016-03-02 11:21:03 +0100 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2016-03-03 07:38:23 +0100 |
commit | d994f2de0fc9331d712b8dddded9e3792efa154f (patch) | |
tree | c01e03a50bc9367262bf3d994fa69960968306a9 /platform/android/MapboxGLAndroidSDK | |
parent | fd123de8ad060441cdd318091531653df4cbcc79 (diff) | |
download | qtlocation-mapboxgl-d994f2de0fc9331d712b8dddded9e3792efa154f.tar.gz |
[android] #4166 - move style url to map view for improved async loading
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK')
4 files changed, 94 insertions, 21 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java index 3d103f6f8c..61d4af51cd 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java @@ -9,6 +9,7 @@ import android.view.View; import android.view.ViewGroup; import com.mapbox.mapboxsdk.R; +import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.utils.ApiAccess; /** diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 3a689ac97b..cd2d0c68ac 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -55,6 +55,7 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ListView; import android.widget.ZoomButtonsController; + import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector; import com.almeros.android.multitouch.gesturedetectors.ShoveGestureDetector; import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector; @@ -71,6 +72,7 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.MyBearingTracking; import com.mapbox.mapboxsdk.constants.MyLocationTracking; +import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.exceptions.IconBitmapChangedException; import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException; import com.mapbox.mapboxsdk.exceptions.TelemetryServiceNotConfiguredException; @@ -82,6 +84,7 @@ import com.mapbox.mapboxsdk.maps.widgets.UserLocationView; import com.mapbox.mapboxsdk.telemetry.MapboxEvent; import com.mapbox.mapboxsdk.telemetry.MapboxEventManager; import com.mapbox.mapboxsdk.utils.ApiAccess; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.nio.ByteBuffer; @@ -145,6 +148,10 @@ public class MapView extends FrameLayout { private int mContentPaddingRight; private int mContentPaddingBottom; + private OnMapReadyCallback mMapReadyCallback; + private String mStyleUrl; + private boolean mInitialLoad; + @UiThread public MapView(@NonNull Context context) { super(context); @@ -164,6 +171,7 @@ public class MapView extends FrameLayout { } private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) { + mInitialLoad = true; mOnMapChangedListener = new CopyOnWriteArrayList<>(); mMapboxMap = new MapboxMap(this); mIcons = new ArrayList<>(); @@ -307,6 +315,9 @@ public class MapView extends FrameLayout { */ @UiThread public void onCreate(@Nullable Bundle savedInstanceState) { + // Force a check for an access token + validateAccessToken(getAccessToken()); + if (savedInstanceState != null && savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) { // Get previous camera position @@ -370,16 +381,10 @@ public class MapView extends FrameLayout { // Force a check for Telemetry validateTelemetryServiceConfigured(); - // Force a check for an access token - validateAccessToken(getAccessToken()); - // Start Telemetry (authorization determined in initial MapboxEventManager constructor) MapboxEventManager.configureAndStartMapboxEventManager(getContext(), getAccessToken()); } - // Force a check for an access token - validateAccessToken(getAccessToken()); - // Initialize EGL mNativeMapView.initializeDisplay(); mNativeMapView.initializeContext(); @@ -392,6 +397,12 @@ public class MapView extends FrameLayout { reloadIcons(); reloadMarkers(); adjustTopOffsetPixels(); + if (mInitialLoad) { + mInitialLoad = false; + if (mMapReadyCallback != null) { + mMapReadyCallback.onMapReady(mMapboxMap); + } + } } } }); @@ -417,7 +428,7 @@ public class MapView extends FrameLayout { outState.putBoolean(MapboxConstants.STATE_HAS_SAVED_STATE, true); outState.putParcelable(MapboxConstants.STATE_CAMERA_POSITION, mMapboxMap.getCameraPosition()); outState.putBoolean(MapboxConstants.STATE_DEBUG_ACTIVE, mMapboxMap.isDebugActive()); - outState.putString(MapboxConstants.STATE_STYLE_URL, mMapboxMap.getStyleUrl()); + outState.putString(MapboxConstants.STATE_STYLE_URL, mStyleUrl); outState.putString(MapboxConstants.STATE_ACCESS_TOKEN, mMapboxMap.getAccessToken()); outState.putLong(MapboxConstants.STATE_DEFAULT_TRANSITION_DURATION, mNativeMapView.getDefaultTransitionDuration()); outState.putBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED, mMapboxMap.isMyLocationEnabled()); @@ -514,6 +525,11 @@ public class MapView extends FrameLayout { mNativeMapView.resume(); mNativeMapView.update(); mUserLocationView.resume(); + + if (mStyleUrl == null) { + // user has failed to supply a style url + setStyleUrl(Style.MAPBOX_STREETS); + } } /** @@ -676,10 +692,72 @@ public class MapView extends FrameLayout { // Styling // - void setStyleUrl(@NonNull String url) { + /** + * <p> + * Loads a new map style from the specified URL. + * </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. + * </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 + * @see Style + */ + public void setStyleUrl(@NonNull String url) { + mStyleUrl = url; mNativeMapView.setStyleUrl(url); } + /** + * <p> + * Loads a new map style from the specified bundled style. + * </p> + * <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. + * </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 style The bundled style. Accepts one of the values from {@link Style}. + * @see Style + */ + @UiThread + public void setStyle(@Style.StyleUrl String style) { + setStyleUrl(style); + } + + /** + * <p> + * Returns the map style currently displayed in the map view. + * </p> + * If the default style is currently displayed, a URL will be returned instead of null. + * + * @return The URL of the map style. + */ + @UiThread + @NonNull + public String getStyleUrl() { + return mStyleUrl; + } + // // Access token // @@ -855,9 +933,6 @@ public class MapView extends FrameLayout { } long addMarker(@NonNull Marker marker) { - if (mNativeMapView == null) { - return 0l; - } return mNativeMapView.addMarker(marker); } @@ -1204,6 +1279,7 @@ public class MapView extends FrameLayout { /** * Helper method for tracking DragEnd gesture event * See {@see MapboxEvent#TYPE_MAP_DRAGEND} + * * @param xCoordinate Original x screen coordinate at end of drag * @param yCoordinate Orginal y screen coordinate at end of drag */ @@ -2231,14 +2307,10 @@ public class MapView extends FrameLayout { */ @UiThread public void getMapAsync(@NonNull final OnMapReadyCallback callback) { - - // We need to put our callback on the message queue - post(new Runnable() { - @Override - public void run() { - callback.onMapReady(mMapboxMap); - } - }); + if (mMapReadyCallback == null && !mInitialLoad) { + callback.onMapReady(mMapboxMap); + } + mMapReadyCallback = callback; } MapboxMap getMapboxMap() { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 9530096131..c5050defee 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -420,7 +420,6 @@ public class MapboxMap { */ @UiThread public void setStyleUrl(@NonNull String url) { - mStyleUrl = url; mMapView.setStyleUrl(url); } @@ -454,7 +453,7 @@ public class MapboxMap { @UiThread @NonNull public String getStyleUrl() { - return mStyleUrl; + return mMapView.getStyleUrl(); } // diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java index 60d9320ee6..ff587ca4d2 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java @@ -9,6 +9,7 @@ import android.view.View; import android.view.ViewGroup; import com.mapbox.mapboxsdk.R; +import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.utils.ApiAccess; /** |