add UI settings demo

This commit is contained in:
Bella Mangunsong 2018-01-11 15:05:10 +11:00
parent d5d5c98ecc
commit 304e0b1c03
7 changed files with 263 additions and 1 deletions

View File

@ -33,4 +33,6 @@ dependencies {
implementation 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
// EasyPermissions is needed to help us request for permission to access location
implementation 'pub.devrel:easypermissions:1.1.1'
}

View File

@ -16,6 +16,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kotlindemos">
<!-- Access to location is needed for the UI Settings Demo -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -36,6 +39,11 @@
</intent-filter>
</activity>
<activity android:name=".BasicMapDemoActivity" />
<activity android:name=".UiSettingsDemoActivity" />
<activity android:name=".MarkerDemoActivity" />
<activity android:name=".PolygonDemoActivity" />
<activity android:name=".CameraDemoActivity" />
<activity android:name=".BasicMapDemoActivity" />
<activity android:name=".StreetViewPanoramaNavigationDemoActivity" />
<activity android:name=".CircleDemoActivity" />
</application>

View File

@ -29,7 +29,9 @@ class DemoDetailsList {
DemoDetails(
R.string.street_view_panorama_navigation_demo_label,
R.string.street_view_panorama_navigation_demo_details,
StreetViewPanoramaNavigationDemoActivity::class.java)
StreetViewPanoramaNavigationDemoActivity::class.java),
DemoDetails(R.string.ui_settings_demo_label, R.string.ui_settings_demo_details,
UiSettingsDemoActivity::class.java)
)
}
}

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.Manifest
import android.annotation.SuppressLint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.CheckBox
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions
const val REQUEST_CODE_LOCATION = 123
class UiSettingsDemoActivity :
AppCompatActivity(),
OnMapReadyCallback {
private lateinit var map: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ui_settings_demo)
val mapFragment: SupportMapFragment =
supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap?) {
// Return early if map is not initialised properly
map = googleMap ?: return
enableMyLocation()
// Set all the settings of the map to match the current state of the checkboxes
with(map.uiSettings) {
isZoomControlsEnabled = isChecked(R.id.zoom_button)
isCompassEnabled = isChecked(R.id.compass_button)
isMyLocationButtonEnabled = isChecked(R.id.myloc_button)
isIndoorLevelPickerEnabled = isChecked(R.id.level_button)
isMapToolbarEnabled = isChecked(R.id.maptoolbar_button)
isZoomGesturesEnabled = isChecked(R.id.zoomgest_button)
isScrollGesturesEnabled = isChecked(R.id.scrollgest_button)
isTiltGesturesEnabled = isChecked(R.id.tiltgest_button)
isRotateGesturesEnabled = isChecked(R.id.rotategest_button)
}
}
/** On click listener for checkboxes */
fun onClick(view: View) {
if (view !is CheckBox) {
return
}
val isChecked: Boolean = view.isChecked
with(map.uiSettings) {
when (view.id) {
R.id.zoom_button -> isZoomControlsEnabled = isChecked
R.id.compass_button -> isCompassEnabled = isChecked
R.id.myloc_button -> isMyLocationButtonEnabled = isChecked
R.id.level_button -> isIndoorLevelPickerEnabled = isChecked
R.id.maptoolbar_button -> isMapToolbarEnabled = isChecked
R.id.zoomgest_button -> isZoomGesturesEnabled = isChecked
R.id.scrollgest_button -> isScrollGesturesEnabled = isChecked
R.id.tiltgest_button -> isTiltGesturesEnabled = isChecked
R.id.rotategest_button -> isRotateGesturesEnabled = isChecked
}
}
}
/** Returns whether the checkbox with the given id is checked */
private fun isChecked(id: Int) = findViewById<CheckBox>(id)?.isChecked ?: false
/** Override the onRequestPermissionResult to use EasyPermissions */
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
}
/**
* enableMyLocation() will enable the location of the map if the user has given permission
* for the app to access their device location.
* Android Studio requires an explicit check before setting map.isMyLocationEnabled to true
* but we are using EasyPermissions to handle it so we can suppress the "MissingPermission"
* check.
*/
@SuppressLint("MissingPermission")
@AfterPermissionGranted(REQUEST_CODE_LOCATION)
private fun enableMyLocation() {
if (hasLocationPermission()) {
map.isMyLocationEnabled = true
} else {
EasyPermissions.requestPermissions(
this,
getString(R.string.location),
REQUEST_CODE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
}
}
private fun hasLocationPermission(): Boolean {
return EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION)
}
}

View File

@ -0,0 +1,112 @@
<?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.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="175dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="25dp"
android:clipToPadding="false">
<LinearLayout
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white">
<CheckBox
android:id="@+id/zoom_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_zoom" />
<CheckBox
android:id="@+id/compass_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_compass" />
<CheckBox
android:id="@+id/myloc_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_my_location_layer" />
<CheckBox
android:id="@+id/level_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_level_picker" />
<CheckBox
android:id="@+id/maptoolbar_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_map_toolbar" />
<CheckBox
android:id="@+id/zoomgest_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_zoom_gesture" />
<CheckBox
android:id="@+id/scrollgest_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_scroll_gesture" />
<CheckBox
android:id="@+id/tiltgest_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_tilt_gesture" />
<CheckBox
android:id="@+id/rotategest_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/checkbox_rotate_gesture" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

View File

@ -3,4 +3,5 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="white">#D0FFFFFF</color>
</resources>

View File

@ -58,4 +58,17 @@
<string name="go_to_san_francisco">Go to San Francisco</string>
<string name="go_to_santorini">Go to Santorini</string>
<!-- UI Settings Demo -->
<string name="ui_settings_demo_label">UI Settings Demo</string>
<string name="ui_settings_demo_details">Demonstrate how to toggle the UI of a map</string>
<string name="location">Location permission is required to run the demo</string>
<string name="checkbox_zoom">Zoom</string>
<string name="checkbox_compass">Compass</string>
<string name="checkbox_my_location_layer">My Location</string>
<string name="checkbox_level_picker">Level Picker</string>
<string name="checkbox_map_toolbar">Map Toolbar</string>
<string name="checkbox_zoom_gesture">Zoom Gestures</string>
<string name="checkbox_scroll_gesture">Scroll Gestures</string>
<string name="checkbox_tilt_gesture">Tilt Gestures</string>
<string name="checkbox_rotate_gesture">Rotate Gestures</string>
</resources>