/
TileCoordinateDemoActivity.kt
96 lines (88 loc) · 3.99 KB
/
TileCoordinateDemoActivity.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
/*
* 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
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
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.Tile
import com.google.android.gms.maps.model.TileOverlayOptions
import com.google.android.gms.maps.model.TileProvider
import java.io.ByteArrayOutputStream
/**
* This demonstrates tile overlay coordinates.
*/
class TileCoordinateDemoActivity : AppCompatActivity(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.tile_coordinate_demo)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(map: GoogleMap) {
val coordTileProvider: TileProvider = CoordTileProvider(this.applicationContext)
map.addTileOverlay(TileOverlayOptions().tileProvider(coordTileProvider))
}
private class CoordTileProvider(context: Context) : TileProvider {
private val scaleFactor: Float
private val borderTile: Bitmap
override fun getTile(x: Int, y: Int, zoom: Int): Tile {
val coordTile = drawTileCoords(x, y, zoom)
val stream = ByteArrayOutputStream()
coordTile!!.compress(Bitmap.CompressFormat.PNG, 0, stream)
val bitmapData = stream.toByteArray()
return Tile((TILE_SIZE_DP * scaleFactor).toInt(),
(TILE_SIZE_DP * scaleFactor).toInt(), bitmapData)
}
private fun drawTileCoords(x: Int, y: Int, zoom: Int): Bitmap? {
// Synchronize copying the bitmap to avoid a race condition in some devices.
var copy: Bitmap? = null
synchronized(borderTile) { copy = borderTile.copy(Bitmap.Config.ARGB_8888, true) }
val canvas = Canvas(copy!!)
val tileCoords = "($x, $y)"
val zoomLevel = "zoom = $zoom"
/* Paint is not thread safe. */
val mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mTextPaint.textAlign = Paint.Align.CENTER
mTextPaint.textSize = 18 * scaleFactor
canvas.drawText(tileCoords, TILE_SIZE_DP * scaleFactor / 2,
TILE_SIZE_DP * scaleFactor / 2, mTextPaint)
canvas.drawText(zoomLevel, TILE_SIZE_DP * scaleFactor / 2,
TILE_SIZE_DP * scaleFactor * 2 / 3, mTextPaint)
return copy
}
companion object {
private const val TILE_SIZE_DP = 256
}
init {
/* Scale factor based on density, with a 0.6 multiplier to increase tile generation
* speed */
scaleFactor = context.resources.displayMetrics.density * 0.6f
val borderPaint = Paint(Paint.ANTI_ALIAS_FLAG)
borderPaint.style = Paint.Style.STROKE
borderTile = Bitmap.createBitmap((TILE_SIZE_DP * scaleFactor).toInt(),
(TILE_SIZE_DP * scaleFactor).toInt(), Bitmap.Config.ARGB_8888)
val canvas = Canvas(borderTile)
canvas.drawRect(0f, 0f, TILE_SIZE_DP * scaleFactor, TILE_SIZE_DP * scaleFactor,
borderPaint)
}
}
}