summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java134
1 files changed, 97 insertions, 37 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
index edbb2764cd..7ae04d2a86 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
@@ -5,6 +5,7 @@ import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
+import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
import com.mapbox.mapboxsdk.style.light.Light;
@@ -15,6 +16,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import java.util.Map;
public class Style {
@@ -22,35 +24,23 @@ public class Style {
private final HashMap<String, Source> sources = new HashMap<>();
private final HashMap<String, Layer> layers = new HashMap<>();
- public Style(NativeMapView nativeMapView) {
- this.nativeMapView = nativeMapView;
- }
-
- public void loadStyle(@StyleUrl String styleUrl) {
- for (Source source : sources.values()) {
- if (source != null) {
- source.setDetached();
- nativeMapView.removeSource(source);
- }
- }
+ private final OnStyleLoaded onStyleLoaded;
+ private final Builder builder;
+ private boolean styleLoaded;
- for (Layer layer : layers.values()) {
- if (layer != null) {
- layer.setDetached();
- nativeMapView.removeLayer(layer);
- }
- }
-
- nativeMapView.setStyleUrl(styleUrl);
+ public Style(Builder builder, NativeMapView nativeMapView, OnStyleLoaded styleLoaded) {
+ this.builder = builder;
+ this.nativeMapView = nativeMapView;
+ this.onStyleLoaded = styleLoaded;
}
- @Nullable
- public String getUrl(){
+ @NonNull
+ public String getUrl() {
return nativeMapView.getStyleUrl();
}
- @Nullable
- public String getJson(){
+ @NonNull
+ public String getJson() {
return nativeMapView.getStyleJson();
}
@@ -336,6 +326,59 @@ public class Style {
return nativeMapView.getLight();
}
+
+ /**
+ * Called when the underlying map will start loading a new style. This method will clean up this style
+ * by setting the java sources and layers in a detached state and removing them from core.
+ */
+ public void onWillStartLoadingStyle() {
+ for (Source source : sources.values()) {
+ if (source != null) {
+ source.setDetached();
+ nativeMapView.removeSource(source);
+ }
+ }
+
+ for (Layer layer : layers.values()) {
+ if (layer != null) {
+ layer.setDetached();
+ nativeMapView.removeLayer(layer);
+ }
+ }
+
+ sources.clear();
+ layers.clear();
+ }
+
+ /**
+ * Called when the underlying map has finished loading this style.
+ * This method will add all components added to the builder that were defined with the 'with' prefix.
+ */
+ public void onDidFinishLoadingStyle() {
+ if (!styleLoaded) {
+ styleLoaded = true;
+ for (Source source : builder.sources) {
+ addSource(source);
+ }
+
+ for (Layer layer : builder.layers) {
+ addLayerBelow(layer, MapboxConstants.LAYER_ID_ANNOTATIONS);
+ }
+
+ for (Map.Entry<String, Bitmap> stringBitmapEntry : builder.images.entrySet()) {
+ addImage(stringBitmapEntry.getKey(), stringBitmapEntry.getValue());
+ }
+
+ if (builder.transitionOptions != null) {
+ setTransition(builder.transitionOptions);
+ }
+
+ if (onStyleLoaded != null) {
+ onStyleLoaded.onStyleLoaded(this);
+ }
+ }
+ }
+
//
// Builder
//
@@ -347,9 +390,9 @@ public class Style {
private List<Source> sources = new ArrayList<>();
// TODO allow adding below and at index
private List<Layer> layers = new ArrayList<>();
+ private HashMap<String, Bitmap> images = new HashMap<>();
private TransitionOptions transitionOptions;
-
/**
* <p>
* Loads a new map style asynchronous from the specified URL.
@@ -380,8 +423,8 @@ public class Style {
* @param url The URL of the map style
* @see Style
*/
- public Builder withStyleUrl(@StyleUrl String url) {
- this.styleUrl = styleUrl;
+ public Builder fromUrl(String url) {
+ this.styleUrl = url;
return this;
}
@@ -393,7 +436,7 @@ public class Style {
* will be triggered.
* </p>
*/
- public Builder withStyleJson(String styleJson) {
+ public Builder fromJson(String styleJson) {
this.styleJson = styleJson;
return this;
}
@@ -403,6 +446,7 @@ public class Style {
return this;
}
+ // TODO add layer at support!
public Builder withLayer(Layer layer) {
layers.add(layer);
return this;
@@ -413,25 +457,41 @@ public class Style {
return this;
}
- Style build(NativeMapView nativeMapView) {
- Style style = new Style(nativeMapView);
- for (Source source : sources) {
- style.addSource(source);
- }
+ // TODO add SDF support!
+ public Builder withImage(String name, Bitmap image) {
+ images.put(name, image);
+ return this;
+ }
- for (Layer layer : layers) {
- style.addLayer(layer);
+ Style build(NativeMapView nativeMapView, OnStyleLoaded styleLoaded) {
+ Style style = new Style(this, nativeMapView, styleLoaded);
+ nativeMapView.setStyle(style);
+
+ if (styleUrl != null) {
+ nativeMapView.setStyleUrl(styleUrl);
+ } else if (styleJson != null) {
+ nativeMapView.setStyleJson(styleJson);
+ } else {
+ // user didn't provide a `from` component,
+ // flag the style as loaded,
+ // add components defined added using the `with` prefix.
+ style.onDidFinishLoadingStyle();
}
- if (transitionOptions != null) {
- style.setTransition(transitionOptions);
- }
return style;
}
}
//
+ //
+ //
+
+ public interface OnStyleLoaded {
+ void onStyleLoaded(Style style);
+ }
+
+ //
// Style URL constants
//