Update ApiDemos sample for Google Play Services 8.4.

Extend the GroundOverlay demo to show clickable and overlapping overlays.
Add new clickable listeners to polygon and polyline demos.
Add new long click and close listeners to info window demo.

Change-Id: Idb367bf1c71a97e64cf6a54db54bccf1130ae2ca
This commit is contained in:
Jan-Felix Schmakeit 2015-12-09 16:40:39 +11:00
parent feffd7e0db
commit 14fbe87c47
10 changed files with 211 additions and 43 deletions

View File

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.mapdemo"
@ -22,6 +22,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services-maps:8.4.0'
}

View File

@ -29,6 +29,7 @@ import com.google.android.gms.maps.model.LatLng;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
@ -39,16 +40,22 @@ import java.util.List;
* This shows how to add a ground overlay to a map.
*/
public class GroundOverlayDemoActivity extends AppCompatActivity
implements OnSeekBarChangeListener, OnMapReadyCallback {
implements OnSeekBarChangeListener, OnMapReadyCallback,
GoogleMap.OnGroundOverlayClickListener {
private static final int TRANSPARENCY_MAX = 100;
private static final LatLng NEWARK = new LatLng(40.714086, -74.228697);
private static final LatLng NEAR_NEWARK =
new LatLng(NEWARK.latitude - 0.001, NEWARK.longitude - 0.025);
private final List<BitmapDescriptor> mImages = new ArrayList<BitmapDescriptor>();
private GroundOverlay mGroundOverlay;
private GroundOverlay mGroundOverlayRotated;
private SeekBar mTransparencyBar;
private int mCurrentEntry = 0;
@ -69,13 +76,24 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
@Override
public void onMapReady(GoogleMap map) {
// Register a listener to respond to clicks on GroundOverlays.
map.setOnGroundOverlayClickListener(this);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
mImages.clear();
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922));
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny));
mCurrentEntry = 0;
// Add a small, rotated overlay that is clickable by default
// (set by the initial state of the checkbox.)
mGroundOverlayRotated = map.addGroundOverlay(new GroundOverlayOptions()
.image(mImages.get(1)).anchor(0, 1)
.position(NEAR_NEWARK, 4300f, 3025f)
.bearing(30)
.clickable(((CheckBox) findViewById(R.id.toggleClickability)).isChecked()));
// Add a large overlay at Newark on top of the smaller overlay.
mGroundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
.image(mImages.get(mCurrentEntry)).anchor(0, 1)
.position(NEWARK, 8600f, 6500f));
@ -106,4 +124,24 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
mCurrentEntry = (mCurrentEntry + 1) % mImages.size();
mGroundOverlay.setImage(mImages.get(mCurrentEntry));
}
/**
* Toggles the visibility between 100% and 50% when a {@link GroundOverlay} is clicked.
*/
@Override
public void onGroundOverlayClick(GroundOverlay groundOverlay) {
// Toggle transparency value between 0.0f and 0.5f. Initial default value is 0.0f.
mGroundOverlayRotated.setTransparency(0.5f - mGroundOverlayRotated.getTransparency());
}
/**
* Toggles the clickability of the smaller, rotated overlay based on the state of the View that
* triggered this call.
* This callback is defined on the CheckBox in the layout for this Activity.
*/
public void toggleClickability(View view) {
if (mGroundOverlayRotated != null) {
mGroundOverlayRotated.setClickable(((CheckBox) view).isChecked());
}
}
}

View File

