Skip to content

Commit

Permalink
mapbox#1856 - Removing FAB from SDK and replacing with intial trackin…
Browse files Browse the repository at this point in the history
…g mode API. Making Compass visible under Toolbar.
  • Loading branch information
bleege committed Aug 10, 2015
1 parent 99e9d9b commit daa42fc
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.hardware.GeomagneticField;
Expand All @@ -26,7 +25,6 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ScaleGestureDetectorCompat;
import android.text.TextUtils;
Expand Down Expand Up @@ -126,13 +124,18 @@ public class MapView extends FrameLayout implements LocationListener {
// Holds the context
private Context mContext;

// Used for GPS
// Used for GPS / Location
private boolean mIsGpsOn = false;
private LostApiClient mLocationClient;
private LocationRequest mLocationRequest;
private ImageView mGpsMarker;
private Location mGpsLocation;

public enum UserLocationTrackingMode {
NONE, FOLLOW, FOLLOW_BEARING;
}
private UserLocationTrackingMode userLocationTrackingMode = UserLocationTrackingMode.FOLLOW;

// Used for compass
private boolean mIsCompassEnabled = true;
private ImageView mCompassView;
Expand All @@ -149,13 +152,13 @@ public class MapView extends FrameLayout implements LocationListener {
private boolean mCompassValid = false;

// Used for map toggle mode
private FloatingActionButton trackingModeButton;
private int trackingMode = 0;
// private FloatingActionButton trackingModeButton;
private long t0 = new Date().getTime();

// Used to manage Event Listeners
private ArrayList<OnMapChangedListener> mOnMapChangedListener;


//
// Properties
//
Expand Down Expand Up @@ -299,26 +302,12 @@ private void initialize(Context context, AttributeSet attrs) {
mCompassView.setContentDescription(getResources().getString(R.string.compassContentDescription));
LayoutParams lp = new FrameLayout.LayoutParams((int)(48 * mScreenDensity), (int)(48 * mScreenDensity));
lp.gravity = Gravity.TOP | Gravity.END;
int tenDp = (int)(10 * mScreenDensity);
lp.setMargins(tenDp, tenDp, tenDp, tenDp);
int twentyDp = (int)(20 * mScreenDensity);
lp.setMargins(twentyDp, twentyDp * 4, twentyDp, twentyDp);
mCompassView.setLayoutParams(lp);
addView(mCompassView);
mCompassView.setOnClickListener(new CompassOnClickListener());

// Setup tracking mode
trackingModeButton = new FloatingActionButton(mContext);
trackingModeButton.setContentDescription(getResources().getString(R.string.trackingModeButtonContentDescription));
trackingModeButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.background_material_light)));
trackingModeButton.setElevation(4);
trackingModeButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_my_location_black_24dp));
LayoutParams lp2 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.gravity = Gravity.BOTTOM | Gravity.END;
int twentyDp = (int)(20 * mScreenDensity);
lp2.setMargins(twentyDp, twentyDp, twentyDp, twentyDp);
trackingModeButton.setLayoutParams(lp2);
addView(trackingModeButton);
trackingModeButton.setOnClickListener(new ToggleModeClickListener());

// Setup Support For Listener Tracking
// MapView's internal listener is setup in onCreate()
mOnMapChangedListener = new ArrayList<OnMapChangedListener>();
Expand Down Expand Up @@ -512,6 +501,14 @@ public void toggleDebug() {
mNativeMapView.toggleCollisionDebug();
}

public UserLocationTrackingMode getUserLocationTrackingMode() {
return userLocationTrackingMode;
}

public void setUserLocationTrackingMode(UserLocationTrackingMode userLocationTrackingMode) {
this.userLocationTrackingMode = userLocationTrackingMode;
}

public boolean isFullyLoaded() {
return mNativeMapView.isFullyLoaded();
}
Expand Down Expand Up @@ -916,10 +913,6 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
return false;
}

if (trackingMode != 0) {
setTrackingMode(0);
}

// Cancel any animation
mNativeMapView.cancelTransitions(); // TODO need to test canceling
// transitions with touch
Expand Down Expand Up @@ -1608,11 +1601,9 @@ private class CompassOnClickListener implements View.OnClickListener {

@Override
public void onClick(View view) {
if(trackingMode == 2){
setTrackingMode(0);
}
resetNorth();
}

}

