Add camera clamping demo

Change-Id: I8dfc0dbaad250f67d05c0c173ff79c525f9338e4
This commit is contained in:
Stephen McDonald 2016-07-19 11:57:30 +10:00
parent 7b9c03e5a7
commit d4b2a1ee5e
4 changed files with 298 additions and 2 deletions

View File

@ -20,7 +20,7 @@
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
@ -45,7 +45,7 @@
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
@ -66,6 +66,9 @@
<activity
android:name=".CameraDemoActivity"
android:label="@string/camera_demo_label"/>
<activity
android:name=".CameraClampingDemoActivity"
android:label="@string/camera_clamping_demo_label"/>
<activity
android:name=".CircleDemoActivity"
android:label="@string/circle_demo_label"/>

View File

@ -0,0 +1,184 @@
/*
* 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.OnCameraIdleListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* This shows how to constrain the camera to specific boundaries and zoom levels.
*/
public class CameraClampingDemoActivity extends AppCompatActivity
implements OnMapReadyCallback, OnCameraIdleListener {
private static final String TAG = CameraClampingDemoActivity.class.getSimpleName();
private static final float ZOOM_DELTA = 2.0f;
private static final float DEFAULT_MIN_ZOOM = 2.0f;
private static final float DEFAULT_MAX_ZOOM = 22.0f;
private static final LatLngBounds ADELAIDE = new LatLngBounds(
new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
private static final CameraPosition ADELAIDE_CAMERA = new CameraPosition.Builder()
.target(new LatLng(-34.92873, 138.59995)).zoom(20.0f).bearing(0).tilt(0).build();
private static final LatLngBounds PACIFIC = new LatLngBounds(
new LatLng(-15.0, 165.0), new LatLng(15.0, -165.0));
private static final CameraPosition PACIFIC_CAMERA = new CameraPosition.Builder()
.target(new LatLng(0, -180)).zoom(4.0f).bearing(0).tilt(0).build();
private GoogleMap mMap;
/**
* Internal min zoom level that can be toggled via the demo.
*/
private float mMinZoom;
/**
* Internal max zoom level that can be toggled via the demo.
*/
private float mMaxZoom;
private TextView mCameraTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_clamping_demo);
mMap = null;
resetMinMaxZoom();
mCameraTextView = (TextView) findViewById(R.id.camera_text);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
map.setOnCameraIdleListener(this);
}
@Override
public void onCameraIdle() {
mCameraTextView.setText(mMap.getCameraPosition().toString());
}
/**
* Before the map is ready many calls will fail.
* This should be called on all entry points that call methods on the Google Maps API.
*/
private boolean checkReady() {
if (mMap == null) {
Toast.makeText(this, R.string.map_not_ready, Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private void toast(String msg) {
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}
private void resetMinMaxZoom() {
mMinZoom = DEFAULT_MIN_ZOOM;
mMaxZoom = DEFAULT_MAX_ZOOM;
}
/**
* Click handler for clamping to Adelaide button.
* @param view
*/
public void onClampToAdelaide(View view) {
if (!checkReady()) {
return;
}
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(ADELAIDE_CAMERA));
}
/**
* Click handler for clamping to Pacific button.
* @param view
*/
public void onClampToPacific(View view) {
if (!checkReady()) {
return;
}
mMap.setLatLngBoundsForCameraTarget(PACIFIC);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(PACIFIC_CAMERA));
}
public void onLatLngClampReset(View view) {
if (!checkReady()) {
return;
}
// Setting bounds to null removes any previously set bounds.
mMap.setLatLngBoundsForCameraTarget(null);
toast("LatLngBounds clamp reset.");
}
public void onSetMinZoomClamp(View view) {
if (!checkReady()) {
return;
}
mMinZoom += ZOOM_DELTA;
// Constrains the minimum zoom level.
mMap.setMinZoomPreference(mMinZoom);
toast("Min zoom preference set to: " + mMinZoom);
}
public void onSetMaxZoomClamp(View view) {
if (!checkReady()) {
return;
}
mMaxZoom -= ZOOM_DELTA;
// Constrains the maximum zoom level.
mMap.setMaxZoomPreference(mMaxZoom);
toast("Max zoom preference set to: " + mMaxZoom);
}
public void onMinMaxZoomClampReset(View view) {
if (!checkReady()) {
return;
}
resetMinMaxZoom();
mMap.resetMinMaxZoomPreference();
toast("Min/Max zoom preferences reset.");
}
}

View File

@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/clamp_min_zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSetMinZoomClamp"
android:text="@string/clamp_min_zoom"/>
<Button
android:id="@+id/clamp_max_zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSetMaxZoomClamp"
android:text="@string/clamp_max_zoom"/>
<Button
android:id="@+id/clamp_zoom_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMinMaxZoomClampReset"
android:text="@string/clamp_zoom_reset"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/clamp_latlng_adelaide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClampToAdelaide"
android:layout_weight="0.5"
android:text="@string/clamp_latlng_adelaide"/>
<Button
android:id="@+id/clamp_latlng_pacific"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClampToPacific"
android:layout_weight="0.5"
android:text="@string/clamp_latlng_pacific"/>
<Button
android:id="@+id/clamp_latlng_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onLatLngClampReset"
android:text="@string/clamp_latlng_reset"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/camera_text"
android:text="@string/move_the_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraMinZoomPreference="10.0"
map:cameraMaxZoomPreference="14.0"
map:latLngBoundsSouthWestLatitude="37.4"
map:latLngBoundsSouthWestLongitude="-122.1"
map:latLngBoundsNorthEastLatitude="37.45"
map:latLngBoundsNorthEastLongitude="-122.05"
map:cameraTargetLat="37.421976"
map:cameraTargetLng="-122.084065"
map:cameraZoom="12"/>
</LinearLayout>

View File

@ -28,6 +28,16 @@
<string name="camera_demo_description">Demonstrates camera functions.</string>
<string name="circle_demo_label">Circles</string>
<string name="circle_demo_description">Demonstrates how to add Circles to a map.</string>
<string name="camera_clamping_demo_label">Camera Clamping</string>
<string name="camera_clamping_demo_description">Demonstrates how to constrain the camera to specific boundaries and zoom levels.</string>
<string name="clamp_latlng_adelaide">Clamp to Adelaide</string>
<string name="clamp_latlng_pacific">Clamp to Pacific</string>
<string name="clamp_latlng_reset">Reset LatLng Bounds</string>
<string name="clamp_min_zoom">MinZoom++</string>
<string name="clamp_max_zoom">MaxZoom--</string>
<string name="clamp_zoom_reset">Reset Zoom Bounds</string>
<string name="clear_map">Clear</string>
<string name="clickable">Clickable</string>
<string name="compass">Compass</string>