From bb29e21b61d041886ce98cedf8d171d5252559a3 Mon Sep 17 00:00:00 2001 From: Stephen McDonald Date: Tue, 11 Oct 2016 11:41:35 +1100 Subject: [PATCH] Add Marker Info Window Close on Retap Demo Change-Id: Ie98c20ab2be5ce5f52289705524ebafc9db2f444 --- ApiDemos/app/build.gradle | 2 +- ApiDemos/app/src/main/AndroidManifest.xml | 3 + .../com/example/mapdemo/DemoDetailsList.java | 3 + ...kerCloseInfoWindowOnRetapDemoActivity.java | 148 ++++++++++++++++++ .../mapdemo/StyledMapDemoActivity.java | 1 - ...marker_close_info_window_on_retap_demo.xml | 26 +++ ApiDemos/app/src/main/res/values/strings.xml | 2 + 7 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 ApiDemos/app/src/main/java/com/example/mapdemo/MarkerCloseInfoWindowOnRetapDemoActivity.java create mode 100644 ApiDemos/app/src/main/res/layout/marker_close_info_window_on_retap_demo.xml diff --git a/ApiDemos/app/build.gradle b/ApiDemos/app/build.gradle index 9d5d6415..8bf13273 100644 --- a/ApiDemos/app/build.gradle +++ b/ApiDemos/app/build.gradle @@ -23,5 +23,5 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' - compile 'com.google.android.gms:play-services-maps:9.6.0' + compile 'com.google.android.gms:play-services-maps:9.8.0' } diff --git a/ApiDemos/app/src/main/AndroidManifest.xml b/ApiDemos/app/src/main/AndroidManifest.xml index d22ae5af..261da736 100644 --- a/ApiDemos/app/src/main/AndroidManifest.xml +++ b/ApiDemos/app/src/main/AndroidManifest.xml @@ -99,6 +99,9 @@ + diff --git a/ApiDemos/app/src/main/java/com/example/mapdemo/DemoDetailsList.java b/ApiDemos/app/src/main/java/com/example/mapdemo/DemoDetailsList.java index 3958516f..258cc81a 100755 --- a/ApiDemos/app/src/main/java/com/example/mapdemo/DemoDetailsList.java +++ b/ApiDemos/app/src/main/java/com/example/mapdemo/DemoDetailsList.java @@ -65,6 +65,9 @@ public final class DemoDetailsList { new DemoDetails(R.string.marker_demo_label, R.string.marker_demo_description, MarkerDemoActivity.class), + new DemoDetails(R.string.marker_close_info_window_on_retap_demo_label, + R.string.marker_close_info_window_on_retap_demo_description, + MarkerCloseInfoWindowOnRetapDemoActivity.class), new DemoDetails(R.string.multi_map_demo_label, R.string.multi_map_demo_description, MultiMapDemoActivity.class), diff --git a/ApiDemos/app/src/main/java/com/example/mapdemo/MarkerCloseInfoWindowOnRetapDemoActivity.java b/ApiDemos/app/src/main/java/com/example/mapdemo/MarkerCloseInfoWindowOnRetapDemoActivity.java new file mode 100644 index 00000000..7cae5605 --- /dev/null +++ b/ApiDemos/app/src/main/java/com/example/mapdemo/MarkerCloseInfoWindowOnRetapDemoActivity.java @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 + * + * http://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.mapdemo; + +import com.google.android.gms.maps.CameraUpdateFactory; +import com.google.android.gms.maps.GoogleMap; +import com.google.android.gms.maps.GoogleMap.OnMapClickListener; +import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; +import com.google.android.gms.maps.OnMapReadyCallback; +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; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +/** + * This shows how to close the info window when the currently selected marker is re-tapped. + */ +public class MarkerCloseInfoWindowOnRetapDemoActivity extends AppCompatActivity implements + OnMarkerClickListener, + OnMapReadyCallback, + OnMapClickListener { + + private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235); + private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298); + private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689); + private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995); + private static final LatLng PERTH = new LatLng(-31.952854, 115.857342); + + private GoogleMap mMap = null; + + /** + * Keeps track of the selected marker. + */ + private Marker mSelectedMarker; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.marker_close_info_window_on_retap_demo); + + SupportMapFragment mapFragment = + (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); + mapFragment.getMapAsync(this); + } + + @Override + public void onMapReady(GoogleMap map) { + mMap = map; + + // Hide the zoom controls. + mMap.getUiSettings().setZoomControlsEnabled(false); + + // Add lots of markers to the map. + addMarkersToMap(); + + // Set listener for marker click event. See the bottom of this class for its behavior. + mMap.setOnMarkerClickListener(this); + + // Set listener for map click event. See the bottom of this class for its behavior. + mMap.setOnMapClickListener(this); + + // Override the default content description on the view, for accessibility mode. + // Ideally this string would be localized. + map.setContentDescription("Demo showing how to close the info window when the currently" + + " selected marker is re-tapped."); + + LatLngBounds bounds = new LatLngBounds.Builder() + .include(PERTH) + .include(SYDNEY) + .include(ADELAIDE) + .include(BRISBANE) + .include(MELBOURNE) + .build(); + mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50)); + } + + private void addMarkersToMap() { + mMap.addMarker(new MarkerOptions() + .position(BRISBANE) + .title("Brisbane") + .snippet("Population: 2,074,200")); + + mMap.addMarker(new MarkerOptions() + .position(SYDNEY) + .title("Sydney") + .snippet("Population: 4,627,300")); + + mMap.addMarker(new MarkerOptions() + .position(MELBOURNE) + .title("Melbourne") + .snippet("Population: 4,137,400")); + + mMap.addMarker(new MarkerOptions() + .position(PERTH) + .title("Perth") + .snippet("Population: 1,738,800")); + + mMap.addMarker(new MarkerOptions() + .position(ADELAIDE) + .title("Adelaide") + .snippet("Population: 1,213,000")); + } + + @Override + public void onMapClick(final LatLng point) { + // Any showing info window closes when the map is clicked. + // Clear the currently selected marker. + mSelectedMarker = null; + } + + @Override + public boolean onMarkerClick(final Marker marker) { + // The user has re-tapped on the marker which was already showing an info window. + if (marker.equals(mSelectedMarker)) { + // The showing info window has already been closed - that's the first thing to happen + // when any marker is clicked. + // 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). + mSelectedMarker = null; + return true; + } + + mSelectedMarker = marker; + + // Return false to indicate that we have not consumed the event and that we wish + // for the default behavior to occur. + return false; + } +} diff --git a/ApiDemos/app/src/main/java/com/example/mapdemo/StyledMapDemoActivity.java b/ApiDemos/app/src/main/java/com/example/mapdemo/StyledMapDemoActivity.java index ed1d363a..65cc9d5c 100644 --- a/ApiDemos/app/src/main/java/com/example/mapdemo/StyledMapDemoActivity.java +++ b/ApiDemos/app/src/main/java/com/example/mapdemo/StyledMapDemoActivity.java @@ -22,7 +22,6 @@ import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MapStyleOptions; -import com.google.android.gms.maps.model.MarkerOptions; import android.content.DialogInterface; import android.os.Bundle; diff --git a/ApiDemos/app/src/main/res/layout/marker_close_info_window_on_retap_demo.xml b/ApiDemos/app/src/main/res/layout/marker_close_info_window_on_retap_demo.xml new file mode 100644 index 00000000..82d6dc29 --- /dev/null +++ b/ApiDemos/app/src/main/res/layout/marker_close_info_window_on_retap_demo.xml @@ -0,0 +1,26 @@ + + + + + diff --git a/ApiDemos/app/src/main/res/values/strings.xml b/ApiDemos/app/src/main/res/values/strings.xml index 18457f2d..e1872f30 100755 --- a/ApiDemos/app/src/main/res/values/strings.xml +++ b/ApiDemos/app/src/main/res/values/strings.xml @@ -80,6 +80,8 @@ Map In Pager Demonstrates how to add a map to a ViewPager. Markers + Marker Close Info Window on Retap + Demonstrates how to close the info window when the currently selected marker is retapped. Markers Demonstrates how to add Markers to a map. Move the camera Multiple Maps