/**
Expand All @@ -1629,12 +1620,6 @@ public void onLocationChanged(Location location) {
private void updateLocation(Location location) {
if (location != null) {
mGpsLocation = location;

// Update map position if in follow mode
if (trackingMode != 0) {
setCenterCoordinate(new LatLng(mGpsLocation));
}

updateMap();
}
}
Expand Down Expand Up @@ -1679,7 +1664,7 @@ private void updateMap() {
mGpsMarker.requestLayout();

// Update direction if tracking mode
if(trackingMode == 2 && mCompassValid){
if(userLocationTrackingMode == UserLocationTrackingMode.FOLLOW_BEARING && mCompassValid){
// TODO need to do proper filtering (see branch filter-compass) or else map will lock up because of all the compass events
long t = new Date().getTime();
if((t-t0)>1000){
Expand All @@ -1688,6 +1673,13 @@ private void updateMap() {
}
}

/*
// Update map position if NOT in NONE mode
if (userLocationTrackingMode != UserLocationTrackingMode.NONE) {
setCenterCoordinate(new LatLng(mGpsLocation));
}
*/

/*
// Used For User Location Bearing UI
if (mGpsLocation.hasBearing() || mCompassValid) {
Expand All @@ -1708,49 +1700,4 @@ private void updateMap() {
}
}
}

private class ToggleModeClickListener implements OnClickListener {
@Override
public void onClick(View v) {
setTrackingMode((trackingMode + 1) % 3);
}
}

public void setTrackingMode(int trackingMode) {

this.trackingMode = trackingMode;

switch (trackingMode) {

case 0: {
trackingModeButton.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_my_location_black_24dp, mContext.getTheme()));
trackingModeButton.setColorFilter(Color.TRANSPARENT);
updateMap();
}
break;

case 1: {
trackingModeButton.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_my_location_black_24dp, mContext.getTheme()));
trackingModeButton.setColorFilter(Color.BLUE);
if(mGpsLocation != null){
setCenterCoordinate(new LatLng(mGpsLocation));
}
updateMap();
}
break;

case 2: {
trackingModeButton.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_explore_black_24dp, mContext.getTheme()));
trackingModeButton.setColorFilter(Color.BLUE);
if(mGpsLocation != null){
setCenterCoordinate(new LatLng(mGpsLocation));
}
updateMap();
}
break;

default:
break;
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="compassContentDescription">Map compass. Click to reset the map rotation to North.</string>
<string name="trackingModeButtonContentDescription">Press button to toggle tracking modes (None, Follow, FollowWithHeading)</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
mapView.setCompassEnabled(!mapView.isCompassEnabled());
break;

/*
case R.id.followNone:
mapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.NONE);
break;
case R.id.followFollow:
mapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.FOLLOW);
break;
case R.id.followBearing:
mapView.setUserLocationTrackingMode(MapView.UserLocationTrackingMode.FOLLOW_BEARING);
break;
*/

case R.id.actionStyleMapboxStreets:
changeMapStyle(getString(R.string.styleURLMapboxStreets));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
android:title="@string/action_compass"
/>

<!--
<group android:id="@+id/locationFollowModesSeparator"/>
<group android:id="@+id/locationFollowModes" android:checkableBehavior="single">
<item android:id="@+id/followNone"
android:title="@string/action_location_none"/>
<item android:id="@+id/followFollow"
android:title="@string/action_location_follow"/>
<item android:id="@+id/followBearing"
android:title="@string/action_location_bearing"/>
</group>
-->

<group android:id="@+id/stylesSeparator"/>

<group android:id="@+id/stylesGroup" android:checkableBehavior="single">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<string name="app_name">Mapbox GL</string>

<string name="action_gps">Toggle GPS location</string>
<string name="action_location_none">Toggle Location Bearing Tracking</string>
<string name="action_location_follow">Follow</string>
<string name="action_location_bearing">Bearing</string>
<string name="action_compass">Toggle Compass</string>
<string name="action_debug">Toggle debug mode</string>
<string name="action_point_annotations">Toggle point annotations</string>
Expand Down

0 comments on commit daa42fc

Please sign in to comment.