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

Commit 611abd3

Browse files
authored
Adds a worldview switch examples (#1278)
* adding worldview toggle example * update starting point
1 parent 0636b3c commit 611abd3

11 files changed

Lines changed: 179 additions & 2 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
import com.mapbox.mapboxandroiddemo.examples.styles.TransparentBackgroundActivity;
156156
import com.mapbox.mapboxandroiddemo.examples.styles.VariableLabelPlacementActivity;
157157
import com.mapbox.mapboxandroiddemo.examples.styles.VectorSourceActivity;
158+
import com.mapbox.mapboxandroiddemo.examples.styles.KotlinWorldviewSwitchActivity;
158159
import com.mapbox.mapboxandroiddemo.examples.styles.ZoomDependentFillColorActivity;
159160
import com.mapbox.mapboxandroiddemo.model.ExampleItemModel;
160161
import com.mapbox.mapboxandroiddemo.utils.ItemClickSupport;
@@ -703,6 +704,14 @@ private void initializeModels() {
703704
null,
704705
R.string.activity_styles_variable_label_placement_url, false, BuildConfig.MIN_SDK_VERSION));
705706

707+
exampleItemModels.add(new ExampleItemModel(
708+
R.id.nav_styles,
709+
R.string.activity_styles_worldview_switch_title,
710+
R.string.activity_styles_worldview_switch_description,
711+
null,
712+
new Intent(MainActivity.this, KotlinWorldviewSwitchActivity.class),
713+
R.string.activity_styles_worldview_switch_url, true, BuildConfig.MIN_SDK_VERSION));
714+
706715
exampleItemModels.add(new ExampleItemModel(
707716
R.id.nav_extrusions,
708717
R.string.activity_extrusions_population_density_extrusions_title,

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,13 @@
450450
android:name="android.support.PARENT_ACTIVITY"
451451
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
452452
</activity>
453+
<activity
454+
android:name=".examples.styles.KotlinWorldviewSwitchActivity"
455+
android:label="@string/activity_styles_language_switch_title">
456+
<meta-data
457+
android:name="android.support.PARENT_ACTIVITY"
458+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
459+
</activity>
453460
<activity
454461
android:name=".examples.styles.ColorSwitcherActivity"
455462
android:label="@string/activity_styles_color_switcher_title">

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/basics/SimpleMapViewActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public void onStyleLoaded(@NonNull Style style) {
4242

4343
// Map is set up and the style has loaded. Now you can add data or make other map adjustments.
4444

45-
4645
}
4746
});
4847
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/query/BuildingOutlineActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66
import androidx.annotation.NonNull;
77
import androidx.appcompat.app.AppCompatActivity;
8+
89
import android.view.Gravity;
910
import android.view.View;
1011
import android.widget.FrameLayout;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.mapbox.mapboxandroiddemo.examples.styles
2+
3+
import android.os.Bundle
4+
import android.widget.Toast
5+
import androidx.appcompat.app.AppCompatActivity
6+
import com.mapbox.mapboxandroiddemo.R
7+
import com.mapbox.mapboxsdk.Mapbox
8+
import com.mapbox.mapboxsdk.maps.MapView
9+
import com.mapbox.mapboxsdk.maps.Style
10+
import com.mapbox.mapboxsdk.style.expressions.Expression.*
11+
import com.mapbox.mapboxsdk.style.layers.LineLayer
12+
import kotlinx.android.synthetic.main.activity_style_worldview_switch.*
13+
14+
/**
15+
* Uses the worldview value to adjust administrative boundaries.
16+
* You can see the worldview options within the worldviews variable in this example.
17+
*
18+
* More about Mapbox's worldview can be found at
19+
* https://docs.mapbox.com/vector-tiles/reference/mapbox-streets-v8/#-worldview-text
20+
*/
21+
class KotlinWorldviewSwitchActivity : AppCompatActivity() {
22+
23+
private lateinit var mapView: MapView
24+
private var possibleWorldViews = mutableListOf("US", "CN", "IN")
25+
private var index = 0
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
30+
// Mapbox access token is configured here. This needs to be called either in your application
31+
// object or in the same activity which contains the mapview.
32+
Mapbox.getInstance(this, getString(R.string.access_token))
33+
34+
// This contains the MapView in XML and needs to be called after the access token is configured.
35+
setContentView(R.layout.activity_style_worldview_switch)
36+
mapView = findViewById(R.id.mapView)
37+
mapView.onCreate(savedInstanceState)
38+
mapView.getMapAsync { mapboxMap ->
39+
mapboxMap.setStyle(Style.MAPBOX_STREETS) { style ->
40+
41+
switch_worldview_fab.setOnClickListener {
42+
43+
style.layers.forEach {
44+
45+
// Apply changes only to certain LineLayers in the style
46+
if (it.id == "admin-0-boundary" ||
47+
it.id == "admin-1-boundary" ||
48+
it.id == "admin-0-boundary-disputed" ||
49+
it.id == "admin-1-boundary-bg" ||
50+
it.id == "admin-0-boundary-bg") {
51+
if (index == possibleWorldViews.size) {
52+
index = 0
53+
}
54+
55+
val layerToBeAdjusted = style.getLayerAs<LineLayer>(it.id)
56+
57+
// Use the Maps SDK's expressions to show the corresponding world view
58+
layerToBeAdjusted?.setFilter(
59+
match(
60+
get("worldview"),
61+
all(literal(possibleWorldViews[index])),
62+
literal(true), literal(false))
63+
)
64+
}
65+
}
66+
67+
when (possibleWorldViews[index]) {
68+
"US" -> {
69+
Toast.makeText(this, String.format(
70+
getString(R.string.now_viewing_worldview, "US")),
71+
Toast.LENGTH_SHORT).show()
72+
}
73+
74+
"CN" -> {
75+
Toast.makeText(this, String.format(
76+
getString(R.string.now_viewing_worldview, "China")),
77+
Toast.LENGTH_SHORT).show()
78+
}
79+
80+
"IN" -> {
81+
Toast.makeText(this, String.format(
82+
getString(R.string.now_viewing_worldview, "India")),
83+
Toast.LENGTH_SHORT).show()
84+
}
85+
}
86+
87+
index++
88+
}
89+
}
90+
}
91+
}
92+
93+
public override fun onResume() {
94+
super.onResume()
95+
mapView.onResume()
96+
}
97+
98+
override fun onStart() {
99+
super.onStart()
100+
mapView.onStart()
101+
}
102+
103+
override fun onStop() {
104+
super.onStop()
105+
mapView.onStop()
106+
}
107+
108+
public override fun onPause() {
109+
super.onPause()
110+
mapView.onPause()
111+
}
112+
113+
override fun onLowMemory() {
114+
super.onLowMemory()
115+
mapView.onLowMemory()
116+
}
117+
118+
override fun onDestroy() {
119+
super.onDestroy()
120+
mapView.onDestroy()
121+
}
122+
123+
override fun onSaveInstanceState(outState: Bundle) {
124+
super.onSaveInstanceState(outState)
125+
mapView.onSaveInstanceState(outState)
126+
}
127+
}

