Skip to content

Commit 70f3c91

Browse files
authored
Add Vector Tile Source example Android Docs & Example app (#3278)
1 parent 46e821a commit 70f3c91

File tree

7 files changed

+135
-8
lines changed

7 files changed

+135
-8
lines changed

platform/android/MapLibreAndroidTestApp/src/main/AndroidManifest.xml

+17-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<application
88
android:name=".MapLibreApplication"
99
android:allowBackup="true"
10+
android:debuggable="true"
1011
android:exported="true"
1112
android:fullBackupContent="true"
1213
android:icon="@drawable/icon"
1314
android:label="@string/app_name"
1415
android:roundIcon="@drawable/ic_launcher_round"
1516
android:supportsRtl="true"
1617
android:theme="@style/AppTheme"
17-
android:debuggable="true"
1818
tools:ignore="HardcodedDebugMode">
1919
<activity
2020
android:name=".activity.FeatureOverviewActivity"
@@ -719,6 +719,18 @@
719719
<meta-data
720720
android:name="android.support.PARENT_ACTIVITY"
721721
android:value=".activity.FeatureOverviewActivity" />
722+
</activity>
723+
<activity
724+
android:name=".activity.sources.VectorTileActivity"
725+
android:description="@string/description_vector_tile"
726+
android:exported="true"
727+
android:label="@string/activity_vector_tile">
728+
<meta-data
729+
android:name="@string/category"
730+
android:value="@string/category_data" />
731+
<meta-data
732+
android:name="android.support.PARENT_ACTIVITY"
733+
android:value=".activity.FeatureOverviewActivity" />
722734
</activity> <!-- Features -->
723735
<activity
724736
android:name=".activity.feature.QueryRenderedFeaturesPropertiesActivity"
@@ -878,10 +890,10 @@
878890
android:value=".activity.FeatureOverviewActivity" />
879891
</activity> <!-- Events -->
880892
<activity
881-
android:name="org.maplibre.android.testapp.activity.events.ObserverActivity"
893+
android:name=".activity.events.ObserverActivity"
882894
android:description="@string/description_observer_activity"
883895
android:exported="true"
884-
android:label="@string/activity_observer_activity" >
896+
android:label="@string/activity_observer_activity">
885897
<meta-data
886898
android:name="@string/category"
887899
android:value="@string/category_events" />
@@ -1259,21 +1271,18 @@
12591271
android:name="android.support.PARENT_ACTIVITY"
12601272
android:value=".activity.FeatureOverviewActivity" />
12611273
</activity>
1262-
12631274
<activity
12641275
android:name=".activity.options.MapOptionsXmlActivity"
1265-
android:exported="true"
12661276
android:description="@string/description_map_options_xml"
1267-
android:label="@string/activity_map_options_xml"
1268-
>
1277+
android:exported="true"
1278+
android:label="@string/activity_map_options_xml">
12691279
<meta-data
12701280
android:name="@string/category"
12711281
android:value="@string/category_map_options" />
12721282
<meta-data
12731283
android:name="android.support.PARENT_ACTIVITY"
12741284
android:value=".activity.FeatureOverviewActivity" />
12751285
</activity>
1276-
12771286
</application>
12781287

12791288
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.maplibre.android.testapp.activity.sources
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import org.maplibre.android.camera.CameraUpdateFactory
6+
import org.maplibre.android.geometry.LatLngBounds
7+
import org.maplibre.android.maps.MapView
8+
import org.maplibre.android.style.layers.LineLayer
9+
import org.maplibre.android.style.layers.PropertyFactory.lineColor
10+
import org.maplibre.android.style.layers.PropertyFactory.lineWidth
11+
import org.maplibre.android.style.sources.TileSet
12+
import org.maplibre.android.style.sources.VectorSource
13+
import org.maplibre.android.testapp.R
14+
import org.maplibre.android.testapp.styles.TestStyles
15+
16+
17+
class VectorTileActivity : AppCompatActivity() {
18+
private lateinit var mapView: MapView
19+
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
setContentView(R.layout.activity_vector_tile)
23+
mapView = findViewById<MapView>(R.id.mapView)
24+
25+
mapView.getMapAsync {
26+
it.animateCamera(
27+
CameraUpdateFactory.newLatLngBounds(
28+
// z: 12, x: 2177, y: 1436 is one of the available tiles:
29+
// https://github.com/maplibre/demotiles/tree/gh-pages/tiles-omt/12/2177
30+
LatLngBounds.from(12, 2177, 1436),
31+
0
32+
)
33+
)
34+
it.setStyle(TestStyles.PROTOMAPS_GRAYSCALE) { style ->
35+
// --8<-- [start:addTileSet]
36+
val tileset = TileSet(
37+
"openmaptiles",
38+
"https://demotiles.maplibre.org/tiles-omt/{z}/{x}/{y}.pbf"
39+
)
40+
val openmaptiles = VectorSource("openmaptiles", tileset)
41+
style.addSource(openmaptiles)
42+
val roadLayer = LineLayer("road", "openmaptiles").apply {
43+
setSourceLayer("transportation")
44+
setProperties(
45+
lineColor("red"),
46+
lineWidth(2.0f)
47+
)
48+
}
49+
// --8<-- [end:addTileSet]
50+
51+
style.addLayer(roadLayer)
52+
}
53+
}
54+
}
55+
56+
override fun onStart() {
57+
super.onStart()
58+
mapView.onStart()
59+
}
60+
61+
override fun onResume() {
62+
super.onResume()
63+
mapView.onResume()
64+
}
65+
66+
override fun onPause() {
67+
super.onPause()
68+
mapView.onPause()
69+
}
70+
71+
override fun onStop() {
72+
super.onStop()
73+
mapView.onStop()
74+
}
75+
76+
override fun onLowMemory() {
77+
super.onLowMemory()
78+
mapView.onLowMemory()
79+
}
80+
81+
override fun onDestroy() {
82+
super.onDestroy()
83+
mapView.onDestroy()
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".activity.sources.VectorTileActivity">
9+
10+
<org.maplibre.android.maps.MapView
11+
android:id="@id/mapView"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent" />
14+
</RelativeLayout>

platform/android/MapLibreAndroidTestApp/src/main/res/values/descriptions.xml

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<string name="description_overlay">Blend an overlay on a map</string>
8181
<string name="description_grid_source">Example Custom Geometry Source</string>
8282
<string name="description_no_style">Load a map without providing a style URI or JSON</string>
83+
<string name="description_vector_tile">Load MVT data</string>
8384
<string name="description_local_glyph">Suzhou using Droid Sans for Chinese glyphs</string>
8485
<string name="description_hillshade">Example raster-dem source and hillshade layer</string>
8586
<string name="description_heatmaplayer">Use HeatmapLayer to visualise earthquakes</string>

platform/android/MapLibreAndroidTestApp/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
<string name="activity_benchmark_world_tour">World Tour Benchmark</string>
99
<string name="description_world_tour_benchmark">Writes out avg. fps and 1% fps after world tour with .flyTo()</string>
1010
<string name="search">Search</string>
11+
<string name="category_data">Data Sources</string>
1112
</resources>

platform/android/MapLibreAndroidTestApp/src/main/res/values/titles.xml

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<string name="activity_overlay">Map overlay</string>
7777
<string name="activity_grid_source">Grid Source</string>
7878
<string name="activity_no_style">No Style URI/JSON</string>
79+
<string name="activity_vector_tile">Vector Tile source</string>
7980
<string name="activity_local_glyph">Local CJK glyph generation</string>
8081
<string name="activity_hillshade">Hillshade</string>
8182
<string name="activity_heatmaplayer">Heatmap layer</string>

platform/android/docs/data/MVT.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Vector Tiles
2+
3+
{{ activity_source_note("VectorTileActivity.kt") }}
4+
5+
You can specify where to load MVTs (which sometimes have `.pbf` extension) by creating a `TileSet` object with template parameters (for example `{z}` which will be replaced with the zoom level).
6+
7+
MapLibre has [a repo](https://github.com/maplibre/demotiles/tree/gh-pages/tiles-omt) with some example vector tiles with the OpenMapTiles schema around Innsbruck, Austria. In the example we load these MVTs and create a line layer for the road network.
8+
9+
```kotlin
10+
--8<-- "MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/sources/VectorTileActivity.kt:addTileSet"
11+
```
12+
13+
<figure markdown="span">
14+
![Screenshot of road overlay from vector tile source]({{ s3_url("vectortilesource.png") }}){ width="400" }
15+
{{ openmaptiles_caption() }}
16+
</figure>

0 commit comments

Comments
 (0)