Skip to content

Commit b2e846e

Browse files
committed
feat(library): add flexible polyline encoding and decoding
Adds FlexiblePolylineUtil to the library module, alongside the existing PolyUtil codec. The format supports configurable precision, a header that carries the precision with the data, and an optional third value per point. Also adds a demo activity exercising the decoder, registered under the Geometry category.
1 parent 82faf3f commit b2e846e

6 files changed

Lines changed: 834 additions & 0 deletions

File tree

‎demo/src/main/AndroidManifest.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<activity
6666
android:name=".PolyDecodeDemoActivity"
6767
android:exported="true" />
68+
<activity
69+
android:name=".FlexiblePolylineDemoActivity"
70+
android:exported="true" />
6871
<activity
6972
android:name=".PolySimplifyDemoActivity"
7073
android:exported="true" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.maps.android.utils.demo
17+
18+
import android.widget.Toast
19+
import com.google.android.gms.maps.CameraUpdateFactory
20+
import com.google.android.gms.maps.model.LatLng
21+
import com.google.android.gms.maps.model.PolylineOptions
22+
import com.google.maps.android.FlexiblePolylineUtil
23+
24+
/**
25+
* Decodes a flexible polyline with [FlexiblePolylineUtil] and draws it.
26+
*
27+
* [ENCODED_WALKWAY] holds ten points along the Sydney Harbour Bridge walkway at seven decimal
28+
* places. [FlexiblePolylineUtil.decodeToLatLngs] supplies the path for [PolylineOptions].
29+
*/
30+
class FlexiblePolylineDemoActivity : BaseDemoActivity() {
31+
override fun startDemo(isRestore: Boolean) {
32+
val map = map ?: return
33+
val path = FlexiblePolylineUtil.decodeToLatLngs(ENCODED_WALKWAY)
34+
35+
map.addPolyline(
36+
PolylineOptions()
37+
.addAll(path)
38+
.width(POLYLINE_WIDTH)
39+
.color(POLYLINE_COLOR),
40+
)
41+
42+
Toast.makeText(
43+
this,
44+
getString(R.string.flexible_polyline_summary, path.size),
45+
Toast.LENGTH_SHORT,
46+
).show()
47+
48+
if (!isRestore) {
49+
map.moveCamera(CameraUpdateFactory.newLatLngZoom(CAMERA_TARGET, CAMERA_ZOOM))
50+
}
51+
}
52+
53+
private companion object {
54+
/**
55+
* The Sydney Harbour Bridge walkway, encoded at precision 7. The alphabet is URL safe,
56+
* so the string needs no escaping in source, a URL or a JSON field. The Google format
57+
* sample in [PolyDecodeDemoActivity] contains four backslashes that are doubled in the
58+
* Java literal.
59+
*/
60+
const val ENCODED_WALKWAY =
61+
"BH__k0lU4kokk6CnzQnqDnzQvwDnzQvwDnzQvwDnzQvwDnzQvwDnzQvwDnzQvwDnzQvwD"
62+
63+
val CAMERA_TARGET = LatLng(-33.85343, 151.21083)
64+
const val CAMERA_ZOOM = 15f
65+
const val POLYLINE_WIDTH = 12f
66+
const val POLYLINE_COLOR = 0xFF1A73E8.toInt()
67+
}
68+
}

‎demo/src/main/java/com/google/maps/android/utils/demo/MainActivity.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class MainActivity : ComponentActivity() {
121121
R.string.category_geometry,
122122
listOf(
123123
Demo(R.string.demo_title_poly_decode, PolyDecodeDemoActivity::class.java),
124+
Demo(R.string.demo_title_flexible_polyline, FlexiblePolylineDemoActivity::class.java),
124125
Demo(R.string.demo_title_poly_simplify, PolySimplifyDemoActivity::class.java),
125126
Demo(R.string.demo_title_polyline_progress, PolylineProgressDemoActivity::class.java),
126127
Demo(R.string.demo_title_spherical_distance, DistanceDemoActivity::class.java),

‎demo/src/main/res/values/strings.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<string name="demo_title_poly_decode">PolyUtil.decode</string>
7070
<string name="demo_title_poly_simplify">PolyUtil.simplify</string>
7171
<string name="demo_title_polyline_progress">Polyline Progress</string>
72+
<string name="demo_title_flexible_polyline">FlexiblePolylineUtil.decode</string>
7273
<string name="demo_title_spherical_distance">SphericalUtil.computeDistanceBetween</string>
7374

7475
<!-- Demo Titles: Utilities -->
@@ -143,4 +144,7 @@ Demonstrates how to dynamically control the visual layering (stacking order) of
143144
<b>Tie Resolution:</b><br/>
144145
• When two shapes of the same type share identical z-index values, the Google Maps SDK resolves their stacking order based on <b>insertion (creation) order</b>.
145146
]]></string>
147+
148+
<!-- Flexible Polyline demo -->
149+
<string name="flexible_polyline_summary">Decoded points: %1$d</string>
146150
</resources>

0 commit comments

Comments
 (0)