@ -20,6 +20,8 @@ import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowCloseListener;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.OnMapReadyCallback;
@ -64,7 +66,9 @@ public class MarkerDemoActivity extends AppCompatActivity implements
OnInfoWindowClickListener,
OnMarkerDragListener,
OnSeekBarChangeListener,
OnMapReadyCallback {
OnMapReadyCallback,
OnInfoWindowLongClickListener,
OnInfoWindowCloseListener {
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
@ -230,6 +234,8 @@ public class MarkerDemoActivity extends AppCompatActivity implements
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerDragListener(this);
mMap.setOnInfoWindowCloseListener(this);
mMap.setOnInfoWindowLongClickListener(this);
// Override the default content description on the view, for accessibility mode.
// Ideally this string would be localised.
@ -417,6 +423,16 @@ public class MarkerDemoActivity extends AppCompatActivity implements
Toast.makeText(this, "Click Info Window", Toast.LENGTH_SHORT).show();
}
@Override
public void onInfoWindowClose(Marker marker) {
Toast.makeText(this, "Close Info Window", Toast.LENGTH_SHORT).show();
}
@Override
public void onInfoWindowLongClick(Marker marker) {
Toast.makeText(this, "Info Window long click", Toast.LENGTH_SHORT).show();
}
@Override
public void onMarkerDragStart(Marker marker) {
mTopText.setText("onMarkerDragStart");
@ -431,4 +447,5 @@ public class MarkerDemoActivity extends AppCompatActivity implements
public void onMarkerDrag(Marker marker) {
mTopText.setText("onMarkerDrag. Current Position: " + marker.getPosition());
}
}

View File

@ -27,6 +27,8 @@ import com.google.android.gms.maps.model.PolygonOptions;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
@ -49,12 +51,16 @@ public class PolygonDemoActivity extends AppCompatActivity
private Polygon mMutablePolygon;
private Polygon mClickablePolygonWithHoles;
private SeekBar mColorBar;
private SeekBar mAlphaBar;
private SeekBar mWidthBar;
private CheckBox mClickabilityCheckbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -72,6 +78,8 @@ public class PolygonDemoActivity extends AppCompatActivity
mWidthBar.setMax(WIDTH_MAX);
mWidthBar.setProgress(10);
mClickabilityCheckbox = (CheckBox) findViewById(R.id.toggleClickability);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@ -84,16 +92,19 @@ public class PolygonDemoActivity extends AppCompatActivity
map.setContentDescription("Google Map with polygons.");
// Create a rectangle with two rectangular holes.
map.addPolygon(new PolygonOptions()
mClickablePolygonWithHoles = map.addPolygon(new PolygonOptions()
.addAll(createRectangle(new LatLng(-20, 130), 5, 5))
.addHole(createRectangle(new LatLng(-22, 128), 1, 1))
.addHole(createRectangle(new LatLng(-18, 133), 0.5, 1.5))
.fillColor(Color.CYAN)
.strokeColor(Color.BLUE)
.strokeWidth(5));
.strokeWidth(5)
.clickable(mClickabilityCheckbox.isChecked()));
// Create a rectangle centered at Sydney.
PolygonOptions options = new PolygonOptions().addAll(createRectangle(SYDNEY, 5, 8));
PolygonOptions options = new PolygonOptions()
.addAll(createRectangle(SYDNEY, 5, 8))
.clickable(mClickabilityCheckbox.isChecked());
int fillColor = Color.HSVToColor(
mAlphaBar.getProgress(), new float[]{mColorBar.getProgress(), 1, 1});
@ -102,12 +113,29 @@ public class PolygonDemoActivity extends AppCompatActivity
.strokeColor(Color.BLACK)
.fillColor(fillColor));
// Create another polygon that overlaps the previous two.
// Clickability defaults to false, so this one won't accept clicks.
map.addPolygon(new PolygonOptions()
.addAll(createRectangle(new LatLng(-27, 140), 10, 7))
.fillColor(Color.WHITE)
.strokeColor(Color.BLACK));
mColorBar.setOnSeekBarChangeListener(this);
mAlphaBar.setOnSeekBarChangeListener(this);
mWidthBar.setOnSeekBarChangeListener(this);
// Move the map so that it is centered on the mutable polygon.
map.moveCamera(CameraUpdateFactory.newLatLng(SYDNEY));
// Add a listener for polygon clicks that changes the clicked polygon's stroke color.
map.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
@Override
public void onPolygonClick(Polygon polygon) {
// Flip the r, g and b components of the polygon's stroke color.
int strokeColor = polygon.getStrokeColor() ^ 0x00ffffff;
polygon.setStrokeColor(strokeColor);
}
});
}
/**
@ -149,4 +177,18 @@ public class PolygonDemoActivity extends AppCompatActivity
mMutablePolygon.setStrokeWidth(progress);
}
}
}
/**
* Toggles the clickability of two polygons based on the state of the View that triggered this
* call.
* This callback is defined on the CheckBox in the layout for this Activity.
*/
public void toggleClickability(View view) {
if (mClickablePolygonWithHoles != null) {
mClickablePolygonWithHoles.setClickable(((CheckBox) view).isChecked());
}
if (mMutablePolygon != null) {
mMutablePolygon.setClickable(((CheckBox) view).isChecked());
}
}
}

View File

