-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PolylineDemoActivity.kt
297 lines (255 loc) · 11.8 KB
/
PolylineDemoActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.kotlindemos.polyline
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.ButtCap
import com.google.android.gms.maps.model.Cap
import com.google.android.gms.maps.model.CustomCap
import com.google.android.gms.maps.model.Dash
import com.google.android.gms.maps.model.Dot
import com.google.android.gms.maps.model.Gap
import com.google.android.gms.maps.model.JointType
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PatternItem
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.RoundCap
import com.google.android.gms.maps.model.SquareCap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.CheckBox
import android.widget.SeekBar
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlindemos.R
import java.util.Arrays
/**
* This shows how to draw polylines on a map.
*/
class PolylineDemoActivity :
AppCompatActivity(),
OnMapReadyCallback,
SeekBar.OnSeekBarChangeListener,
AdapterView.OnItemSelectedListener {
private val CUSTOM_CAP_IMAGE_REF_WIDTH_PX = 50
private val INITIAL_STROKE_WIDTH_PX = 5
private val MAX_WIDTH_PX = 100
private val MAX_HUE_DEGREES = 360
private val MAX_ALPHA = 255
private val PATTERN_DASH_LENGTH_PX = 50
private val PATTERN_GAP_LENGTH_PX = 20
// City locations for mutable polyline.
private val adelaideLatLng = LatLng(-34.92873, 138.59995)
private val darwinLatLng = LatLng(-12.4258647, 130.7932231)
private val melbourneLatLng = LatLng(-37.81319, 144.96298)
private val perthLatLng = LatLng(-31.95285, 115.85734)
// Airport locations for geodesic polyline.
private val aklLatLng = LatLng(-37.006254, 174.783018)
private val jfkLatLng = LatLng(40.641051, -73.777485)
private val laxLatLng = LatLng(33.936524, -118.377686)
private val lhrLatLng = LatLng(51.471547, -0.460052)
private val dot = Dot()
private val dash = Dash(PATTERN_DASH_LENGTH_PX.toFloat())
private val gap = Gap(PATTERN_GAP_LENGTH_PX.toFloat())
private val patternDotted = Arrays.asList(dot, gap)
private val patternDashed = Arrays.asList(dash, gap)
private val patternMixed = Arrays.asList(dot, gap, dot, dash, gap)
private lateinit var mutablePolyline: Polyline
private lateinit var hueBar: SeekBar
private lateinit var alphaBar: SeekBar
private lateinit var widthBar: SeekBar
private lateinit var startCapSpinner: Spinner
private lateinit var endCapSpinner: Spinner
private lateinit var jointTypeSpinner: Spinner
private lateinit var patternSpinner: Spinner
private lateinit var clickabilityCheckbox: CheckBox
// These are the options for polyline caps, joints and patterns. We use their
// string resource IDs as identifiers.
private val capTypeNameResourceIds = intArrayOf(R.string.cap_butt,
R.string.cap_round,
R.string.cap_square,
R.string.cap_image)
private val jointTypeNameResourceIds = intArrayOf(R.string.joint_type_default,
R.string.joint_type_bevel,
R.string.joint_type_round)
private val patternTypeNameResourceIds = intArrayOf(R.string.pattern_solid,
R.string.pattern_dashed,
R.string.pattern_dotted,
R.string.pattern_mixed)
// [START maps_android_sample_polylines]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.polyline_demo)
hueBar = findViewById<SeekBar>(R.id.hueSeekBar).apply {
max = MAX_HUE_DEGREES
progress = 0
}
alphaBar = findViewById<SeekBar>(R.id.alphaSeekBar).apply {
max = MAX_ALPHA
progress = MAX_ALPHA
}
widthBar = findViewById<SeekBar>(R.id.widthSeekBar).apply {
max = MAX_WIDTH_PX
progress = MAX_WIDTH_PX / 2
}
startCapSpinner = findViewById<Spinner>(R.id.startCapSpinner).apply {
adapter = ArrayAdapter(this@PolylineDemoActivity,
android.R.layout.simple_spinner_item,
getResourceStrings(capTypeNameResourceIds))
}
endCapSpinner = findViewById<Spinner>(R.id.endCapSpinner).apply {
adapter = ArrayAdapter(this@PolylineDemoActivity,
android.R.layout.simple_spinner_item,
getResourceStrings(capTypeNameResourceIds))
}
jointTypeSpinner = findViewById<Spinner>(R.id.jointTypeSpinner).apply {
adapter = ArrayAdapter(this@PolylineDemoActivity,
android.R.layout.simple_spinner_item,
getResourceStrings(jointTypeNameResourceIds))
}
patternSpinner = findViewById<Spinner>(R.id.patternSpinner).apply {
adapter = ArrayAdapter(
this@PolylineDemoActivity, android.R.layout.simple_spinner_item,
getResourceStrings(patternTypeNameResourceIds))
}
clickabilityCheckbox = findViewById<CheckBox>(R.id.toggleClickability)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
// [START_EXCLUDE silent]
private fun getResourceStrings(resourceIds: IntArray): List<String> {
return resourceIds.map { getString(it) }
}
// [END_EXCLUDE]
override fun onMapReady(googleMap: GoogleMap) {
googleMap
with(googleMap) {
// Override the default content description on the view, for accessibility mode.
setContentDescription(getString(R.string.polyline_demo_description))
// A geodesic polyline that goes around the world.
addPolyline(PolylineOptions().apply {
add(lhrLatLng, aklLatLng, laxLatLng, jfkLatLng, lhrLatLng)
width(INITIAL_STROKE_WIDTH_PX.toFloat())
color(Color.BLUE)
geodesic(true)
clickable(clickabilityCheckbox.isChecked)
})
// Move the googleMap so that it is centered on the mutable polyline.
moveCamera(CameraUpdateFactory.newLatLngZoom(melbourneLatLng, 3f))
// Add a listener for polyline clicks that changes the clicked polyline's color.
setOnPolylineClickListener { polyline ->
// Flip the values of the red, green and blue components of the polyline's color.
polyline.color = polyline.color xor 0x00ffffff
}
}
// A simple polyline across Australia. This polyline will be mutable.
mutablePolyline = googleMap.addPolyline(PolylineOptions().apply{
color(Color.HSVToColor(
alphaBar.progress, floatArrayOf(hueBar.progress.toFloat(), 1f, 1f)))
width(widthBar.progress.toFloat())
clickable(clickabilityCheckbox.isChecked)
add(melbourneLatLng, adelaideLatLng, perthLatLng, darwinLatLng)
})
arrayOf(hueBar, alphaBar, widthBar).map {
it.setOnSeekBarChangeListener(this)
}
arrayOf(startCapSpinner, endCapSpinner, jointTypeSpinner, patternSpinner).map {
it.onItemSelectedListener = this
}
with(mutablePolyline) {
startCap = getSelectedCap(startCapSpinner.selectedItemPosition) ?: ButtCap()
endCap = getSelectedCap(endCapSpinner.selectedItemPosition) ?: ButtCap()
jointType = getSelectedJointType(jointTypeSpinner.selectedItemPosition)
pattern = getSelectedPattern(patternSpinner.selectedItemPosition)
}
clickabilityCheckbox.setOnClickListener {
view -> mutablePolyline.isClickable = (view as CheckBox).isChecked
}
}
// [END maps_android_sample_polylines]
private fun getSelectedCap(pos: Int): Cap? {
return when (capTypeNameResourceIds[pos]) {
R.string.cap_butt -> ButtCap()
R.string.cap_square -> SquareCap()
R.string.cap_round -> RoundCap()
R.string.cap_image -> CustomCap(
BitmapDescriptorFactory.fromResource(R.drawable.chevron),
CUSTOM_CAP_IMAGE_REF_WIDTH_PX.toFloat())
else -> null
}
}
private fun getSelectedJointType(pos: Int): Int {
return when (jointTypeNameResourceIds[pos]) {
R.string.joint_type_bevel -> JointType.BEVEL
R.string.joint_type_round -> JointType.ROUND
R.string.joint_type_default -> JointType.DEFAULT
else -> 0
}
}
private fun getSelectedPattern(pos: Int): List<PatternItem>? {
return when (patternTypeNameResourceIds[pos]) {
R.string.pattern_solid -> null
R.string.pattern_dotted -> patternDotted
R.string.pattern_dashed -> patternDashed
R.string.pattern_mixed -> patternMixed
else -> null
}
}
/**
* Listener for changes in a seekbar's position.
* Can change polyline color, width and transparency.
*/
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
when(seekBar) {
hueBar -> mutablePolyline.color = Color.HSVToColor(
Color.alpha(mutablePolyline.color), floatArrayOf(progress.toFloat(), 1f, 1f))
alphaBar -> {
val prevHSV = FloatArray(3)
Color.colorToHSV(mutablePolyline.color, prevHSV)
mutablePolyline.color = Color.HSVToColor(progress, prevHSV)
}
widthBar -> mutablePolyline.width = progress.toFloat()
}
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
// Don't do anything here.
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
// Don't do anything here.
}
/**
* Listener for changes in a spinner's position.
* Can change the polyline's start and end caps, pattern and joint type.
*/
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
when (parent.id) {
R.id.startCapSpinner -> mutablePolyline.startCap = getSelectedCap(pos) ?: ButtCap()
R.id.endCapSpinner -> mutablePolyline.endCap = getSelectedCap(pos) ?: ButtCap()
R.id.jointTypeSpinner -> mutablePolyline.jointType = getSelectedJointType(pos)
R.id.patternSpinner -> mutablePolyline.pattern = getSelectedPattern(pos)
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Don't do anything here.
}
}