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

Commit 10b47a9

Browse files
author
Langston Smith
authored
Adding Kotlin example of adding a map fragment to container (#1188)
1 parent 57afa93 commit 10b47a9

File tree

7 files changed

+118
-1
lines changed

7 files changed

+118
-1
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
@@ -24,6 +24,7 @@
2424
import com.mapbox.mapboxandroiddemo.commons.AnalyticsTracker;
2525
import com.mapbox.mapboxandroiddemo.commons.FirstTimeRunChecker;
2626
import com.mapbox.mapboxandroiddemo.examples.basics.KotlinSimpleMapViewActivity;
27+
import com.mapbox.mapboxandroiddemo.examples.basics.KotlinSupportMapFragmentActivity;
2728
import com.mapbox.mapboxandroiddemo.examples.basics.MapboxMapOptionActivity;
2829
import com.mapbox.mapboxandroiddemo.examples.basics.SimpleMapViewActivity;
2930
import com.mapbox.mapboxandroiddemo.examples.basics.SupportMapFragmentActivity;
@@ -1443,6 +1444,14 @@ private void initializeModels() {
14431444
null,
14441445
R.string.activity_basic_mapbox_options_url, false, BuildConfig.MIN_SDK_VERSION));
14451446

1447+
exampleItemModels.add(new ExampleItemModel(
1448+
R.id.nav_basics,
1449+
R.string.activity_basic_kotlin_support_map_frag_title,
1450+
R.string.activity_basic_kotlin_support_map_frag_description,
1451+
null,
1452+
new Intent(MainActivity.this, KotlinSupportMapFragmentActivity.class),
1453+
R.string.activity_basic_kotlin_support_map_frag_url, true, BuildConfig.MIN_SDK_VERSION));
1454+
14461455
exampleItemModels.add(new ExampleItemModel(
14471456
R.id.nav_dds,
14481457
R.string.activity_dds_circle_radius_title,

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,13 @@
648648
android:name="android.support.PARENT_ACTIVITY"
649649
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
650650
</activity>
651+
<activity
652+
android:name=".examples.basics.KotlinSupportMapFragmentActivity"
653+
android:label="@string/activity_basic_kotlin_support_map_frag_title">
654+
<meta-data
655+
android:name="android.support.PARENT_ACTIVITY"
656+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
657+
</activity>
651658
<activity
652659
android:name=".examples.dds.BathymetryActivity"
653660
android:label="@string/activity_dds_bathymetry_title">
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.mapbox.mapboxandroiddemo.examples.basics
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import com.mapbox.mapboxandroiddemo.R
6+
import com.mapbox.mapboxsdk.Mapbox
7+
import com.mapbox.mapboxsdk.camera.CameraPosition
8+
import com.mapbox.mapboxsdk.geometry.LatLng
9+
import com.mapbox.mapboxsdk.maps.MapboxMapOptions
10+
import com.mapbox.mapboxsdk.maps.Style
11+
import com.mapbox.mapboxsdk.maps.SupportMapFragment
12+
13+
/**
14+
* Use Kotlin to add a map into a fragment container.
15+
*/
16+
class KotlinSupportMapFragmentActivity : AppCompatActivity() {
17+
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
setContentView(R.layout.activity_basic_kotlin_support_map_frag)
21+
22+
// Mapbox access token is configured here. This needs to be called either in your application
23+
// object or in the same activity which contains the mapview.
24+
Mapbox.getInstance(this, getString(R.string.access_token))
25+
26+
// Create supportMapFragment
27+
val mapFragment: SupportMapFragment
28+
if (savedInstanceState == null) {
29+
30+
// Create fragment
31+
val transaction = supportFragmentManager.beginTransaction()
32+
33+
// Build mapboxMap
34+
val options = MapboxMapOptions.createFromAttributes(this, null)
35+
options.camera(CameraPosition.Builder()
36+
.target(LatLng(-52.6885, -70.1395))
37+
.zoom(9.0)
38+
.build())
39+
40+
// Create map fragment
41+
mapFragment = SupportMapFragment.newInstance(options)
42+
43+
// Add map fragment to parent container
44+
transaction.add(R.id.container, mapFragment, "com.mapbox.map")
45+
transaction.commit()
46+
} else {
47+
mapFragment = supportFragmentManager.findFragmentByTag("com.mapbox.map") as SupportMapFragment
48+
}
49+
50+
mapFragment.getMapAsync {
51+
mapboxMap -> mapboxMap.setStyle(Style.SATELLITE) {
52+
53+
// Map in fragment container is set up and the style has loaded.
54+
// Now you can add data or make other map adjustments.
55+
}
56+
}
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="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+
android:background="@color/md_divider_white"
9+
tools:context=".examples.basics.SupportMapFragmentActivity">
10+
11+
<TextView
12+
android:id="@+id/fragment_below_textview"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:layout_marginTop="8dp"
16+
android:gravity="center_horizontal"
17+
android:text="@string/fragment_in_card_below"/>
18+
19+
<androidx.cardview.widget.CardView
20+
android:id="@+id/cardview"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"
23+
android:layout_below="@id/fragment_below_textview"
24+
android:layout_marginBottom="8dp"
25+
android:layout_marginEnd="8dp"
26+
android:layout_marginLeft="8dp"
27+
android:layout_marginRight="8dp"
28+
android:layout_marginStart="8dp"
29+
android:layout_marginTop="8dp"
30+
app:cardCornerRadius="2dp"
31+
app:cardElevation="@dimen/cardview_default_elevation">
32+
33+
<FrameLayout
34+
android:id="@+id/container"
35+
android:layout_width="match_parent"
36+
android:layout_height="match_parent"/>
37+
</androidx.cardview.widget.CardView>
38+
39+
40+
</RelativeLayout>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
each example's recyclerview card ****** -->
66

77
<string name="activity_basic_simple_mapview_description">Show a map in your app using the Mapbox Maps SDK.</string>
8-
<string name="activity_basic_support_map_frag_description">Include a map fragment within your app using Android support library.</string>
8+
<string name="activity_basic_support_map_frag_description">Place a map fragment inside of a container.</string>
99
<string name="activity_basic_mapbox_options_description">Add a mapview in a dynamically created layout.</string>
1010
<string name="activity_basic_mapbox_kotlin_description">Display a basic map with Kotlin code.</string>
11+
<string name="activity_basic_kotlin_support_map_frag_description">Place a map fragment inside of a container.</string>
1112
<string name="activity_styles_mapbox_studio_description">Use a custom Mapbox-hosted style.</string>
1213
<string name="activity_style_raster_description">Use legacy raster tiles in your app.</string>
1314
<string name="activity_styles_default_description">Use a variety of professionally designed styles 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
@@ -8,6 +8,7 @@
88
<string name="activity_basic_support_map_frag_title">Support map fragment</string>
99
<string name="activity_basic_mapbox_options_title">Dynamically build a map view</string>
1010
<string name="activity_basic_mapbox_kotlin_title">Use Kotlin to show a map</string>
11+
<string name="activity_basic_kotlin_support_map_frag_title">Support map fragment</string>
1112
<string name="activity_styles_mapbox_studio_title">Mapbox Studio style</string>
1213
<string name="activity_styles_default_title">Default styles</string>
1314
<string name="activity_styles_show_hide_layer_title">Show and hide layers</string>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<string name="activity_basic_simple_mapview_url" translatable="false">https://i.imgur.com/UF3B0o8.png</string>
99
<string name="activity_basic_support_map_frag_url" translatable="false">https://i.imgur.com/y5QIBeM.png</string>
1010
<string name="activity_basic_mapbox_options_url" translatable="false">https://i.imgur.com/CRtK8UE.png</string>
11+
<string name="activity_basic_kotlin_support_map_frag_url" translatable="false">https://i.imgur.com/y5QIBeM.png</string>
1112
<string name="activity_styles_mapbox_studio_url" translatable="false">http://i.imgur.com/MbR2zbD.png</string>
1213
<string name="activity_style_raster_url" translatable="false">http://i.imgur.com/QpFmrdR.png</string>
1314
<string name="activity_styles_default_url" translatable="false">http://i.imgur.com/ZuUhFHw.jpg</string>

0 commit comments

Comments
 (0)