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

Commit c7f4d90

Browse files
author
Langston Smith
authored
Symbol switch based on zoom level example (#929)
* initial additions * refactoring based on tobrun review
1 parent 5933ab2 commit c7f4d90

File tree

9 files changed

+208
-0
lines changed

9 files changed

+208
-0
lines changed

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.mapbox.mapboxandroiddemo.examples.dds.SatelliteLandSelectActivity;
5858
import com.mapbox.mapboxandroiddemo.examples.dds.StyleCirclesCategoricallyActivity;
5959
import com.mapbox.mapboxandroiddemo.examples.dds.StyleLineIdentityPropertyActivity;
60+
import com.mapbox.mapboxandroiddemo.examples.dds.SymbolSwitchOnZoomActivity;
6061
import com.mapbox.mapboxandroiddemo.examples.extrusions.AdjustExtrusionLightActivity;
6162
import com.mapbox.mapboxandroiddemo.examples.extrusions.Indoor3DMapActivity;
6263
import com.mapbox.mapboxandroiddemo.examples.extrusions.MarathonExtrusionActivity;
@@ -1214,6 +1215,14 @@ private void initializeModels() {
12141215
null,
12151216
R.string.activity_dds_satellite_land_select_url, true, BuildConfig.MIN_SDK_VERSION));
12161217

