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

Update zoom function example with selected state #9987

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Changes from all commits
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
@@ -1,35 +1,46 @@
package com.mapbox.mapboxsdk.testapp.activity.style;

import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;
import com.mapbox.services.commons.models.Position;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import timber.log.Timber;

import static com.mapbox.mapboxsdk.style.functions.Function.property;
import static com.mapbox.mapboxsdk.style.functions.Function.zoom;
import static com.mapbox.mapboxsdk.style.functions.stops.Stop.stop;
import static com.mapbox.mapboxsdk.style.functions.stops.Stops.categorical;
import static com.mapbox.mapboxsdk.style.functions.stops.Stops.interval;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize;

/**
* Test activity showcasing changing the icon with a zoom function and adding selection state to a SymbolLayer.
*/
public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {

private static final String SOURCE_URL = "https://gist.githubusercontent.com/anonymous/"
+ "70ddad0e58520307cd1699e704f4b626/raw/a5deaccdc4517c12d63b1f8abcb7becf5e36506a/map.geojson";
private static final String LAYER_ID = "symbolLayer";
private static final String SOURCE_ID = "poiSource";
private static final String BUS_MAKI_ICON_ID = "bus-11";
private static final String CAFE_MAKI_ICON_ID = "cafe-11";
private static final String KEY_PROPERTY_SELECTED = "selected";
private static final float ZOOM_STOP_MIN_VALUE = 7.0f;
private static final float ZOOM_STOP_MAX_VALUE = 12.0f;

Expand All @@ -48,19 +59,30 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onMapReady(@NonNull final MapboxMap map) {
mapboxMap = map;
addSource();
updateSource(false);
addLayer();
addMapClickListener();
}
});
}

private void addSource() {
try {
source = new GeoJsonSource(SOURCE_ID, new URL(SOURCE_URL));
} catch (MalformedURLException exception) {
Timber.e(exception, "That's not an url... ");
private void updateSource(boolean selected) {
FeatureCollection featureCollection = createFeatureCollection(selected);
if (source != null) {
source.setGeoJson(featureCollection);
} else {
source = new GeoJsonSource(SOURCE_ID, featureCollection);
mapboxMap.addSource(source);
}
mapboxMap.addSource(source);
}

private FeatureCollection createFeatureCollection(boolean selected) {
Point point = Point.fromCoordinates(Position.fromCoordinates(-74.016181, 40.701745));
Feature feature = Feature.fromGeometry(point);
JsonObject properties = new JsonObject();
properties.addProperty(KEY_PROPERTY_SELECTED, selected);
feature.setProperties(properties);
return FeatureCollection.fromFeatures(new Feature[] {feature});
}

private void addLayer() {
Expand All @@ -74,11 +96,37 @@ private void addLayer() {
)
)
),
iconSize(
property(
KEY_PROPERTY_SELECTED,
categorical(
stop(true, iconSize(3.0f)),
stop(false, iconSize(1.0f))
)
)
),
iconAllowOverlap(true)
);
mapboxMap.addLayer(layer);
}

private void addMapClickListener() {
mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> featureList = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID);
if (!featureList.isEmpty()) {
Feature feature = featureList.get(0);
boolean isSelected = feature.getBooleanProperty(KEY_PROPERTY_SELECTED);
updateSource(!isSelected);
} else {
Timber.e("No features found");
}
}
});
}

@Override
protected void onStart() {
super.onStart();
Expand Down