Skip to content

Commit

Permalink
mapbox#1940 - DRYing out GPS Marker setup. Confirming that On / Off t…
Browse files Browse the repository at this point in the history
…oggle and setup is done in correct order.
  • Loading branch information
bleege committed Aug 4, 2015
1 parent a00a553 commit 4f6f4bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,23 @@ private void initialize(Context context, AttributeSet attrs) {
onConnectivityChanged(isConnected);
}

// Setup User Location UI
mGpsMarker = new ImageView(getContext());
// mGpsMarker.setVisibility(View.INVISIBLE);
mGpsMarker.setImageResource(R.drawable.location_marker);
mGpsMarker.setLayoutParams(new FrameLayout.LayoutParams((int) (27.0f * mScreenDensity), (int) (27.0f * mScreenDensity)));
addView(mGpsMarker);

// Setup Location Services
mLocationClient = new LostApiClient.Builder(getContext()).build();
mLocationRequest = LocationRequest.create()
.setInterval(1000)
.setSmallestDisplacement(1)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

setOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged() {
// TODO - doesn't seem to be firing like it does client side

Log.i(TAG, "onMapChanged!");
updateMap();
}
});
}

//
Expand Down Expand Up @@ -1425,11 +1429,12 @@ private void toggleGps(boolean enableGps) {
} else {
if (mIsGpsOn) {
mIsGpsOn = false;
// LocationServices.FusedLocationApi.removeLocationUpdates(this);
LocationServices.FusedLocationApi.removeLocationUpdates(this);
mLocationClient.disconnect();
mGpsLocation = null;
}
}
updateMap();
}

/**
Expand All @@ -1438,28 +1443,45 @@ private void toggleGps(boolean enableGps) {
*/
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged: " + location);
Log.i(TAG, "onLocationChanged(): " + location);
updateLocation(location);
}

// Handles location updates from GPS
private void updateLocation(Location location) {
if (location != null) {
mGpsLocation = location;
updateMap();
}

updateMap();
}

// Updates the UI to match the current map's position
private void updateMap() {
Log.i(TAG, "updateMap()");
// rotateImageView(mCompassView, (float) mapView.getDirection());

if (mGpsLocation != null) {
if (isMyLocationEnabled() && mGpsLocation != null) {
if (mGpsMarker == null) {
// Setup User Location UI
// NOTE: mIsGpsOn == false to begin with
mGpsMarker = new ImageView(getContext());
mGpsMarker.setImageResource(R.drawable.location_marker);
addView(mGpsMarker);
}

mGpsMarker.setVisibility(View.VISIBLE);
LatLng coordinate = new LatLng(mGpsLocation);
PointF screenLocation = toScreenLocation(coordinate);

float iconSize = 27.0f * mScreenDensity;
// Update Location
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int) iconSize, (int) iconSize);
lp.leftMargin = (int) (screenLocation.x - iconSize / 2.0f);
lp.topMargin = getHeight() - (int) (screenLocation.y + iconSize / 2.0f);
mGpsMarker.setLayoutParams(lp);
// rotateImageView(mGpsMarker, 0.0f);
mGpsMarker.requestLayout();

/*
if (mGpsLocation.hasBearing() || mCompassValid) {
mGpsMarker.setImageResource(R.drawable.direction_arrow);
Expand All @@ -1473,15 +1495,6 @@ private void updateMap() {
mGpsMarker.requestLayout();
} else {
*/
mGpsMarker.setImageResource(R.drawable.location_marker);
float iconSize = 27.0f * mScreenDensity;
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int) iconSize, (int) iconSize);
lp.leftMargin = (int) (screenLocation.x - iconSize / 2.0f);
lp.topMargin = getHeight() - (int) (screenLocation.y + iconSize / 2.0f);
mGpsMarker.setLayoutParams(lp);
// rotateImageView(mGpsMarker, 0.0f);
mGpsMarker.requestLayout();
// }
} else {
mGpsMarker.setVisibility(View.INVISIBLE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ private class MyOnMapChangedListener implements MapView.OnMapChangedListener {

@Override
public void onMapChanged() {
Log.i(TAG, "onMapChanged()");
updateMap();
}
}
Expand Down

0 comments on commit 4f6f4bc

Please sign in to comment.