MapboxAndroidDemo/src/main/res/layout/activity_marathon_extrusion.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
mapbox:mapbox_cameraZoom="12.692151"
2929
/>
3030

31-
</androidx.constraintlayout.widget.ConstraintLayout>
31+
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<com.mapbox.mapboxsdk.maps.MapView
9+
android:id="@id/mapView"
10+
mapbox:mapbox_cameraTargetLat="28.56"
11+
mapbox:mapbox_cameraTargetLng="87.2"
12+
mapbox:mapbox_cameraZoom="4.64"
13+
android:layout_width="match_parent"
14+
android:layout_height="match_parent" />
15+
16+
<com.google.android.material.floatingactionbutton.FloatingActionButton
17+
android:id="@+id/switch_worldview_fab"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:layout_gravity="bottom|right"
21+
android:layout_marginEnd="16dp"
22+
android:layout_marginRight="16dp"
23+
android:layout_marginBottom="16dp"
24+
app:layout_constraintBottom_toBottomOf="parent"
25+
app:layout_constraintEnd_toEndOf="parent"
26+
app:srcCompat="@drawable/ic_swap_horiz_white_24dp"
27+
mapbox:fabSize="normal" />
28+
</FrameLayout>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,7 @@
447447
<string name="thick_bordered_circle_border_difference">Border difference: %1$d kilometers</string>
448448
<string name="tap_on_map_to_move_bordered_circle">Tap on the map to move the bordered circle</string>
449449

450+
<!-- Worldview-->
451+
<string name="now_viewing_worldview">Now viewing the "%1$s" world view</string>
452+
450453
</resources>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<string name="activity_styles_missing_icon_description">Provide an icon when a Style failed to load one.</string>
3939
<string name="activity_styles_variable_label_placement_description">To increase the chance of high-priority labels staying visible, provide the map renderer a list of preferred text anchor positions.</string>
4040
<string name="activity_styles_runtime_styling_description">Change various properties of a map based on user interaction and other runtime situations.</string>
41+
<string name="activity_styles_worldview_switch_description">Uses the worldview value to adjust administrative boundaries based on the map\'s audience.</string>
4142
<string name="activity_extrusions_catalina_marathon_extrusions_description">Use data-driven styling and GeoJSON data to set extrusions\' heights.</string>
4243
<string name="activity_extrusions_population_density_extrusions_description">Use extrusions to display 3D building height based on imported vector data.</string>
4344
<string name="activity_extrusions_adjust_extrusions_description">Change the location and color of the light shined on extrusions.</string>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<string name="activity_styles_missing_icon_title">Style with missing icon</string>
3737
<string name="activity_styles_variable_label_placement_title">Variable label placement</string>
3838
<string name="activity_styles_runtime_styling_title">Button interaction styling</string>
39+
<string name="activity_styles_worldview_switch_title">Worldviews</string>
3940
<string name="activity_extrusions_catalina_marathon_extrusions_title">Use GeoJSON data to set extrusion height</string>
4041
<string name="activity_extrusions_population_density_extrusions_title">Display 3D building height based on vector data</string>
4142
<string name="activity_extrusions_adjust_extrusions_title">Adjust light location and color</string>

0 commit comments

Comments
 (0)