[android] - Style.java
This commit is contained in:
@@ -76,10 +76,13 @@ public final class MapboxMap {
|
||||
@Nullable
|
||||
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
|
||||
|
||||
private com.mapbox.mapboxsdk.maps.Style style;
|
||||
|
||||
MapboxMap(NativeMapView map, Transform transform, UiSettings ui, Projection projection,
|
||||
OnGesturesManagerInteractionListener listener, AnnotationManager annotations,
|
||||
CameraChangeDispatcher cameraChangeDispatcher, MapChangeReceiver mapChangeReceiver) {
|
||||
this.nativeMapView = map;
|
||||
this.style = new com.mapbox.mapboxsdk.maps.Style(nativeMapView);
|
||||
this.uiSettings = ui;
|
||||
this.projection = projection;
|
||||
this.annotationManager = annotations.bind(this);
|
||||
@@ -101,6 +104,10 @@ public final class MapboxMap {
|
||||
setPrefetchesTiles(options);
|
||||
}
|
||||
|
||||
public com.mapbox.mapboxsdk.maps.Style getStyle(){
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the hosting Activity/Fragment onStart() method is called.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.mapbox.mapboxsdk.maps;
|
||||
|
||||
import com.mapbox.mapboxsdk.style.layers.Layer;
|
||||
import com.mapbox.mapboxsdk.style.sources.Source;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Style {
|
||||
|
||||
private NativeMapView nativeMapView;
|
||||
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(@com.mapbox.mapboxsdk.constants.Style.StyleUrl String styleUrl) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
nativeMapView.setStyleUrl(styleUrl);
|
||||
}
|
||||
|
||||
//
|
||||
// Source
|
||||
//
|
||||
|
||||
public void addSource(Source source) {
|
||||
sources.put(source.getId(), source);
|
||||
nativeMapView.addSource(source);
|
||||
}
|
||||
|
||||
public Source getSource(String id) {
|
||||
Source source = sources.get(id);
|
||||
if (source == null) {
|
||||
source = nativeMapView.getSource(id);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
//
|
||||
// Layer
|
||||
//
|
||||
|
||||
public void addLayer(Layer layer) {
|
||||
layers.put(layer.getId(), layer);
|
||||
nativeMapView.addLayer(layer);
|
||||
}
|
||||
|
||||
public Layer getLayer(String id) {
|
||||
Layer layer = layers.get(id);
|
||||
if (layer == null) {
|
||||
layer = nativeMapView.getLayer(id);
|
||||
}
|
||||
return layer;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ public abstract class Layer {
|
||||
private long nativePtr;
|
||||
@Keep
|
||||
private boolean invalidated;
|
||||
private boolean detached;
|
||||
|
||||
@Keep
|
||||
protected Layer(long nativePtr) {
|
||||
@@ -37,6 +38,10 @@ public abstract class Layer {
|
||||
}
|
||||
|
||||
public void setProperties(@NonNull PropertyValue<?>... properties) {
|
||||
if (detached) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkThread();
|
||||
if (properties.length == 0) {
|
||||
return;
|
||||
@@ -146,4 +151,12 @@ public abstract class Layer {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDetached() {
|
||||
detached = true;
|
||||
}
|
||||
|
||||
public boolean isDetached() {
|
||||
return detached;
|
||||
}
|
||||
}
|
||||
@@ -194,6 +194,9 @@ public class GeoJsonSource extends Source {
|
||||
* @param feature the GeoJSON {@link Feature} to set
|
||||
*/
|
||||
public void setGeoJson(Feature feature) {
|
||||
if (detached) {
|
||||
return;
|
||||
}
|
||||
checkThread();
|
||||
nativeSetFeature(feature);
|
||||
}
|
||||
@@ -205,6 +208,9 @@ public class GeoJsonSource extends Source {
|
||||
* @param geometry the GeoJSON {@link Geometry} to set
|
||||
*/
|
||||
public void setGeoJson(Geometry geometry) {
|
||||
if (detached) {
|
||||
return;
|
||||
}
|
||||
checkThread();
|
||||
nativeSetGeometry(geometry);
|
||||
}
|
||||
@@ -216,6 +222,9 @@ public class GeoJsonSource extends Source {
|
||||
* @param features the GeoJSON FeatureCollection
|
||||
*/
|
||||
public void setGeoJson(FeatureCollection features) {
|
||||
if (detached) {
|
||||
return;
|
||||
}
|
||||
checkThread();
|
||||
nativeSetFeatureCollection(features);
|
||||
}
|
||||
@@ -227,6 +236,9 @@ public class GeoJsonSource extends Source {
|
||||
* @param json the raw GeoJson FeatureCollection string
|
||||
*/
|
||||
public void setGeoJson(String json) {
|
||||
if (detached) {
|
||||
return;
|
||||
}
|
||||
checkThread();
|
||||
nativeSetGeoJsonString(json);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ public abstract class Source {
|
||||
@Keep
|
||||
private long nativePtr;
|
||||
|
||||
protected boolean detached;
|
||||
|
||||
/**
|
||||
* Internal use
|
||||
*
|
||||
@@ -76,4 +78,8 @@ public abstract class Source {
|
||||
@NonNull
|
||||
@Keep
|
||||
protected native String nativeGetAttribution();
|
||||
|
||||
public void setDetached() {
|
||||
detached = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.mapbox.mapboxsdk.style
|
||||
|
||||
import android.support.test.espresso.UiController
|
||||
import com.mapbox.geojson.Feature
|
||||
import com.mapbox.geojson.FeatureCollection
|
||||
import com.mapbox.geojson.Point
|
||||
import com.mapbox.mapboxsdk.constants.Style
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap
|
||||
import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage
|
||||
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
|
||||
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke
|
||||
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest
|
||||
import com.mapbox.mapboxsdk.testapp.activity.maplayout.SimpleMapActivity
|
||||
import org.junit.Test
|
||||
|
||||
class CustomGeometrySourceTest : BaseActivityTest() {
|
||||
|
||||
override fun getActivityClass(): Class<*> = SimpleMapActivity::class.java
|
||||
|
||||
@Test
|
||||
fun sourceUpdateAfterStyleChange() {
|
||||
validateTestSetup()
|
||||
invoke(mapboxMap) { uiController: UiController, mapboxMap: MapboxMap ->
|
||||
val style = mapboxMap.style
|
||||
|
||||
val source = GeoJsonSource(
|
||||
"source-id",
|
||||
FeatureCollection.fromFeature(
|
||||
Feature.fromGeometry(
|
||||
Point.fromLngLat(0.0, 0.0)
|
||||
)
|
||||
)
|
||||
)
|
||||
style.addSource(source)
|
||||
|
||||
style.loadStyle(Style.DARK)
|
||||
|
||||
uiController.loopMainThreadForAtLeast(300)
|
||||
|
||||
source.setGeoJson(
|
||||
FeatureCollection.fromFeature(
|
||||
Feature.fromGeometry(
|
||||
Point.fromLngLat(1.0, 1.0)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun layerUpdateAfterStyleChange() {
|
||||
validateTestSetup()
|
||||
invoke(mapboxMap) { uiController: UiController, mapboxMap: MapboxMap ->
|
||||
val style = mapboxMap.style
|
||||
|
||||
val source = GeoJsonSource(
|
||||
"source-id",
|
||||
FeatureCollection.fromFeature(
|
||||
Feature.fromGeometry(
|
||||
Point.fromLngLat(0.0, 0.0)
|
||||
)
|
||||
)
|
||||
)
|
||||
style.addSource(source)
|
||||
|
||||
val layer = SymbolLayer("test", "source-id")
|
||||
style.addLayer(layer)
|
||||
|
||||
style.loadStyle(Style.DARK)
|
||||
|
||||
layer.setProperties(iconImage("bus-15"))
|
||||
|
||||
uiController.loopMainThreadForAtLeast(300)
|
||||
|
||||
layer.setProperties(iconImage("bus-15"))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user