-
Notifications
You must be signed in to change notification settings - Fork 3k
/
PolyActivity.kt
253 lines (230 loc) · 10.1 KB
/
PolyActivity.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
// Copyright 2020 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
//
// http://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.polygons
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnPolygonClickListener
import com.google.android.gms.maps.GoogleMap.OnPolylineClickListener
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.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.Polygon
import com.google.android.gms.maps.model.PolygonOptions
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.RoundCap
/**
* An activity that displays a Google map with polylines to represent paths or routes,
* and polygons to represent areas.
*/
// [START maps_poly_activity_on_map_ready]
class PolyActivity : AppCompatActivity(), OnMapReadyCallback, OnPolylineClickListener, OnPolygonClickListener {
// [START EXCLUDE]
// [START maps_poly_activity_get_map_async]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps)
// Get the SupportMapFragment and request notification when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
// [END maps_poly_activity_get_map_async]
/**
* Manipulates the map when it's available.
* The API invokes this callback when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera.
* In this tutorial, we add polylines and polygons to represent routes and areas on the map.
*/
// [END EXCLUDE]
override fun onMapReady(googleMap: GoogleMap) {
// Add polylines to the map.
// Polylines are useful to show a route or some other connection between points.
// [START maps_poly_activity_add_polyline_set_tag]
// [START maps_poly_activity_add_polyline]
val polyline1 = googleMap.addPolyline(PolylineOptions()
.clickable(true)
.add(
LatLng(-35.016, 143.321),
LatLng(-34.747, 145.592),
LatLng(-34.364, 147.891),
LatLng(-33.501, 150.217),
LatLng(-32.306, 149.248),
LatLng(-32.491, 147.309)))
// [END maps_poly_activity_add_polyline]
// [START_EXCLUDE silent]
// Store a data object with the polyline, used here to indicate an arbitrary type.
polyline1.tag = "A"
// [END maps_poly_activity_add_polyline_set_tag]
// Style the polyline.
stylePolyline(polyline1)
val polyline2 = googleMap.addPolyline(PolylineOptions()
.clickable(true)
.add(
LatLng(-29.501, 119.700),
LatLng(-27.456, 119.672),
LatLng(-25.971, 124.187),
LatLng(-28.081, 126.555),
LatLng(-28.848, 124.229),
LatLng(-28.215, 123.938)))
polyline2.tag = "B"
stylePolyline(polyline2)
// [START maps_poly_activity_add_polygon]
// Add polygons to indicate areas on the map.
val polygon1 = googleMap.addPolygon(PolygonOptions()
.clickable(true)
.add(
LatLng(-27.457, 153.040),
LatLng(-33.852, 151.211),
LatLng(-37.813, 144.962),
LatLng(-34.928, 138.599)))
// Store a data object with the polygon, used here to indicate an arbitrary type.
polygon1.tag = "alpha"
// Style the polygon.
// [END maps_poly_activity_add_polygon]
stylePolygon(polygon1)
val polygon2 = googleMap.addPolygon(PolygonOptions()
.clickable(true)
.add(
LatLng(-31.673, 128.892),
LatLng(-31.952, 115.857),
LatLng(-17.785, 122.258),
LatLng(-12.4258, 130.7932)))
polygon2.tag = "beta"
stylePolygon(polygon2)
// [END_EXCLUDE]
// Position the map's camera near Alice Springs in the center of Australia,
// and set the zoom factor so most of Australia shows on the screen.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(-23.684, 133.903), 4f))
// Set listeners for click events.
googleMap.setOnPolylineClickListener(this)
googleMap.setOnPolygonClickListener(this)
}
// [END maps_poly_activity_on_map_ready]
// [START maps_poly_activity_style_polyline]
private val COLOR_BLACK_ARGB = -0x1000000
private val POLYLINE_STROKE_WIDTH_PX = 12
/**
* Styles the polyline, based on type.
* @param polyline The polyline object that needs styling.
*/
private fun stylePolyline(polyline: Polyline) {
// Get the data object stored with the polyline.
val type = polyline.tag?.toString() ?: ""
when (type) {
"A" -> {
// Use a custom bitmap as the cap at the start of the line.
polyline.startCap = CustomCap(
BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10f)
}
"B" -> {
// Use a round cap at the start of the line.
polyline.startCap = RoundCap()
}
}
polyline.endCap = RoundCap()
polyline.width = POLYLINE_STROKE_WIDTH_PX.toFloat()
polyline.color = COLOR_BLACK_ARGB
polyline.jointType = JointType.ROUND
}
// [END maps_poly_activity_style_polyline]
// [START maps_poly_activity_on_polyline_click]
private val PATTERN_GAP_LENGTH_PX = 20
private val DOT: PatternItem = Dot()
private val GAP: PatternItem = Gap(PATTERN_GAP_LENGTH_PX.toFloat())
// Create a stroke pattern of a gap followed by a dot.
private val PATTERN_POLYLINE_DOTTED = listOf(GAP, DOT)
/**
* Listens for clicks on a polyline.
* @param polyline The polyline object that the user has clicked.
*/
override fun onPolylineClick(polyline: Polyline) {
// Flip from solid stroke to dotted stroke pattern.
if (polyline.pattern == null || !polyline.pattern!!.contains(DOT)) {
polyline.pattern = PATTERN_POLYLINE_DOTTED
} else {
// The default pattern is a solid stroke.
polyline.pattern = null
}
Toast.makeText(this, "Route type " + polyline.tag.toString(),
Toast.LENGTH_SHORT).show()
}
// [END maps_poly_activity_on_polyline_click]
/**
* Listens for clicks on a polygon.
* @param polygon The polygon object that the user has clicked.
*/
override fun onPolygonClick(polygon: Polygon) {
// Flip the values of the red, green, and blue components of the polygon's color.
var color = polygon.strokeColor xor 0x00ffffff
polygon.strokeColor = color
color = polygon.fillColor xor 0x00ffffff
polygon.fillColor = color
Toast.makeText(this, "Area type ${polygon.tag?.toString()}", Toast.LENGTH_SHORT).show()
}
// [START maps_poly_activity_style_polygon]
private val COLOR_WHITE_ARGB = -0x1
private val COLOR_DARK_GREEN_ARGB = -0xc771c4
private val COLOR_LIGHT_GREEN_ARGB = -0x7e387c
private val COLOR_DARK_ORANGE_ARGB = -0xa80e9
private val COLOR_LIGHT_ORANGE_ARGB = -0x657db
private val POLYGON_STROKE_WIDTH_PX = 8
private val PATTERN_DASH_LENGTH_PX = 20
private val DASH: PatternItem = Dash(PATTERN_DASH_LENGTH_PX.toFloat())
// Create a stroke pattern of a gap followed by a dash.
private val PATTERN_POLYGON_ALPHA = listOf(GAP, DASH)
// Create a stroke pattern of a dot followed by a gap, a dash, and another gap.
private val PATTERN_POLYGON_BETA = listOf(DOT, GAP, DASH, GAP)
/**
* Styles the polygon, based on type.
* @param polygon The polygon object that needs styling.
*/
private fun stylePolygon(polygon: Polygon) {
// Get the data object stored with the polygon.
val type = polygon.tag?.toString() ?: ""
var pattern: List<PatternItem>? = null
var strokeColor = COLOR_BLACK_ARGB
var fillColor = COLOR_WHITE_ARGB
when (type) {
"alpha" -> {
// Apply a stroke pattern to render a dashed line, and define colors.
pattern = PATTERN_POLYGON_ALPHA
strokeColor = COLOR_DARK_GREEN_ARGB
fillColor = COLOR_LIGHT_GREEN_ARGB
}
"beta" -> {
// Apply a stroke pattern to render a line of dots and dashes, and define colors.
pattern = PATTERN_POLYGON_BETA
strokeColor = COLOR_DARK_ORANGE_ARGB
fillColor = COLOR_LIGHT_ORANGE_ARGB
}
}
polygon.strokePattern = pattern
polygon.strokeWidth = POLYGON_STROKE_WIDTH_PX.toFloat()
polygon.strokeColor = strokeColor
polygon.fillColor = fillColor
}
// [END maps_poly_activity_style_polygon]
}