Skip to content

Commit c1a8f49

Browse files
committed
add marker close info window on retap demo
1 parent 304e0b1 commit c1a8f49

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

ApiDemos/kotlin/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</intent-filter>
4040
</activity>
4141
<activity android:name=".BasicMapDemoActivity" />
42+
<activity android:name=".CloseInfoWindowDemoActivity" />
4243
<activity android:name=".UiSettingsDemoActivity" />
4344
<activity android:name=".MarkerDemoActivity" />
4445
<activity android:name=".PolygonDemoActivity" />
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2018 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+
* https://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+
17+
package com.example.kotlindemos
18+
19+
import android.support.v7.app.AppCompatActivity
20+
import android.os.Bundle
21+
import com.example.kotlindemos.OnMapAndViewReadyListener.OnGlobalLayoutAndMapReadyListener
22+
import com.google.android.gms.maps.CameraUpdateFactory
23+
import com.google.android.gms.maps.GoogleMap
24+
import com.google.android.gms.maps.SupportMapFragment
25+
import com.google.android.gms.maps.model.LatLng
26+
import com.google.android.gms.maps.model.LatLngBounds
27+
import com.google.android.gms.maps.model.Marker
28+
import com.google.android.gms.maps.model.MarkerOptions
29+
30+
31+
/**
32+
* This shows how to close the info window when the currently selected marker is re-tapped.
33+
*/
34+
class CloseInfoWindowDemoActivity :
35+
AppCompatActivity(),
36+
OnGlobalLayoutAndMapReadyListener {
37+
38+
private lateinit var map: GoogleMap
39+
40+
/** Keeps track of the selected marker. It will be set to null if no marker is selected. */
41+
private var selectedMarker: Marker? = null
42+
43+
/**
44+
* If user tapped on the the marker which was already showing info window,
45+
* the showing info window will be closed. Otherwise will show a different window.
46+
*/
47+
private val markerClickListener = object : GoogleMap.OnMarkerClickListener {
48+
override fun onMarkerClick(marker: Marker?): Boolean {
49+
if (marker == selectedMarker) {
50+
selectedMarker = null
51+
// Return true to indicate we have consumed the event and that we do not
52+
// want the the default behavior to occur (which is for the camera to move
53+
// such that the marker is centered and for the marker's info window to open,
54+
// if it has one).
55+
return true
56+
}
57+
58+
selectedMarker = marker
59+
// Return false to indicate that we have not consumed the event and that
60+
// we wish for the default behavior to occur.
61+
return false
62+
}
63+
}
64+
65+
override fun onCreate(savedInstanceState: Bundle?) {
66+
super.onCreate(savedInstanceState)
67+
setContentView(R.layout.activity_marker_close_info_window_on_retap_demo)
68+
69+
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
70+
OnMapAndViewReadyListener(mapFragment, this)
71+
}
72+
73+
override fun onMapReady(googleMap: GoogleMap?) {
74+
// Return if googleMap was null
75+
map = googleMap ?: return
76+
77+
with(map) {
78+
uiSettings.isZoomControlsEnabled = false
79+
80+
setOnMarkerClickListener(markerClickListener)
81+
82+
// Set listener for map click event. Any showing info window closes
83+
// when the map is clicked. Clear the currently selected marker.
84+
setOnMapClickListener { selectedMarker = null }
85+
86+
setContentDescription(getString(R.string.close_info_window_demo_details))
87+
88+
// Add markers to different cities in Australia and include it in bounds
89+
val bounds = LatLngBounds.Builder()
90+
cities.map { city ->
91+
addMarker(MarkerOptions().apply {
92+
position(city.latLng)
93+
title(city.title)
94+
snippet(city.snippet)
95+
})
96+
bounds.include(city.latLng)
97+
}
98+
99+
moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 50))
100+
}
101+
}
102+
103+
/**
104+
* Class to contain information about a marker.
105+
*
106+
* @property latLng latitude and longitude of the marker
107+
* @property title a string containing the city name
108+
* @property snippet a string containing the population of the city
109+
*/
110+
class MarkerInfo(val latLng: LatLng, val title: String, val snippet: String)
111+
112+
private val cities = listOf(
113+
MarkerInfo(LatLng(-27.47093, 153.0235),
114+
"Brisbane", "Population: 2,074,200"),
115+
MarkerInfo(LatLng(-37.81319, 144.96298),
116+
"Melbourne", "Population: 4,137,400"),
117+
MarkerInfo(LatLng(-33.87365, 151.20689),
118+
"Sydney", "Population: 4,627,300"),
119+
MarkerInfo(LatLng(-34.92873, 138.59995),
120+
"Adelaide", "Population: 1,213,000"),
121+
MarkerInfo(LatLng(-31.952854, 115.857342),
122+
"Perth", "Population: 1,738,800")
123+
)
124+
}

ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/DemoDetailsList.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class DemoDetailsList {
2424
val DEMOS = listOf<DemoDetails>(
2525
DemoDetails(R.string.basic_demo_label, R.string.basic_demo_details,
2626
BasicMapDemoActivity::class.java),
27+
DemoDetails(R.string.close_info_window_demo_label,
28+
R.string.close_info_window_demo_details,
29+
CloseInfoWindowDemoActivity::class.java),
2730
DemoDetails(R.string.circle_demo_label, R.string.circle_demo_details,
2831
CircleDemoActivity::class.java),
2932
DemoDetails(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
Copyright 2018 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+
https://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+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
android:orientation="vertical">
20+
<fragment
21+
android:id="@+id/map"
22+
android:layout_width="match_parent"
23+
android:layout_height="match_parent"
24+
class="com.google.android.gms.maps.SupportMapFragment"/>
25+
</LinearLayout>

ApiDemos/kotlin/app/src/main/res/values/strings.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121

2222
<!-- Basic Map Demo -->
2323
<string name="basic_demo_label">Basic Map</string>
24-
<string name="basic_demo_details">Launches a map with marker pointing at Sydney</string>
24+
<string name="basic_demo_details">Launches a map with marker pointing at Sydney.</string>
2525

2626
<!-- Circle Demo -->
2727
<string name="circle_demo_label">Circle Demo</string>
2828
<string name="circle_demo_details">Demonstrate how to add circles to a map</string>
2929
<string name="properties_circle">Properties for Circle(s)</string>
3030

31+
<!-- Marker Close Info Window On Retap Demo -->
32+
<string name="close_info_window_demo_label">Close Info Window Demo</string>
33+
<string name="close_info_window_demo_details">Demonstrates how to close the info window when the currently selected marker is retapped.</string>
34+
3135
<!-- Polygon Demo -->
3236
<string name="clickable">Clickable</string>
3337
<string name="fill_alpha">Fill Alpha</string>

0 commit comments

Comments
 (0)