-
Notifications
You must be signed in to change notification settings - Fork 3k
/
CloseInfoWindowDemoActivity.kt
124 lines (105 loc) · 4.77 KB
/
CloseInfoWindowDemoActivity.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
/*
* 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.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlindemos.OnMapAndViewReadyListener.OnGlobalLayoutAndMapReadyListener
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
/**
* This shows how to close the info window when the currently selected marker is re-tapped.
*/
class CloseInfoWindowDemoActivity :
AppCompatActivity(),
OnGlobalLayoutAndMapReadyListener {
private lateinit var map: GoogleMap
/** Keeps track of the selected marker. It will be set to null if no marker is selected. */
private var selectedMarker: Marker? = null
/**
* If user tapped on the the marker which was already showing info window,
* the showing info window will be closed. Otherwise will show a different window.
*/
private val markerClickListener = object : GoogleMap.OnMarkerClickListener {
override fun onMarkerClick(marker: Marker?): Boolean {
if (marker == selectedMarker) {
selectedMarker = null
// Return true to indicate we have consumed the event and that we do not
// want the the default behavior to occur (which is for the camera to move
// such that the marker is centered and for the marker's info window to open,
// if it has one).
return true
}
selectedMarker = marker
// Return false to indicate that we have not consumed the event and that
// we wish for the default behavior to occur.
return false
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_marker_close_info_window_on_retap_demo)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
OnMapAndViewReadyListener(mapFragment, this)
}
override fun onMapReady(googleMap: GoogleMap?) {
// Return if googleMap was null
map = googleMap ?: return
with(map) {
uiSettings.isZoomControlsEnabled = false
setOnMarkerClickListener(markerClickListener)
// Set listener for map click event. Any showing info window closes
// when the map is clicked. Clear the currently selected marker.
setOnMapClickListener { selectedMarker = null }
setContentDescription(getString(R.string.close_info_window_demo_details))
// Add markers to different cities in Australia and include it in bounds
val bounds = LatLngBounds.Builder()
cities.map { city ->
addMarker(MarkerOptions().apply {
position(city.latLng)
title(city.title)
snippet(city.snippet)
})
bounds.include(city.latLng)
}
moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 50))
}
}
/**
* Class to contain information about a marker.
*
* @property latLng latitude and longitude of the marker
* @property title a string containing the city name
* @property snippet a string containing the population of the city
*/
class MarkerInfo(val latLng: LatLng, val title: String, val snippet: String)
private val cities = listOf(
MarkerInfo(LatLng(-27.47093, 153.0235),
"Brisbane", "Population: 2,074,200"),
MarkerInfo(LatLng(-37.81319, 144.96298),
"Melbourne", "Population: 4,137,400"),
MarkerInfo(LatLng(-33.87365, 151.20689),
"Sydney", "Population: 4,627,300"),
MarkerInfo(LatLng(-34.92873, 138.59995),
"Adelaide", "Population: 1,213,000"),
MarkerInfo(LatLng(-31.952854, 115.857342),
"Perth", "Population: 1,738,800")
)
}