1218+
exampleItemModels.add(new ExampleItemModel(
1219+
R.id.nav_dds,
1220+
R.string.activity_dds_symbol_zoom_switch_title,
1221+
R.string.activity_dds_symbol_zoom_switch_description,
1222+
new Intent(MainActivity.this, SymbolSwitchOnZoomActivity.class),
1223+
null,
1224+
R.string.activity_dds_symbol_zoom_switch_url, true, BuildConfig.MIN_SDK_VERSION));
1225+
12171226
exampleItemModels.add(new ExampleItemModel(
12181227
R.id.nav_basics,
12191228
R.string.activity_basic_simple_mapview_title,

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,13 @@
628628
android:name="android.support.PARENT_ACTIVITY"
629629
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
630630
</activity>
631+
<activity
632+
android:name=".examples.dds.SymbolSwitchOnZoomActivity"
633+
android:label="@string/activity_dds_symbol_zoom_switch_title">
634+
<meta-data
635+
android:name="android.support.PARENT_ACTIVITY"
636+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
637+
</activity>
631638
<activity
632639
android:name=".examples.camera.RestrictCameraActivity"
633640
android:label="@string/activity_camera_restrict_title">
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.mapbox.mapboxandroiddemo.examples.dds;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.widget.Toast;
7+
8+
import com.mapbox.geojson.Feature;
9+
import com.mapbox.geojson.FeatureCollection;
10+
import com.mapbox.geojson.Point;
11+
import com.mapbox.mapboxandroiddemo.R;
12+
import com.mapbox.mapboxsdk.Mapbox;
13+
import com.mapbox.mapboxsdk.maps.MapView;
14+
import com.mapbox.mapboxsdk.maps.MapboxMap;
15+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
16+
import com.mapbox.mapboxsdk.maps.Style;
17+
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
18+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
19+
import com.mapbox.mapboxsdk.utils.BitmapUtils;
20+
21+
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
22+
import static com.mapbox.mapboxsdk.style.expressions.Expression.step;
23+
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
24+
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
25+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
26+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement;
27+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
28+
29+
/**
30+
* Use the SymbolLayer's setMinZoom and setMaxZoom methods to create the effect of SymbolLayer icons switching
31+
* based on the map camera's zoom level.
32+
*/
33+
public class SymbolSwitchOnZoomActivity extends AppCompatActivity implements OnMapReadyCallback {
34+
35+
private static final float ZOOM_LEVEL_FOR_SWITCH = 12;
36+
private static final String BLUE_PERSON_ICON_ID = "blue-car-icon-marker-icon-id";
37+
private static final String BLUE_PIN_ICON_ID = "blue-marker-icon-marker-icon-id";
38+
private MapView mapView;
39+
40+
@Override
41+
protected void onCreate(Bundle savedInstanceState) {
42+
super.onCreate(savedInstanceState);
43+
44+
// Mapbox access token is configured here. This needs to be called either in your application
45+
// object or in the same activity which contains the mapview.
46+
Mapbox.getInstance(this, getString(R.string.access_token));
47+
48+
// This contains the MapView in XML and needs to be called after the access token is configured.
49+
setContentView(R.layout.activity_zoom_based_icon_switch);
50+
51+
mapView = findViewById(R.id.mapView);
52+
mapView.onCreate(savedInstanceState);
53+
mapView.getMapAsync(this);
54+
}
55+
56+
@Override
57+
public void onMapReady(final MapboxMap mapboxMap) {
58+
59+
mapboxMap.setStyle(new Style.Builder().fromUrl(Style.OUTDOORS)
60+
61+
// Add images to the map so that the SymbolLayers can reference the images.
62+
.withImage(BLUE_PERSON_ICON_ID, BitmapUtils.getBitmapFromDrawable(
63+
getResources().getDrawable(R.drawable.ic_person)))
64+
.withImage(BLUE_PIN_ICON_ID, BitmapUtils.getBitmapFromDrawable(
65+
getResources().getDrawable(R.drawable.blue_marker)))
66+
67+
// Add random data to the GeoJsonSource and then add the GeoJsonSource to the map
68+
.withSource(new GeoJsonSource("source-id",
69+
FeatureCollection.fromFeatures(new Feature[] {
70+
Feature.fromGeometry(Point.fromLngLat(
71+
9.205394983291626,
72+
45.47661043757903)),
73+
Feature.fromGeometry(Point.fromLngLat(
74+
9.223880767822266,
75+
45.47623240235297)),
76+
Feature.fromGeometry(Point.fromLngLat(
77+
9.15530204772949,
78+
45.4706650227671)),
79+
Feature.fromGeometry(Point.fromLngLat(
80+
9.153714179992676,
81+
45.48625229963004)),
82+
Feature.fromGeometry(Point.fromLngLat(
83+
9.158306121826172,
84+
45.482731998239636)),
85+
Feature.fromGeometry(Point.fromLngLat(
86+
9.188523888587952,
87+
45.4923746929562)),
88+
Feature.fromGeometry(Point.fromLngLat(
89+
9.20929491519928,
90+
45.45314676076135)),
91+
Feature.fromGeometry(Point.fromLngLat(
92+
9.177778959274292,
93+
45.45569808340158))
94+
})
95+
)), new Style.OnStyleLoaded() {
96+
@Override
97+
public void onStyleLoaded(@NonNull Style style) {
98+
99+
// Create a SymbolLayer and use the {@link com.mapbox.mapboxsdk.style.expressions.Expression.step()}
100+
// to adjust the SymbolLayer icon based on the zoom level. The blue person icon is set as the default
101+
// icon and then a step is used to switch to the blue person icon at a certain map camera zoom level.
102+
SymbolLayer singleLayer = new SymbolLayer("symbol-layer-id", "source-id");
103+
singleLayer.setProperties(
104+
iconImage(step(zoom(), literal(BLUE_PERSON_ICON_ID),
105+
stop(ZOOM_LEVEL_FOR_SWITCH, BLUE_PIN_ICON_ID))),
106+
iconIgnorePlacement(true),
107+
iconAllowOverlap(true));
108+
style.addLayer(singleLayer);
109+
110+
Toast.makeText(SymbolSwitchOnZoomActivity.this,
111+
R.string.zoom_map_in_and_out_icon_switch_instruction, Toast.LENGTH_SHORT).show();
112+
}
113+
}
114+
);
115+
}
116+
117+
@Override
118+
public void onStart() {
119+
super.onStart();
120+
mapView.onStart();
121+
}
122+
123+
@Override
124+
public void onResume() {
125+
super.onResume();
126+
mapView.onResume();
127+
}
128+
129+
@Override
130+
public void onPause() {
131+
super.onPause();
132+
mapView.onPause();
133+
}
134+
135+
@Override
136+
public void onStop() {
137+
super.onStop();
138+
mapView.onStop();
139+
}
140+
141+
@Override
142+
public void onLowMemory() {
143+
super.onLowMemory();
144+
mapView.onLowMemory();
145+
}
146+
147+
@Override
148+
protected void onDestroy() {
149+
super.onDestroy();
150+
mapView.onDestroy();
151+
}
152+
153+
@Override
154+
protected void onSaveInstanceState(Bundle outState) {
155+
super.onSaveInstanceState(outState);
156+
mapView.onSaveInstanceState(outState);
157+
}
158+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#2096F3"
8+
android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/>
9+
</vector>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".examples.dds.SymbolSwitchOnZoomActivity">
9+
10+
<com.mapbox.mapboxsdk.maps.MapView
11+
android:id="@+id/mapView"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
mapbox:mapbox_cameraTargetLat="45.459145"
15+
mapbox:mapbox_cameraTargetLng="9.186176"
16+
mapbox:mapbox_cameraZoom="10.662822"/>
17+
18+
</FrameLayout>

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,8 @@
308308
<string name="light_azimuth_angle">Light azimuthal angle</string>
309309
<string name="light_polar_angle">Light polar angle</string>
310310
<string name="building_opacity">Building opacity</string>
311+
312+
<!-- Zoom based icon change -->
313+
<string name="zoom_map_in_and_out_icon_switch_instruction">Zoom in and out to see the icons change</string>
314+
311315
</resources>

MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<string name="activity_dds_info_window_symbol_layer_description">Use SymbolLayer and icons to show data in a BubbleLayout "info window".</string>
5252
<string name="activity_dds_expression_integration_description">Use multiple expressions to visualize unit change in data.</string>
5353
<string name="activity_dds_satellite_land_select_description">View satellite photos and click to outline an area of land.</string>
54+
<string name="activity_dds_symbol_zoom_switch_description">Change SymbolLayer icons based on the camera\'s zoom level.</string>
5455
<string name="activity_annotation_marker_description">Create a default marker with an InfoWindow.</string>
5556
<string name="activity_dds_geojson_line_description">Draw a polyline by parsing a GeoJSON file with the Mapbox Maps SDK.</string>
5657
<string name="activity_dds_polygon_description">Draw a vector polygon on a map with the Mapbox Maps SDK.</string>

MapboxAndroidDemo/src/main/res/values/titles_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<string name="activity_dds_expression_integration_title">Temperature change</string>
5252
<string name="activity_dds_satellite_land_select_title">Satellite land select</string>
5353
<string name="activity_dds_line_gradient_title">Line gradient</string>
54+
<string name="activity_dds_symbol_zoom_switch_title">Zoom-based icon switch</string>
5455
<string name="activity_annotation_marker_title">Draw a marker</string>
5556
<string name="activity_dds_geojson_line_title">Draw a GeoJSON line</string>
5657
<string name="activity_dds_polygon_title">Draw a polygon</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<string name="activity_dds_image_clustering_url" translatable="false">https://i.imgur.com/KjgNcS0.png</string>
5151
<string name="activity_dds_expression_integration_url" translatable="false">https://imgur.com/YiBeH0I.png</string>
5252
<string name="activity_dds_satellite_land_select_url" translatable="false">https://i.imgur.com/tfbO1m4.png</string>
53+
<string name="activity_dds_symbol_zoom_switch_url" translatable="false">https://i.imgur.com/sbWU4Ui.png</string>
5354
<string name="activity_annotation_marker_url" translatable="false">http://i.imgur.com/X59UoaY.png</string>
5455
<string name="activity_dds_geojson_line_url" translatable="false">https://i.imgur.com/Bs0X98z.png</string>
5556
<string name="activity_dds_polygon_url" translatable="false">http://i.imgur.com/v9X28id.png</string>

0 commit comments

Comments
 (0)