|
| 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 | +} |
0 commit comments