Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[android] - update LatLngBounds activity with BottomSheet interaction #9736

Merged
merged 1 commit into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,75 +1,122 @@
package com.mapbox.mapboxsdk.testapp.activity.camera;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdate;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.view.LockableBottomSheetBehavior;

import java.util.ArrayList;
import java.util.List;

/**
* Test activity showcasing using the LatLngBounds camera API.
* <p>
* This activity opens the map at zoom level 0 and animates into a bounds set by Los Angeles and New York
* with some additional padding and an animation duration of 1500 ms.
* </p>
*/
public class LatLngBoundsActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final LatLng LOS_ANGELES = new LatLng(34.053940, -118.242622);
private static final LatLng NEW_YORK = new LatLng(40.712730, -74.005953);

private final LatLng CHINA_BOTTOM_LEFT = new LatLng(15.68169, 73.499857);
private final LatLng CHINA_TOP_RIGHT = new LatLng(53.560711, 134.77281);
public class LatLngBoundsActivity extends AppCompatActivity implements View.OnClickListener {

private static final List<LatLng> LOCATIONS = new ArrayList<LatLng>() {
{
add(new LatLng(37.806866, -122.422502));
add(new LatLng(37.812905, -122.477605));
add(new LatLng(37.826944, -122.423188));
add(new LatLng(37.752676, -122.447736));
add(new LatLng(37.769305, -122.479322));
add(new LatLng(37.749834, -122.417867));
add(new LatLng(37.756149, -122.405679));
add(new LatLng(37.751403, -122.387397));
add(new LatLng(37.793064, -122.391517));
add(new LatLng(37.769122, -122.427394));
}
};
private static final LatLngBounds BOUNDS = new LatLngBounds.Builder().includes(LOCATIONS).build();
private static final int ANIMATION_DURATION_LONG = 450;
private static final int ANIMATION_DURATION_SHORT = 250;
private static final int BOUNDS_PADDING_DIVIDER_SMALL = 3;
private static final int BOUNDS_PADDING_DIVIDER_LARGE = 9;

private MapView mapView;
private MapboxMap mapboxMap;
private View bottomSheet;
private LockableBottomSheetBehavior bottomSheetBehavior;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visible_bounds);

setContentView(R.layout.activity_latlngbounds);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStyleUrl(Style.DARK);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final MapboxMap map) {
mapboxMap = map;
initMap();
}
});
}

private void initMap() {
addMarkers();
initFab();
initBottomSheet();
moveToBounds(bottomSheet.getMeasuredHeight(), BOUNDS_PADDING_DIVIDER_SMALL, 0);
}

private void addMarkers() {
for (LatLng location : LOCATIONS) {
mapboxMap.addMarker(new MarkerOptions().position(location));
}
}

private void initFab() {
findViewById(R.id.fab).setOnClickListener(this);
}

@Override
public void onMapReady(final MapboxMap map) {
mapboxMap = map;
moveToBounds(new LatLngBounds.Builder().include(NEW_YORK).include(LOS_ANGELES).build(), new int[] {0, 0, 0, 0});
new Handler().postDelayed(new Runnable() {
public void onClick(View v) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
v.animate().alpha(0.0f).setDuration(ANIMATION_DURATION_SHORT);
}

private void initBottomSheet() {
bottomSheet = findViewById(R.id.bottom_sheet);
bottomSheetBehavior = (LockableBottomSheetBehavior) BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setLocked(true);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_SETTLING) {
moveToBounds(0, BOUNDS_PADDING_DIVIDER_LARGE, ANIMATION_DURATION_LONG);
}
}

@Override
public void run() {
moveToBounds(new LatLngBounds.Builder().include(CHINA_BOTTOM_LEFT).include(CHINA_TOP_RIGHT).build(),
new int[] {100, 100, 100, 100 });
public void onSlide(@NonNull View bottomSheet, float slideOffset) {

}
}, 5000);
});
}

private void moveToBounds(LatLngBounds latLngBounds, int[] padding) {
mapboxMap.clear();
mapboxMap.addMarker(new MarkerOptions().position(latLngBounds.getNorthEast()));
mapboxMap.addMarker(new MarkerOptions().position(latLngBounds.getSouthEast()));
mapboxMap.addMarker(new MarkerOptions().position(latLngBounds.getSouthWest()));
mapboxMap.addMarker(new MarkerOptions().position(latLngBounds.getNorthWest()));
CameraUpdate update =
CameraUpdateFactory.newLatLngBounds(latLngBounds,
padding[0],
padding[1],
padding[2],
padding[3]);
mapboxMap.moveCamera(update);
private void moveToBounds(int verticalOffset, int boundsPaddingDivider, int duration) {
int paddingHorizontal = mapView.getMeasuredWidth() / boundsPaddingDivider;
int paddingVertical = (mapView.getMeasuredHeight() - verticalOffset) / boundsPaddingDivider;
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(
BOUNDS,
paddingHorizontal,
paddingVertical,
paddingHorizontal,
paddingVertical + verticalOffset),
duration
);
}

@Override
Expand Down Expand Up @@ -97,9 +144,9 @@ protected void onStop() {
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
Expand All @@ -109,8 +156,8 @@ protected void onDestroy() {
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.mapbox.mapboxsdk.testapp.view;

import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LockableBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {

private boolean locked = false;

public LockableBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setLocked(boolean locked) {
this.locked = locked;
}

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
boolean handled = false;
if (!locked) {
handled = super.onInterceptTouchEvent(parent, child, event);
}
return handled;
}

@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
boolean handled = false;
if (!locked) {
handled = super.onTouchEvent(parent, child, event);
}
return handled;
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target,
int nestedScrollAxes) {
boolean handled = false;
if (!locked) {
handled = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
return handled;
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy,
int[] consumed) {
if (!locked) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
}
}

@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
if (!locked) {
super.onStopNestedScroll(coordinatorLayout, child, target);
}
}

@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX,
float velocityY) {
boolean handled = false;
if (!locked) {
handled = super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
}
return handled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"
android:fillColor="#F1F1F1"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="375dp"
android:background="@color/primaryDark"
app:behavior_hideable="true"
app:behavior_skipCollapsed="false"
app:layout_behavior="com.mapbox.mapboxsdk.testapp.view.LockableBottomSheetBehavior"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_arrow_downward"
app:backgroundTint="@color/primary"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="top|end"/>

</android.support.design.widget.CoordinatorLayout>

This file was deleted.