Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
refactored to use the step expression
Browse files Browse the repository at this point in the history
  • Loading branch information
langsmith committed Mar 6, 2019
1 parent d9a4088 commit 411fd44
Showing 1 changed file with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.utils.BitmapUtils;

import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
import static com.mapbox.mapboxsdk.style.expressions.Expression.step;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
Expand Down Expand Up @@ -73,7 +77,7 @@ private void initLayerIcons(@NonNull Style loadedMapStyle) {
}

/**
* Add the GeoJsonSource and SymbolLayers to the map.
* Add the GeoJsonSource and the SymbolLayer to the map.
*/
private void addDataToMap(@NonNull Style loadedMapStyle) {
// Add a new source from the GeoJSON data
Expand Down Expand Up @@ -107,29 +111,19 @@ private void addDataToMap(@NonNull Style loadedMapStyle) {
})
)
);
loadedMapStyle.addLayer(createLayer(BLUE_PERSON_ICON_ID, false));
loadedMapStyle.addLayer(createLayer(BLUE_PIN_ICON_ID, true));
}

/**
* This method creates a SymbolLayer which is ready to be added to the map.
*
* @param iconId The unique id of the image which the SymbolLayer should use for its symbol icon
* @param setMin Whether or not a minimum or maximum zoom level should be set on the SymbolLayer.
* @return a completed SymbolLayer which is ready to add to the map.
*/
private SymbolLayer createLayer(String iconId, boolean setMin) {
SymbolLayer singleLayer = new SymbolLayer(iconId + "symbol-layer-id", "source-id");
/** Create a SymbolLayer and use the {@link com.mapbox.mapboxsdk.style.expressions.Expression.step()}
* to adjust the SymbolLayer icon based on the zoom level.
*/
SymbolLayer singleLayer = new SymbolLayer( "symbol-layer-id", "source-id");
singleLayer.setProperties(
iconImage(iconId),
iconImage(
step(zoom(), literal(BLUE_PERSON_ICON_ID),
stop(ZOOM_LEVEL_FOR_SWITCH, BLUE_PIN_ICON_ID))),
iconIgnorePlacement(true),
iconAllowOverlap(true));
if (setMin) {
singleLayer.setMinZoom(ZOOM_LEVEL_FOR_SWITCH);
} else {
singleLayer.setMaxZoom(ZOOM_LEVEL_FOR_SWITCH);
}
return singleLayer;

loadedMapStyle.addLayer(singleLayer);
}

@Override
Expand Down

0 comments on commit 411fd44

Please sign in to comment.