add marker close info window on retap demo

This commit is contained in:
Bella Mangunsong 2018-01-16 13:24:46 +11:00
parent 304e0b1c03
commit c1a8f49f74
5 changed files with 158 additions and 1 deletions

View File

@ -39,6 +39,7 @@
</intent-filter>
</activity>
<activity android:name=".BasicMapDemoActivity" />
<activity android:name=".CloseInfoWindowDemoActivity" />
<activity android:name=".UiSettingsDemoActivity" />
<activity android:name=".MarkerDemoActivity" />
<activity android:name=".PolygonDemoActivity" />

View File

@ -0,0 +1,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.support.v7.app.AppCompatActivity
import android.os.Bundle
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")
)
}

View File

@ -24,6 +24,9 @@ class DemoDetailsList {
val DEMOS = listOf<DemoDetails>(
DemoDetails(R.string.basic_demo_label, R.string.basic_demo_details,
BasicMapDemoActivity::class.java),
DemoDetails(R.string.close_info_window_demo_label,
R.string.close_info_window_demo_details,
CloseInfoWindowDemoActivity::class.java),
DemoDetails(R.string.circle_demo_label, R.string.circle_demo_details,
CircleDemoActivity::class.java),
DemoDetails(

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?><!--
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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

View File

@ -21,13 +21,17 @@
<!-- Basic Map Demo -->
<string name="basic_demo_label">Basic Map</string>
<string name="basic_demo_details">Launches a map with marker pointing at Sydney</string>
<string name="basic_demo_details">Launches a map with marker pointing at Sydney.</string>
<!-- Circle Demo -->
<string name="circle_demo_label">Circle Demo</string>
<string name="circle_demo_details">Demonstrate how to add circles to a map</string>
<string name="properties_circle">Properties for Circle(s)</string>
<!-- Marker Close Info Window On Retap Demo -->
<string name="close_info_window_demo_label">Close Info Window Demo</string>
<string name="close_info_window_demo_details">Demonstrates how to close the info window when the currently selected marker is retapped.</string>
<!-- Polygon Demo -->
<string name="clickable">Clickable</string>
<string name="fill_alpha">Fill Alpha</string>