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

Added tracking modes (None, Follow, FollowAndHeading) for MapView #2004

Merged
merged 1 commit into from
Aug 7, 2015
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
1 change: 1 addition & 0 deletions android/java/MapboxGLAndroidSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
compile 'com.android.support:support-v4:22.2.1'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.mapzen.android:lost:1.0.1'
compile 'com.android.support:design:22.2.1'
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
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 @@ -22,6 +25,8 @@
import android.os.Build;
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 All @@ -37,6 +42,7 @@
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ZoomButtonsController;
Expand All @@ -58,6 +64,7 @@
import org.apache.commons.validator.routines.UrlValidator;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// Custom view that shows a Map
Expand Down Expand Up @@ -141,6 +148,11 @@ public class MapView extends FrameLayout implements LocationListener {
private float mCompassBearing;
private boolean mCompassValid = false;

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

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

Expand Down Expand Up @@ -293,6 +305,20 @@ private void initialize(Context context, AttributeSet attrs) {
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 @@ -890,6 +916,10 @@ 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 @@ -1578,6 +1608,9 @@ private class CompassOnClickListener implements View.OnClickListener {

@Override
public void onClick(View view) {
if(trackingMode == 2){
setTrackingMode(0);
}
resetNorth();
}
}
Expand All @@ -1596,6 +1629,12 @@ 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 @@ -1639,6 +1678,16 @@ private void updateMap() {
rotateImageView(mGpsMarker, 0.0f);
mGpsMarker.requestLayout();

// Update direction if tracking mode
if(trackingMode == 2 && 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){
t0 = t;
setDirection(-mCompassBearing, true);
}
}

/*
// Used For User Location Bearing UI
if (mGpsLocation.hasBearing() || mCompassValid) {
Expand All @@ -1659,4 +1708,49 @@ 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;
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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>