@ -27,6 +27,8 @@ import com.google.android.gms.maps.model.PolylineOptions;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
@ -60,12 +62,16 @@ public class PolylineDemoActivity extends AppCompatActivity
private Polyline mMutablePolyline;
private Polyline mClickablePolyline;
private SeekBar mColorBar;
private SeekBar mAlphaBar;
private SeekBar mWidthBar;
private CheckBox mClickabilityCheckbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -83,6 +89,8 @@ public class PolylineDemoActivity extends AppCompatActivity
mWidthBar.setMax(WIDTH_MAX);
mWidthBar.setProgress(10);
mClickabilityCheckbox = (CheckBox) findViewById(R.id.toggleClickability);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@ -99,11 +107,12 @@ public class PolylineDemoActivity extends AppCompatActivity
.add(MELBOURNE, ADELAIDE, PERTH));
// A geodesic polyline that goes around the world.
map.addPolyline((new PolylineOptions())
mClickablePolyline = map.addPolyline((new PolylineOptions())
.add(LHR, AKL, LAX, JFK, LHR)
.width(5)
.color(Color.BLUE)
.geodesic(true));
.geodesic(true)
.clickable(mClickabilityCheckbox.isChecked()));
// Rectangle centered at Sydney. This polyline will be mutable.
int radius = 5;
@ -117,7 +126,8 @@ public class PolylineDemoActivity extends AppCompatActivity
mAlphaBar.getProgress(), new float[]{mColorBar.getProgress(), 1, 1});
mMutablePolyline = map.addPolyline(options
.color(color)
.width(mWidthBar.getProgress()));
.width(mWidthBar.getProgress())
.clickable(mClickabilityCheckbox.isChecked()));
mColorBar.setOnSeekBarChangeListener(this);
mAlphaBar.setOnSeekBarChangeListener(this);
@ -125,6 +135,16 @@ public class PolylineDemoActivity extends AppCompatActivity
// Move the map so that it is centered on the mutable polyline.
map.moveCamera(CameraUpdateFactory.newLatLng(SYDNEY));
// Add a listener for polyline clicks that changes the clicked polyline's color.
map.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
// Flip the values of the r, g and b components of the polyline's color.
int strokeColor = polyline.getColor() ^ 0x00ffffff;
polyline.setColor(strokeColor);
}
});
}
@Override
@ -154,4 +174,18 @@ public class PolylineDemoActivity extends AppCompatActivity
mMutablePolyline.setWidth(progress);
}
}
}
/**
* Toggles the clickability of two polylines based on the state of the View that triggered this
* call.
* This callback is defined on the CheckBox in the layout for this Activity.
*/
public void toggleClickability(View view) {
if (mClickablePolyline != null) {
mClickablePolyline.setClickable(((CheckBox) view).isChecked());
}
if (mMutablePolyline != null) {
mMutablePolyline.setClickable(((CheckBox) view).isChecked());
}
}
}

View File

@ -18,12 +18,14 @@
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:background="@color/white"
android:padding="5dp">
<TextView
android:id="@+id/transparency_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -32,31 +34,35 @@
<SeekBar
android:id="@+id/transparencySeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/transparency_text"
android:layout_toRightOf="@+id/transparency_text" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
<Button
android:id="@+id/switchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="5dp">
android:layout_below="@+id/transparencySeekBar"
android:onClick="switchImage"
android:text="@string/switch_image" />
<CheckBox
android:id="@+id/toggleClickability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/switchImage"
android:layout_toEndOf="@+id/switchImage"
android:layout_toRightOf="@+id/switchImage"
android:checked="true"
android:onClick="toggleClickability"
android:text="@string/clickable" />
<Button
android:id="@+id/switchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="switchImage"
android:text="@string/switch_image" />
</FrameLayout>
</RelativeLayout>
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -58,11 +58,26 @@
<SeekBar android:id="@+id/widthSeekBar" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/toggleClickability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:checked="true"
android:onClick="toggleClickability"
android:text="@string/clickable" />
</TableRow>
</TableLayout>
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -58,11 +58,26 @@
<SeekBar android:id="@+id/widthSeekBar" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/toggleClickability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:checked="true"
android:onClick="toggleClickability"
android:text="@string/clickable" />
</TableRow>
</TableLayout>
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -29,6 +29,7 @@
<string name="circle_demo_label">Circles</string>
<string name="circle_demo_description">Demonstrates how to add Circles to a map.</string>
<string name="clear_map">Clear</string>
<string name="clickable">Clickable</string>
<string name="compass">Compass</string>
<string name="custom_info_contents">Custom info contents</string>
<string name="custom_info_window">Custom info window</string>

View File

@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files